This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Simple Animation. Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Reply Bookmark "Simple Animation." Watch "Simple Animation." New topic
Author

Simple Animation.

jin kazama
Greenhorn

Joined: Mar 09, 2010
Posts: 7
Hi There

I am trying to learn some simple animation with Java and swing. The code below should create a frame and add a simple graphic to the screen. I have then added a Key Listener to capture the left and right cursor keys.

When the left or right key is pressed the image should move left or right. Currently it draws the image onto the frame and that is all I get, your advise is appreciated:





Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 1788
    
    2
1) First you need to learn the basics of doing custom painting. Read the section from the Swing tutorial on Custom Painting.

2) There is no need to create an infinite loop. The GUI will repaint itself when you repaint the image.

3) Also, KeyEvents are only received by components that have focus. By default a panel does not get focus, so you need to make the panel focusable. However, you should NOT be using KeyEvent to handle the left/right KeyStrokes. You SHOULD be using Key Bindings. Again the Swing tutorial has a section that explains what Key Bindings are.

Happy reading.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Simple Animation.
 
Similar Threads
Having problems with Window and Frame
Getting this to move.
Need help with my game.
display mouse pointer coordinates
Racing game does not work(HELP!!!!)