Results 1 to 4 of 4
Thread: Moving an image on a JFrame
- 06-03-2010, 01:40 PM #1
Member
- Join Date
- Jun 2010
- Posts
- 2
- Rep Power
- 0
Moving an image on a JFrame
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:
Java Code:package animationtest; public class AnimationTestStart { public static void main(String[] args) { Animation world = new Animation("Animation"); world.setVisible(true); world.run(); } } package animationtest; import java.awt.*; import javax.swing.*; import java.awt.event.*; public class Animation extends JFrame{ Image image; private SpaceShip player1 = new SpaceShip(); public Animation(String title) { setTitle(title); setSize(400,400); setLocation(100,100); setBackground(Color.black); setDefaultCloseOperation(EXIT_ON_CLOSE); addKeyListener( new KeyList()); } public void run() { while (true) { repaint(); } } public void paint(Graphics g) { player1.paint(g); repaint(); } private class KeyList extends KeyAdapter { public void keyPressed(KeyEvent k) { if (k.getKeyCode() == KeyEvent.VK_LEFT){ player1.moveLeft(); } if (k.getKeyCode() == KeyEvent.VK_RIGHT){ player1.moveRight(); } } } } package animationtest; import java.awt.*; import javax.swing.*; public class SpaceShip extends JFrame{ Image image; private int yMove = 370; private int xMove = 200; public SpaceShip() { image = getToolkit().getImage("SpaceShip.JPG"); } public void paint(Graphics g) { g.drawImage(image, xMove, yMove, this); } public void moveLeft() { xMove = xMove--; } public void moveRight() { xMove = xMove++; } public void moveReset(int value) { xMove = value; } }
- 06-03-2010, 03:39 PM #2
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
You where given the answer to this questions yesterday when you cross posted the question in this forum:
Simple Animation. (Swing / AWT / SWT / JFace forum at JavaRanch)
- 06-03-2010, 04:33 PM #3
Member
- Join Date
- Jun 2010
- Posts
- 2
- Rep Power
- 0
further update
Hi Rob
Thank you for help with this request, I was hoping for a different explanation why the program does not work.
Reading through the Swing manual and looking at Key Bindings has not really answered why the code I have put together does not do what I intended.
Cheers
Jin.
-
Thanks for desiring us to repeat work that has already been done. Next time, please let us know, else you could be on several black-lists or banned.
Rob thanks for the warning.
No, but in answers how do do what you are desiring correctly. Which is more important?Reading through the Swing manual and looking at Key Bindings has not really answered why the code I have put together does not do what I intended.
Edit: On review of the cross-post, I see that Rob did in fact give you the explanation for why you code doesn't work.
Similar Threads
-
How to add a background image to JFrame
By dunafrothint in forum AWT / SwingReplies: 1Last Post: 02-26-2010, 10:17 PM -
Add an image to JFrame
By Eranga in forum AWT / SwingReplies: 4Last Post: 02-01-2010, 03:09 PM -
JFrame + image
By Blacknight in forum New To JavaReplies: 2Last Post: 05-07-2009, 05:34 AM -
can display image in JFrame?
By xCLARAx in forum AWT / SwingReplies: 14Last Post: 04-03-2009, 07:02 PM -
moving image - PROBLEM
By Triss in forum New To JavaReplies: 3Last Post: 01-17-2008, 06:52 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks