• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Graphics sliding out of view when resizing window

 
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
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • 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.
 
Ranch Hand
Posts: 4702
9
Scala 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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!