Results 1 to 4 of 4
  1. #1
    jinkazama is offline Member
    Join Date
    Jun 2010
    Posts
    2
    Rep Power
    0

    Default 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;  
        }  
    }

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    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)

  3. #3
    jinkazama is offline Member
    Join Date
    Jun 2010
    Posts
    2
    Rep Power
    0

    Default 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.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by jinkazama View Post
    Hi Rob
    Thank you for help with this request, I was hoping for a different explanation why the program does not work.
    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.


    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.
    No, but in answers how do do what you are desiring correctly. Which is more important?

    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

  1. How to add a background image to JFrame
    By dunafrothint in forum AWT / Swing
    Replies: 1
    Last Post: 02-26-2010, 10:17 PM
  2. Add an image to JFrame
    By Eranga in forum AWT / Swing
    Replies: 4
    Last Post: 02-01-2010, 03:09 PM
  3. JFrame + image
    By Blacknight in forum New To Java
    Replies: 2
    Last Post: 05-07-2009, 05:34 AM
  4. can display image in JFrame?
    By xCLARAx in forum AWT / Swing
    Replies: 14
    Last Post: 04-03-2009, 07:02 PM
  5. moving image - PROBLEM
    By Triss in forum New To Java
    Replies: 3
    Last Post: 01-17-2008, 06:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •