Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Graphics sliding out of view when resizing window

 
Mikael Saltzman
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5125
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Randall Twede
Ranch Hand
Posts: 4369
3
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic