Win a copy of Succeeding with AI this week in the Artificial Intelligence and Machine Learning forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Junilu Lacar
Sheriffs:
  • Tim Cooke
  • Jeanne Boyarsky
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • fred rosenberger
  • Frits Walraven

Animating with Swing Timer

 
Ranch Hand
Posts: 41
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
this isn't exactly the same thing, but it might give the answer you need.
 
Marshal
Posts: 68985
275
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Whip out those weird instruments of science and probe away! I think it's a tiny ad:
Try Free Java/.NET Libraries for Word Excel PowerPoint and PDF
htttp://www.e-iceblue.com/free-apis.html
    Bookmark Topic Watch Topic
  • New Topic