How to move image in Applet?

Posted by user1609804 on Stack Overflow See other posts from Stack Overflow or by user1609804
Published on 2012-09-02T00:58:31Z Indexed on 2012/09/02 3:38 UTC
Read the original article Hit count: 104

Filed under:
|
|
|
|

I want to move the character left, right up, and down in applet, but it is not moving at all. here is my code, help please

import javax.swing.JPanel;

import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
import java.applet.*;
import java.awt.event.*;
import java.awt.*;

public class drawCenter extends Applet
{
    private int x,y;// the x and y of the position of the player
    private BufferedImage image, pos;

    public void init(  )
    {
        try
        {
            image = ImageIO.read(new File("pokemonCenter.png"));
            pos = ImageIO.read(new File("player/maleInGame.png"));
        }
        catch (IOException ex)
        {

        }
        x = 150; y = 171;
    }

    public void keyPressed(KeyEvent e)
    {
        int keyCode = e.getKeyCode();

        switch( keyCode )
        {
            case KeyEvent.VK_UP: if( y>0 )
            {
                y=y-19;
                repaint();

            }

            break;
            case KeyEvent.VK_DOWN: if( y<171 )
            {
                y=y+19;
                repaint();

            }

            break;
            case KeyEvent.VK_LEFT:if( x>0 )
            {
                x=x-15;
                repaint();

            }

            break;
            case KeyEvent.VK_RIGHT:if( x<285 )
            {
                x=x+15;
                repaint();

            }

            break;
        }
        e.consume();
    }

    public void keyReleased(){

    }

    public void paint( Graphics g )
    {
        g.drawImage(image, 0, 0, null);
        g.drawImage(pos, x, y, null);
    }
}

© Stack Overflow or respective owner

Related posts about java

Related posts about image