Jump to content
  • Advertisement
Sign in to follow this  
d000hg

[java] Finding the client rectangle of a JFrame

This topic is 4135 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have this very simple class:
public class simplegraphics extends JFrame
{
   public simplegraphics()
   {
      setVisible(true);
      setSize(800,800);
   }
}

I am having issues that I don't think the client area is 800x800 - pixels are taken up by the window border and title bar. In Windows there's a simple API call to find out the real rectangle I have to play with - how may I do this in Java?

Share this post


Link to post
Share on other sites
Advertisement
An easier way is to allow yourself to set the size like this.


public class SimplegGraphics extends JPanel
{
Dimension size;

public SimplegGraphics() {
size = new Dimension(800, 800);
}
public void paint(Graphics g) {
//custom painting
}
//return the size you want
public Dimension getPreferredSize() {
return size;
}
public static void main(String args[]) {
JFrame f = new JFrame();
f.setContentPane(new SimplegGraphics());
f.pack();
f.setVisible();
}
}




By doing it this way, you can set the area you want to draw to and do all your custom drawing in this class. This will almost guaruntee you will get the size you want. The only time you won't is if you let the user resize your frame. You can fix that problem by not letting the user resize or putting your SimpleGraphics in a scroll pane.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!