tags:

views:

630

answers:

3

Hi all, I am just messing around trying to make a game right now, but I have had this problem before too. When I specify a specific window size (1024 x 768 for instance) the window produced is just a little larger than what I specified. Very annoying. Is there a reason for this? How do I correct it so the window created is actually the size I want instead of being just a little bit bigger? Up till now I have always just gone back and manually adjusted the size a few pixels at a time until I got the result I wanted, but that is getting old. If there was even a formula I could use that would tell me how many pixels I needed to add/subtract from my my variable that would be excellent!

P.S. I don't know if my OS could be a factor in this, but I am using W7X64.

private int windowWidth = 1024;
private int windowHeight = 768;

public SomeWindow() {
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setSize(windowWidth, windowHeight);
    this.setResizable(false);
    this.setLocation(0,0);
    this.setVisible(true);
}
A: 

When you say the obtained window size is not the asked one, are you talking about the window, with its decorations ?

Indeed, window size is defined without OS specific window decoration.

Try to add

this.setUndecorated(true);

before the

this.setVisible(true);
Riduidel
A: 

In general, using pack is preferable to calling setSize, since pack leaves the frame layout manager in charge of the frame size, and layout managers are good at adjusting to platform dependencies and other factors that affect component size.

So I would go with:

this.getContentPane().setPreferredSize(new Dimension(windowWidth, windowHeight));
this.setResizable(false);
this.setLocation(0, 0);
this.pack();
this.setVisible(true);
rochb
Hummm... I think this is on the right track, but something is still off. When I do as you suggested the actual frame of the window does not change size from my original code, only the inner image. I want the total frame that Windows creates to be the exact size I specify.
typoknig
+1  A: 

I want the total frame that Windows creates to be the exact size I specify

I don't understand your problem. Post your SSCCE that shows the problem.

If I run code like:

frame.setResizable(false);
frame.setSize(1024, 768);
frame.setVisible(true);
System.out.println(frame.getSize());

It displays java.awt.Dimension[width=1024,height=768], is that not what you expect?

If there was even a formula I could use that would tell me how many pixels I needed to add/subtract from my my variable that would be excellent!

Maybe you are referring to the space occupied by the title bar and borders?

import java.awt.*;
import javax.swing.*;

public class FrameInfo
{
    public static void main(String[] args)
    {
        GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
        Rectangle bounds = env.getMaximumWindowBounds();
        System.out.println("Screen Bounds: " + bounds );

        GraphicsDevice screen = env.getDefaultScreenDevice();
        GraphicsConfiguration config = screen.getDefaultConfiguration();
        System.out.println("Screen Size  : " + config.getBounds());

        JFrame frame = new JFrame("Frame Info");
        System.out.println("Frame Insets : " + frame.getInsets() );

        frame.setSize(200, 200);
        System.out.println("Frame Insets : " + frame.getInsets() );
        frame.setVisible( true );

        System.out.println("Frame Size   : " + frame.getSize() );
        System.out.println("Frame Insets : " + frame.getInsets() );
        System.out.println("Content Size : " + frame.getContentPane().getSize() );
     }
}
camickr
I used this code and it tells me that the frame that W7 creates for is indeed the size I specified, but it is still a few pixels larger than my 1024 x 768 monitor. I have 3 monitors (a 1920 x 1080 with a 1024 x 768 on both sides) and I noticed that even though I have specified setLocation(0,0) the window created in my main monitor is just a few pixels into the monitor to my left. Annoying but I guess this is how W7 works. I see no option except to deal with it or change the numbers in small increments until I get the desired results. camickr for the win... (helping to right past wrongs).
typoknig
Maybe you will find frame.setExtendedState(JFrame.MAXIMIZED_BOTH); a better option? Then you don't have to worry about the manually specifying the sizes.
camickr
That also sounds like a good idea. Is there anyway to use that while also specifying that I do not want the frame to go over a certain size? For instance if I used that method on my set up the resulting window would fill up my main monitor at 1920 x 1080, I wouldn't want the window being created to ever be any bigger than 1280 x 1024 I'll read through the API and see what I can come up with. Bare with me Camickr, there are going to be a lot more questions from me as I learn to code. Please correct me if I use the wrong termonology too. Thanks for your help.
typoknig
frame.setMaximizedBounds(...);
camickr