• Post Reply Bookmark Topic Watch Topic
  • New Topic

Using Graphics from anonymous class

 
Amr Hesham
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a part of my code which does some sort of animation; however, there seems to be something wrong: Whenever I try to use the passed 'g' from inside the anonymous class to draw anything, it does nothing, yet when I used it outside the anonymous class (inside the rollBalls method) it does what it's supposed to do. Any idea why? And how do I fix this? Thank you.


 
Paul Clapham
Sheriff
Posts: 21892
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The purpose of the paintComponent method is to paint the component. The Graphics object you get as a parameter can be used to paint the component, but you can't store it and use it later. Which is what your code does.

In fact the name of the method suggests that having it schedule tasks to run later is not a sensible thing to do. Remember that the paintComponent method can be called at any time. For example it's called when you un-minimize your application, or when you minimize some other application which was hiding your application. So it makes no sense to kick off a series of tasks to happen after those events.

So don't do that. Just have paintComponent paint the component with the latest version of the model data. If you have to have something schedule tasks to happen once per millisecond, it has to be something else. And the tasks should modify the model data and call the repaint() method of the component. They shouldn't attempt to paint the component themselves.
 
Amr Hesham
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:The purpose of the paintComponent method is to paint the component. The Graphics object you get as a parameter can be used to paint the component, but you can't store it and use it later. Which is what your code does.

In fact the name of the method suggests that having it schedule tasks to run later is not a sensible thing to do. Remember that the paintComponent method can be called at any time. For example it's called when you un-minimize your application, or when you minimize some other application which was hiding your application. So it makes no sense to kick off a series of tasks to happen after those events.

So don't do that. Just have paintComponent paint the component with the latest version of the model data. If you have to have something schedule tasks to happen once per millisecond, it has to be something else. And the tasks should modify the model data and call the repaint() method of the component. They shouldn't attempt to paint the component themselves.


Thank you, but still why is it painting correctly from rollBalls() but not from its anonymous class?
 
Darryl Burke
Bartender
Posts: 5155
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amr Hesham wrote:Thank you, but still why is it painting correctly from rollBalls() but not from its anonymous class?


Paul already answered that.

Paul Clapham wrote:The purpose of the paintComponent method is to paint the component. The Graphics object you get as a parameter can be used to paint the component, but you can't store it and use it later. Which is what your code does.

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!