• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Animating with Swing Timer

 
Ranch Hand
Posts: 41
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't really know how to use javax.swing.timer in my application. My program animates a sprite moving across the screen to wherever you click, and it must look like it's traveling there. I used to use thread.sleep(), but that doesn't work anymore, so I've heard that I need to use javax.swing.timer. Here is my moveTo() method(I took the Thread.sleep() parts out):



I don't know how to insert the actionlistener. I have tried by myself but I can't seem to get it right. Btw, the moveTo method is called in a separate class, on the EDT.
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the basic scenario with a swing timer is

you draw your image (via the container's paintComponent()) at x,y
in the timer, you change x,y and call [container].repaint()
 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this isn't exactly the same thing, but it might give the answer you need.
 
Marshal
Posts: 79986
399
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am impressed by your mathematical abilities in that post
 
Randall Twede
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
lol Campbell.
i just wanted to mention that the timer can be started in the constructor. i had the run() method because i couldn't compute origin in my constructor. it seemed like a good place to start the timer.
 
Destroy anything that stands in your way. Except this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic