Two Laptop Bag
The moose likes Swing / AWT / SWT and the fly likes Graphics coordinates problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Graphics coordinates problem" Watch "Graphics coordinates problem" New topic

Graphics coordinates problem

Jim Longmore

Joined: May 04, 2005
Posts: 12
Hi all,

I'm fiddling around with Swing, and decided to create a simple program that will graph a set of data you provide it. In order to do this, I created a subclass of JPanel so I could override the paint() method and use the Graphics class to draw lines etc straight to the panel. However, when I try to add it to my JFrame, and size it to the exact size of the JFrame, it extends over the edge of the JFrame, and the entire panel is only shown when I enlarge the frame to fit.

The code I'm using is below.

I'm not particularly experienced with Swing/AWT, so I may well be using methods incorrectly. Any help is appreciated.


<i>One thought away from chaos</i>
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
You do not need to call 'setSize' on 'graph'. The general practice is to limit the use of 'setSize' to top–level containers. You can let the JFrame maximize the size of your graphic component by adding it to the center section of the JFrames default BorderLayout.
Jim Longmore

Joined: May 04, 2005
Posts: 12
I've changed tact slightly (now using panels and layout managers), now the only problem I'm having is still the co-ordinate thing. Where my problem lies (I'm guessing) is getting the width of a component? I've been using the getSize() method, but I'm self-taught, so I don't know what the standard practice is.

Any ideas?
colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 750
I kind of get the same problem too, if I set a frame size as 500 by 500,
then using the mouse, you can see the grey area starts at x=4 to x=496(more or less), so I just compensate by using a wider frame.
I find using the mouse a good way to check your screen is the right size.
But I'm no expert either
Jim Longmore

Joined: May 04, 2005
Posts: 12

I think your problem might be solved by the use of the Inset class. The Inset class records the margins for a particular component, see the Java documentation for more details.

I may be wrong, but i guess its worth a try.
I agree. Here's the link:
subject: Graphics coordinates problem
jQuery in Action, 3rd edition