• Post Reply Bookmark Topic Watch Topic
  • New Topic

Animation not showing up  RSS feed

 
Kris Saelen
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

i'm trying to move a circle on a Jform but the circle is not showing up. Below my code. I want to make it possible in the future to call several animations from this class.


 
Piet Souris
Rancher
Posts: 1783
55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can't test it at the moment, but first two remarks:

in your ActionListener (in MoveRight), you create a New DrawPanel every time the timer fires. Just one DrawPanel suffices...

You  do  not set any preferredSize for Draw panel

See if that helps.
 
Dave Tolls
Ranch Hand
Posts: 2504
27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don' t think you need any DrawPanels do you?
Why not just do the drawing in the paintComponent of the Anim class?
 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Why have you got an action listener as a local variable in the constructor? Why are you starting the timer in the constructor and stopping it in the action listener? You appear to want the circle to move 100 to the right every 0.5″. That means that after 8″ it will have disappeared off the right side of your screen, never to be found again. Try moving the circle by smaller distances.
 
Piet Souris
Rancher
Posts: 1783
55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:I don' t think you need any DrawPanels do you?
Why not just do the drawing in the paintComponent of the Anim class?

OP might be using a ButtonPanel as well in Anim. That's not unusual.
Campbell Ritchie wrote:Why have you got an action listener as a local variable in the constructor?

Can you elaborate a little why that would be a problem, since only the timer needs to access it?
 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:. . .  since only the timer needs to access [the action listener]?
Good point. I forgot that Timers can fire action listeners. Put that down as my mistake
 
Piet Souris
Rancher
Posts: 1783
55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No mistake! And I'm glad you raised the point. In java 8, one would probably do something like:


and for those doing their OCAJP (like me)  it is interesting to know what the difference is compared to using a local ActionListener. No doubt they have some very nasty questions about  this.   .
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!