Java - 按键时更改图像

时间:2012-10-15 19:01:57

标签: java image if-statement keylistener

Java新手在这里!

我正在开发一款只是在屏幕上移动的小游戏,它会攻击我正在制作的小AI矩形。

我有两个GIF文件pikachu.gif(是的,它是一个口袋妖怪游戏)和pikready.gif。

我已经实现了KeyListener和所有内容,并且可以使用箭头键让Pikachu.gif在框架内移动。

我想要的是,当我按下箭头键时将图像更改为pikready.gif,我在网上看了看,似乎无法找到答案。

代码在下面,任何注释掉的东西都是我尝试的不同东西。

正如我所说的Java新手所以对我很轻松!

package game;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.ImageIcon;
import javax.swing.JFrame;

public class PikachuAttack extends JFrame implements Runnable{

    Graphics dbg;
    Image dbImage;
    Image Pik1;
    static ImageIcon active;

    int x, y, xDirection, yDirection;

    public void run(){
        try{
            while(true){
                move();
                Thread.sleep(10);
            }
        }catch(Exception e){
            System.out.println("Uh-oh, something went wrong!.");
        }
    }

    private void move() {
        x += xDirection;
        y += yDirection;

    }

     public void setXDirection(int xdir) {
            xDirection = xdir;
        }

        public void setYDirection(int ydir) {
            yDirection = ydir;
        }


    // KEY COMMANDS //
      public class AL extends KeyAdapter {
        @Override
        public void keyPressed(KeyEvent e) {
            int keyCode = e.getKeyCode();

                if(keyCode == e.VK_LEFT) {
                    setXDirection(-1);
                }
                if(keyCode == e.VK_RIGHT) {
                    setXDirection(+1);
                }
                if(keyCode == e.VK_UP) {    
                    setYDirection(-1);
                }
                if(keyCode == e.VK_DOWN) {
                    setYDirection(+1);
                }   
                /*(if(keyCode == e.VK_LEFT | keyCode == e.VK_RIGHT | keyCode == e.VK_UP | keyCode == e.VK_DOWN){
                    active = new ImageIcon("C:\\Users\\Neil\\workspace\\MyOwnTutorials\\bin\\game\\pikready.gif");
                }else{
                    active = new ImageIcon("C:\\Users\\Neil\\workspace\\MyOwnTutorials\\bin\\game\\pikachu.gif");
                } */

        }

        @Override
        public void keyReleased(KeyEvent e) {
            int keyCode = e.getKeyCode();
                    if(keyCode== e.VK_LEFT){
                       setXDirection(0);                    
                    }
                    if(keyCode== e.VK_RIGHT){
                       setXDirection(0);                    
                    }
                    if(keyCode== e.VK_UP){
                        setYDirection(0);
                    }
                    if(keyCode== e.VK_DOWN){
                        setYDirection(0);                   
                    }

        }

        @Override
        public void keyTyped(KeyEvent e) {


        }

    }

    // CONSTRUCTOR //
    public PikachuAttack(){

        //Image Import
        ImageIcon still = new ImageIcon("C:\\Java\\GIFS\\Pikachu.gif");
        Pik1 = still.getImage();
        //ImageIcon ready = new ImageIcon("C:\\Users\\Neil\\workspace\\MyOwnTutorials\\bin\\game\\pikready.gif");
        //Pik2 = ready.getImage(); */
        // Pik1 = active.getImage();

        //JFrame properties
        addKeyListener(new AL());
        setTitle("Pikachu Attack");
        setSize(500, 500);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);
        setVisible(true);

        x = 15;
        y = 15;
    }



    public void paint(Graphics g){
        dbImage = createImage(getWidth(), getHeight());
        dbg = dbImage.getGraphics();
        paintComponent(dbg);
        g.drawImage(dbImage, 0, 0, this);   

    }

    public void paintComponent(Graphics g){
        g.setColor(Color.red);
        g.fillRect(200, 190, 12, 20);
        g.drawImage(Pik1, x, y, this);
        g.setColor(Color.red);



        repaint();
    }


    public static void main(String[] args) {
        PikachuAttack game = new PikachuAttack();
        Thread t1 = new Thread(game);
        t1.start();
    }

}

