• Advertisement
Sign in to follow this  

[java] Finding the client rectangle of a JFrame

This topic is 4039 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
You could try subtracting getInsets() from the given size, or getContentPane().getSize().

Share this post


Link to post
Share on other sites
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