1

I am doing some calculations and drawing based on the width/height of my JPanel (640x480), but when I output the actual position of my mouse, I notice that it has actual bounds of (622x441). Why is this, and how can I compensate for it?

Here is my constructor of GamePanel

public GamePanel(){ 
    wallBoard = new WallBoard();
    player = new Player(100, 100);
    Map<String, GameObject> startingEquipment = new HashMap<String, GameObject>();
    startingEquipment.put("Sword and Sheild", new Sword(player.getX(), player.getY()));
    player.equip(startingEquipment);
    enemy = new Enemy(10, 10);
    objects.add(player);
    objects.add(enemy);
    mouseSelection = new MouseSelection();

    setSize(new Dimension(640, 480));
    setDoubleBuffered(true);
    setFocusable(true);
    addKeyListener(this);
    addMouseListener(this);
    addMouseMotionListener(this);

    frame = new GameFrame();
    frame.pack(); //this makes the window just the bar at the top
            //frame.setUndecorated(true); crashes the program
    frame.addKeyListener(this);
    frame.add(this);


    t = new Thread(this);
    t.start();
    hud = new HeadsUpDisplay(getWidth(), getHeight());
}
ceptno
  • 687
  • 2
  • 6
  • 28
  • 1
    For better help sooner, post an [SSCCE](http://sscce.org/). It is possible you are getting co-ordinates relative to the frame rather than the panel. – Andrew Thompson Jan 05 '13 at 04:53
  • Sounds lie your trying to set the size of the window/frame, expecting its contents to be the same size. Windows typically have decoration that consume some of available space you've allocated. Try overriding the getPreferredSize method of your panel and return the desired size instead (don't forget to pack the window). Check [this out](http://stackoverflow.com/questions/13313084/graphics-rendering-in-title-bar/13316131#13316131) for an example/explanation and [this](http://stackoverflow.com/questions/13734069/how-can-i-set-in-the-midst/13734319#13734319) – MadProgrammer Jan 05 '13 at 05:07
  • After all you will post your answer again and accept it ignoring others help? – Nikolay Kuznetsov Jan 05 '13 at 05:54
  • Thanks @MadProgrammer, really great examples. I tried both of the suggestions in the first answer. Both of them made my window only the draggable bar at the top. What could be the cause of this? I am constructing the JFrame and adding the JPanel (GamePanel) to it inside the GamePanel class. Could that be the cause? – ceptno Jan 05 '13 at 06:32
  • @Brandon `JFame#pack` uses the content panes preferred size (plus some extra info) to determine the "preferred" size of the window. Try overriding the `GamePanel`'s `getPreferredSize` method to return the size you want – MadProgrammer Jan 05 '13 at 07:25

3 Answers3

2

The JPanel is the size you are setting it to be, you're just not seeing the whole thing. The reason why is because the frame decorations push the JPanel down and to the right. Instead of setting the size of the JFrame just call frame.pack() or just extend the JFrame a size where the entire JPanel can be seen. Or you can always set the JFrame undecorated by calling frame.setUndecorated(true) I had the same problem you had and thats how I fixed it. Good luck!

Here is the Java tutorial on JFrames:

Here is specifically the pack() method:

Note: If you use pack() make sure to set the JPanel's preferred size by using panel.setPreferredSize(width, height)

John
  • 3,769
  • 6
  • 30
  • 49
  • How about overriding the panel's getPreferredSize method so it returns the size of the panel you want and then using JFrame#pack to get the frame to meet the layout requirements of the content, saving you having to remove the frame decoration, which users tend to find useful – MadProgrammer Jan 05 '13 at 05:27
  • @MadProgrammer Ah yes I forgot you have to set the preferred size, good catch as always MadProgrammer – John Jan 05 '13 at 05:43
  • using setUndecorted gives me this error Exception in thread "main" java.awt.IllegalComponentStateException: The frame is displayable. at java.awt.Frame.setUndecorated(Unknown Source) at GamePanel.(GamePanel.java:53) at GamePanel.main(GamePanel.java:14) ... – ceptno Jan 05 '13 at 06:18
  • using frame.pack() makes the window just the bar. I am setting the size on the panel. What should I do with the size of the JFrame? If I set both to the same dimensions, and use frame.pack() will that do the trick? – ceptno Jan 05 '13 at 06:19
  • 1
    @Brandon Your exception is self explanatory - You can't call `setUndecorated` after you've called `setVisible(true)`. `pack` resizes the frame to match the layout's preferred size. This suggestions that the frames content has no preferred size (or your not using a layout manager). Override the `getPreferredSize` method of your frames primary container/panel to return an appropriate size. – MadProgrammer Jan 05 '13 at 07:24
0

Try this http://www.miglayout.com/ May be this will help you.

Himanshu
  • 706
  • 6
  • 27
0

One way to compensate for the lost mouse area would be to add the difference to the initial size, so when you create the JPanel instead of setting the width and height to 640x480, make it 638x519.

I'm not sure why exactly it does that, one guess would be that it takes away the demension because of the title and the side graphics, but I'm not posotive.

Jonny Henly
  • 4,023
  • 4
  • 26
  • 43
  • 1
    I guess your close to the answer, checkout [this](http://stackoverflow.com/questions/13734069/how-can-i-set-in-the-midst/13734319#13734319) for possible answer why – MadProgrammer Jan 05 '13 at 05:15