Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (780)
Games in Android Showcase (233)
games submitted by our members
Games in WIP (856)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  JFrame dimensions?  (Read 1647 times)
0 Members and 1 Guest are viewing this topic.
Offline nhmllr

Senior Devvie

Medals: 1
Projects: 3

slow and steady...

« Posted 2013-12-31 02:46:30 »

I want my next game to have a re sizable window (no external libraries used)

How do I get the size of the interior of the JFrame?
What I mean is, I can get the total width/height of the JFrame "frame" via "frame.getWidth()"

The problem is that I set the size of the frame to be 640 x 480, but when I get the dimensions via the method above, I get 646 x 509.

I can only imagine that the numbers get bigger because of the size of the border of the window.

But I don't want those dimensions, I want the dimensions of what I'm actually drawing on.

I know I could just subtract 6 and 29, but that won't work for every OS.

So how can I get the dimensions I'm looking for?

Offline Abuse

JGO Ninja

Medals: 71

falling into the abyss of reality

« Reply #1 - Posted 2013-12-31 03:08:14 »


Though it's less work (and probably more reliable) to draw onto a child Component (JPanel for instance), rather than directly onto the top-level Window.
Offline nhmllr

Senior Devvie

Medals: 1
Projects: 3

slow and steady...

« Reply #2 - Posted 2013-12-31 03:40:12 »

So how could I get the width and height using the aforementioned frame.getWidth() and the "getInset()" method, because the only variables I see from the Inset are top, bottom, right, and left, and I don't quite see how to use that information. I don't quite understand what to add and subtract.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline BoBear2681

JGO Coder

Medals: 19

« Reply #3 - Posted 2014-01-01 05:51:47 »

Abuse's second suggestion is the better approach.  You're rendering to some component you shove into a JFrame, such as a Canvas or JPanel.  Use setPreferredSize(new Dimension(640, 480) to set the size of that component.  Then your drawing surface is 640x480, and you don't have to care about the window's frame size.

On resize events, just grab the new size of the Canvas or JPanel you're rendering on.  Then you can decide whether to stretch your screens to fit, center them, etc.
Pages: [1]
  ignore  |  Print  

hadezbladez (712 views)
2018-11-16 13:46:03

hadezbladez (352 views)
2018-11-16 13:41:33

hadezbladez (697 views)
2018-11-16 13:35:35

hadezbladez (175 views)
2018-11-16 13:32:03

EgonOlsen (2366 views)
2018-06-10 19:43:48

EgonOlsen (2478 views)
2018-06-10 19:43:44

EgonOlsen (1465 views)
2018-06-10 19:43:20

DesertCoockie (2130 views)
2018-05-13 18:23:11

nelsongames (1909 views)
2018-04-24 18:15:36

nelsongames (2575 views)
2018-04-24 18:14:32
Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45

Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20

Deployment and Packaging
by philfrei
2018-08-19 23:54:46 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!