Java Newbie

D

David McCallum

I'm creating frames in Java, is there a way to automatically center a frame
in the window.

Or, how do I find out what resolution my screen is so I can calculate the
center

David McCallum
 
D

Danny Woods

David McCallum said:
I'm creating frames in Java, is there a way to automatically center a frame
in the window.

Or, how do I find out what resolution my screen is so I can calculate the
center

David,

Your frame has a getToolkit() method, which in turn supplies getScreenSize(),
which returns a Dimension object containing the screen height/width in pixels.

You can figure out the correct xy coordinates for your frames from there, with
code like this in your Frame constructor (after using pack() or setting the
desired size):

Dimension size = getSize();
Dimension screen = getToolkit().getSreenSize();
int x = (screen.width - size.width) >> 1;
int y = (screen.height - size.height) >> 1;
setLocation(x,y);

Hope that helps,

Danny.
 
D

Danny Woods

Danny Woods said:
Dimension screen = getToolkit().getSreenSize();

Of course, the example code works even better without the typo. You may have
guessed that I didn't compile this ;-)

Danny.
 
P

Phil...

I just divide by 2 rather than shift.

Danny Woods said:
David,

Your frame has a getToolkit() method, which in turn supplies getScreenSize(),
which returns a Dimension object containing the screen height/width in pixels.

You can figure out the correct xy coordinates for your frames from there, with
code like this in your Frame constructor (after using pack() or setting the
desired size):

Dimension size = getSize();
Dimension screen = getToolkit().getSreenSize();
int x = (screen.width - size.width) >> 1;
int y = (screen.height - size.height) >> 1;
setLocation(x,y);

Hope that helps,

Danny.
 
K

KC Wong

I just divide by 2 rather than shift.

Are compilers these days smart enough to do that (convert divide by 2 to
right shift 1)?... Now I have some experiments to do this weekend :)

KC
 
D

Danny Woods

Roedy Green said:
If they know both operands are positive. Modulus and divide get
tricky as soon as any negatives as possible and you can't always use
the shift substitution.

True. But in this case, if both operands are *not* positive, the original
poster has other problems.

I just right-shift instead of dividing by powers of 2 in case the compiler
*isn't* smart enough, as most of the data I work with in a regular basis
is video/colour dat: performance savings on per-pixel operations are
important with 640x480 video refreshing at 30fps.

Admittedly, in the answer to the original question, I right-shifted out of
force of habit. Straight /2 would have been clearer. Oops.

Regards,

Danny.
 
W

Walter Mitty

"KC Wong" <[email protected]> brightened my day with
his incisive wit when in he conjectured that:
Are compilers these days smart enough to do that (convert divide by 2 to
right shift 1)?... Now I have some experiments to do this weekend :)

They have been smart enough for years to do that in C etc.

The only thing to be careful of is when using signed values. be sure the
bit wise shift doesn't start bringing ones in on the msb.
 
P

Phil...

I'm confused, how can you center a window on the screen
and end up with negative numbers? I suppose if the size of the
frame is larger than the screen (highly unlikely). For maintenance
purposes most novice programmers know what divide by 2 does.
 
R

Roedy Green

I'm confused, how can you center a window on the screen
and end up with negative numbers?

The compiler in general does not know which variables will ever go
negative and which won't. You "know" that, but there is really
nothing to stop variables going negative. In that case the compiler
has to inhibit using shift for divide. However, you can live
"dangerously" and do the substitution manually. It will simply screw
up if you feed it negative numbers.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top