如何使用KeyListener绘制不同的图像?

时间:2015-10-27 19:55:14

标签: java swing graphics keylistener

这只是我的JFrame文件。我想在我的角色向下移动时绘制down.png,在我的角色向上移动时绘制up.png等等。我已经浏览了一段时间,我无法找到解决问题的方法。如果有人可以将我链接到答案,或提供答案,那就太棒了!

这是我的代码:

package Michael;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

public class MyCanvas extends Canvas implements KeyListener
{
int myX = 100;
int myY = 100;

BufferedImage down;
{
    try
    {
        down = ImageIO.read(new File("Images/down.png"));
    }

    catch (IOException e)
    {
        System.out.println("Cannot find image file.");
    }
}

BufferedImage downrun;
{
    try
    {
        downrun = ImageIO.read(new File("Images/downrun.png"));
    }

    catch (IOException e)
    {
        System.out.println("Cannot find image file.");
    }
}

BufferedImage left;
{
    try
    {
        left = ImageIO.read(new File("Images/left.png"));
    }

    catch (IOException e)
    {
        System.out.println("Cannot find image file.");
    }
}

BufferedImage right;
{
    try
    {
        right = ImageIO.read(new File("Images/right.png"));
    }

    catch (IOException e)
    {
        System.out.println("Cannot find image file.");
    }
}

BufferedImage runleft;
{
    try
    {
        runleft = ImageIO.read(new File("Images/runleft.png"));
    }

    catch (IOException e)
    {
        System.out.println("Cannot find image file.");
    }
}

BufferedImage runright;
{
    try
    {
        runright = ImageIO.read(new File("Images/runright.png"));
    }

    catch (IOException e)
    {
        System.out.println("Cannot find image file.");
    }
}

BufferedImage swoosh;
{
    try
    {
        swoosh = ImageIO.read(new File("Images/swoosh.png"));
    }

    catch (IOException e)
    {
        System.out.println("Cannot find image file.");
    }
}

BufferedImage swordraise;
{
    try
    {
        swordraise = ImageIO.read(new File("Images/swordraise.png"));
    }

    catch (IOException e)
    {
        System.out.println("Cannot find image file.");
    }
}

BufferedImage up;
{
    try
    {
        up = ImageIO.read(new File("Images/up.png"));
    }

    catch (IOException e)
    {
        System.out.println("Cannot find image file.");
    }
}

BufferedImage uprun;
{
    try
    {
        left = ImageIO.read(new File("Images/uprun.png"));
    }

    catch (IOException e)
    {
        System.out.println("Cannot find image file.");
    }
}

public MyCanvas() // The MyCanvas constructor.
{
    this.setSize(600, 400);
    this.addKeyListener(this);
    this.setBackground(Color.WHITE);
    this.setFocusable(true);
}

public void paint(Graphics g)
{
    g.drawImage(down, myX, myY, 55, 55, null);
    g.drawImage(left, myX, myY, 55, 55, null);
    g.drawImage(right, myX, myY, 55, 55, null);
    g.drawImage(up, myX, myY, 55, 55, null);
} // I don't want to draw every image at the same time, I only want to draw 'up', when the charcter is moving up, etc.

public void moveIt(KeyEvent e)
{
    switch(e.getKeyCode())
    {
        case KeyEvent.VK_DOWN:
            myY += 10;
            break;
        case KeyEvent.VK_UP:
            myY -= 10;
            break;
        case KeyEvent.VK_LEFT:
            myX -= 10;
            break;
        case KeyEvent.VK_RIGHT:
            myX += 10;
            break;
        case KeyEvent.VK_SHIFT:

    }

    repaint();
}

@Override
public void keyPressed(KeyEvent e)
{
    moveIt(e);
}

@Override
public void keyTyped(KeyEvent e)
{
    // TODO Auto-generated method stub
}

@Override
public void keyReleased(KeyEvent e)
{
    // TODO Auto-generated method stub
}
}

非常感谢!

1 个答案:

答案 0 :(得分:1)

查看Motion Using the Keyboard中的<html> <head> <title>Print3</title> </head> <body onload="getData();"> <div id="reportData"> </div> </body> </html> 示例。此示例显示如何使用Motion With Key Bindings(这是更好的解决方案)而不是KeyListener来执行动画。

首先了解基本概念,因为您需要进行更改:

  1. 现有代码移动Key Bindings。您可能希望将其更改为Component

  2. 创建操作时,您当前只需指定JLabel (with an Icon)移动的x / y值。现在,您需要使用Component创建动作,这样您就可以更改每个方向更改时要绘制的图标。

  3. 需要修改x/y values AND an Icon方法以接受Icon,然后更改JLabel组件的Icon。