3 个答案:

答案 0 :(得分:1)

为什么不创建对当前Image的新引用,比如currentImage和DOWN键,请执行以下操作:

if (keyCode == e.VK_DOWN) {
   setYDirection(+1);
   currentImage = Pik2; // the ready image
}   

然后在paintComponent中,您可以绘制当前图像:

g.drawImage(currentImage, x, y, this);

答案 1 :(得分:0)

重绘后再打电话

revalidate();

答案 2 :(得分:0)

您应该创建一个具有空值或默认图像的图像,然后创建第二个图像,当您按下该键时将取代第一个图像。在键监听器中,您应该将图像的值设置为秒在您要按的键的键代码下的图像。

这是一个例子。按下键时移动的charechter与更改图像

public class WorldOfWarriorsDefault extends JPanel implements KeyListener, ActionListener //main game interface
{
	Timer t = new Timer(5, this); 	
	int x = 0, y = 0, velx = 0, vely = 0; //declares the default values co-ordinates of the moving object
    Rectangle r = new Rectangle(100, 100, 40, 40);
    Toolkit toolkit = Toolkit.getDefaultToolkit();
    Image image1 = toolkit.getImage("D:\\My Stuff\\frame_2_delay-0.05s.gif");
    Image image2 = toolkit.getImage("D:\\My Stuff\\runtest2.gif");
    Image image3 = toolkit.getImage("D:\\My Stuff\\ezgif-3-4570fcd5e5.gif");
    Image image4 = toolkit.getImage("D:\\My Stuff\\Leftr.gif");
    Image image = image1;
    public WorldOfWarriorsDefault() //makes a constructor
    {
    	t.start(); //starts the timer
    	addKeyListener(this);
    	 //adds a keyboard listener
    	setFocusable(true); 
    	setFocusTraversalKeysEnabled(false); 
    }
    public void actionPerformed(ActionEvent e) 
    {
        x = x + velx;
        y = y + vely;
        repaint();
    }
    public void up()
    {
    	vely = -2;
    	velx = 0;
    }
    public void down()
    {
    	vely = 2;
    	velx = 0;
    }
    public void left()
    {
    	velx = -2;
    	vely = 0;
    }
    public void right() 
    {	
    	velx = 2;
    	vely = 0;
    }
    public void keyPressed(KeyEvent e) 
	{
		
		int code = e.getKeyCode(); 
		
        if (code == KeyEvent.VK_UP || code == KeyEvent.VK_W) 
        {
        	up();
        }
        if (code == KeyEvent.VK_DOWN || code == KeyEvent.VK_S) 
        {
        	down();
        }
        if (code == KeyEvent.VK_LEFT || code == KeyEvent.VK_A) 
        {
        	left();
        	image = image3;
        }
        if (code == KeyEvent.VK_RIGHT || code == KeyEvent.VK_D) 
        {
        	right();
        	image = image2;
        }
	}
    public void keyTyped(KeyEvent e) {}
	public void keyReleased(KeyEvent e) 
	{
		int coder = e.getKeyCode();
		
		if (coder == KeyEvent.VK_LEFT)
		{
			image = image4;
		}
		if (coder == KeyEvent.VK_RIGHT)
		{
			image = image1;	
		}
		velx = 0;
		vely = 0;
		x = r.getBounds().x;
		y = r.getBounds().y;
		repaint();
	}
	public void paintComponent(Graphics g) //starts a painter
    {
    	super.paintComponent(g); //paints the component
    	r.setBounds(x, y, 40, 40);
    	g.drawImage(image, x, y, this); 
    }
    public static void main(String[] args) 
	{
          JFrame f = new JFrame();
          WorldOfWarriorsDefault w = new WorldOfWarriorsDefault();
          f.add(w);
          f.setVisible(true);
          f.setSize(1900, 1000);
          f.setLocationRelativeTo(null);
          f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
}