Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Graphics coordinates problem

 
Jim Longmore
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


Jim
 
Craig Wood
Ranch Hand
Pie
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Colin,

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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic