File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Graphics sliding out of view when resizing window Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Graphics sliding out of view when resizing window" Watch "Graphics sliding out of view when resizing window" New topic
Author

Graphics sliding out of view when resizing window

Mikael Saltzman
Greenhorn

Joined: Dec 07, 2012
Posts: 9
Hi!

I have created a class that extends JPanel and have passed a Graphics object to its paintComponent method which draws a couple of shapes onto it, and added it to a JFrame object. I have set this frame's size and made it visible.

All works fine except for the shape sliding more and more out of view (down and right) every time I resize the window at runtime.

Would be great if you could point me to some JFrame method or something that could make these elements stay in their initial position in the frame, regardless of window size.

Best,
Mikael
Mikael Saltzman
Greenhorn

Joined: Dec 07, 2012
Posts: 9
Oh my, I just realized that this was due to incremental operators left in drawRect's parameter from previous experimentation. So, to answer my own silly little question, the paintComponent method is called every time the window is resized. Lesson learned. If answering your own question is considered a bit silly, I won't sadden me if you remove it, but maybe someone'll find it useful.

Best,
Mikael
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

Moral of the story: use painting methods for painting and painting alone. Never include business logic in a painting method override.

And answering your own question is smart, not silly.


luck, db
There are no new questions, but there may be new answers.
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4339
    
    2

and no, don't feel bad about it. often just formulating the question so others can understand it gives you the clue you need to solve it. i am probably the worst person here for this. i get frustrated, ask a question, take a brak and come back and say "oh, never mind i figured it out". but please give the solution when you do this


SCJP
Visit my download page
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Graphics sliding out of view when resizing window
 
Similar Threads
nested components and sizing
determining minimum dimensions of an Applet
JFrames and JPanel
Questions regarding to using applets and JComponents
updating JPanel in a JFrame