2

Ok, this sort of have 2 parts of the question.

  1. When I make a JFrame, and draw something on it, even if I make the width 400, and make it so that when an item hits it (Allowing for an items width, of course) It bounces back. But it always goes about 10 pixels off the screen for some reason. Is there a way to fix it, or do I just need to compensate by add/subtracting numbers?

  2. How can I get the EXACT screen center. So, if I make a JFrame which is default as 200*200 pixels, then the center would be 100*100. (Allowing for the problem in question 1) But if someone resizes the screen (and yes, I want them to be able to re-size it) the point is still the center. so if they make it full screen and their screen size is 1200*900, then the center would be 600*450.

I hope this is clear enough

mre
  • 43,520
  • 33
  • 120
  • 170
Poo2thegeek
  • 45
  • 1
  • 1
  • 9
  • 2
    screen dimensions are always even. you can't get the EXACT center point. LOL – jondinham Nov 19 '12 at 15:48
  • I presume I'm missing something - but why can't you just do getWidth()/2 getHeight()/2 to find the centre? – Michael Berry Nov 19 '12 at 15:48
  • Well that makes me feel stupid... lol – Poo2thegeek Nov 19 '12 at 15:49
  • But the second bit of info, is why does my item go off the screen even though it shouldnt? (As in question 1) – Poo2thegeek Nov 19 '12 at 15:49
  • For question 1: Are you sure you are getting the width of your **drawnig area** and not the width of the **frame**? These may differ. – svz Nov 19 '12 at 15:53
  • WHat i did was make 2 variables, one called height, one called width, and used tyhose while making my JFrame the right size. Then when I drew the ball and all that stuff, I made it so that when it hits the width - 20 (Thats the size of the ball) It bounces back. But instead i needed to compenstate by an aditional 18 pixels – Poo2thegeek Nov 19 '12 at 15:57
  • 2
    This might happen because the JFrame size includes menu and different decorators, i.e areas where you cannot draw anything. Not sure about the width part, but for height it is definitely so. The better practice is to create a *drawing area* and draw whatever you want on it. Then just place it on your frame. Then you won't have this kind of trouble. – svz Nov 19 '12 at 16:01

3 Answers3

9

From the sounds of what you are saying, I think you misunderstand what the dimensions of a normal (decorated) frame include.

A JFrame consists of a window/frame (normally decorated with a border), a JRootPane, which contains a JLayeredPane which contains the content pane, JMenuBar and glass pane.

enter image description here

This is, one of the, reasons why you should never override a top level containers paint method, because you won't actually be painting within the content/view area.

So, the actual "paintable" region of a frame is it's width - border.width x height - border.height

enter image description here

The red line indicates the frame, the blue indicates the content pane.

This raises a very important question, where's the center of the frame? From the frames perspective, it's 100x100, but from the content pane's perspective, it's 92x81. Depending on what you want, will depend on which value you will use. For positioning the frame, you will want to to use the frames center point, for painting, your will want to use the content panes.

Now, the easiest way to center a frame on the screen is simply to call Window#setLocationRelativeTo(null) otherwise, I would suggest you use Timr's solution ;)

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
3

This is what I use to centre a JFrame on screen, it simply retrieves the height and width of your monitor, then centres your frame

public static void moveToCenterScreen(JFrame frame) {
    Toolkit kit = frame.getToolkit();       
    GraphicsDevice[] gs = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices();
    Insets in = kit.getScreenInsets(gs[0].getDefaultConfiguration());
    Dimension d = kit.getScreenSize();

    int max_width = (d.width - in.left - in.right);
    int max_height = (d.height - in.top - in.bottom);   

    frame.setLocation((int) (max_width - frame.getWidth()) / 2, (int) (max_height - frame.getHeight() ) / 2);
}
Timr
  • 1,012
  • 1
  • 14
  • 29
2

You could use something simple along the lines of

int ___ = (Jframe.getWidth())/2
int ___ = (Jframe.getHeight())/2

The blank would be the value you want to be at the center point.

As for the other problem, you could have some padding or something that is moving the item around. Not sure without seeing the code for it.

  • Unless the frame is undecorated (which is unspecified), frames have borders inset into the bounds of the frame (so that the actually content area is smaller then the width/height of the frame) – MadProgrammer Nov 19 '12 at 19:42