Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 按键时尝试更改图像失败_Java_Animation_Keyevent - Fatal编程技术网

Java 按键时尝试更改图像失败

Java 按键时尝试更改图像失败,java,animation,keyevent,Java,Animation,Keyevent,我试着让它成为这样,当我按下右键时,会弹出一张新的图片,使它看起来像我的角色在行走,但不太确定如何做到这一点。。。这是我的密码: import java.awt.*; public class Dude { int x, dx, y; Image still; public Dude() { ImageIcon i = new ImageIcon("Ken3.png"); ImageIcon ii = new Imag

我试着让它成为这样,当我按下右键时,会弹出一张新的图片,使它看起来像我的角色在行走,但不太确定如何做到这一点。。。这是我的密码:

import java.awt.*;


public class Dude {
    int x, dx, y;
    Image still;

    public Dude() {
            ImageIcon i = new ImageIcon("Ken3.png");
            ImageIcon ii = new ImageIcon("KenTurn1.png");
            still = i.getImage();
            x = 50;
            y = 785;
    }


    public void move() {
            x = x + dx;
    }

    public int getX() {
            return x;
    }

    public int getY() {
            return y;
    }

    public Image getImage() {
            return still;
    }

    public void keyPressed(KeyEvent e) {
            int key = e.getKeyCode();
            if (key == KeyEvent.VK_A)
                    dx = -2;

            if (key == KeyEvent.VK_D) 
                    dx = 2;

            if (key == KeyEvent.VK_SPACE)
                dx = 5;

    }

    public void keyReleased(KeyEvent e) {
            int key = e.getKeyCode();

            if (key == KeyEvent.VK_A)
                    dx = 0;

            if (key == KeyEvent.VK_D)
                    dx = 0;

任何帮助都会很棒,谢谢

像这样的,我评论过。祝你好运

public class Dude {

    private int walkingIndex;
    private Image walkingFrames;
    private Image still;

    private String state;

    public Dude() {
        // say were starting off standing
        state = "standing";
        // init our still frame
        still = (new ImageIcon("Ken3.png")).getImage();

        // set our walking frame to 0 to start
        walkingIndex = 0;
        walkingFrames = new Image[3]; // or however many images you have in your walking animation
        // go through each frame and initialize the image to KenWalking{index}.png
        for(int i=0;i<walkingFrames.length;i++) {
            walkingFrames[i] = (new ImageIcon("KenWalking"+i+".png")).getImage();
        }
    }

    public void move() {
        x+=dx;
        // add 1 to the walking index and make sure its not greater than our number of walking frames, % means get the remainder of
        // so 12 % 10 = 2, and 8 % 7 = 1
        walkingIndex = (walkingIndex+1)%walkingFrames.length;
    }

    public Image getImage() {
        // if our state is walking then give out a walking frame
        if(state.equals("walking")) {
            return walkingFrames[walkingIndex];
        } else {
            // otherwise we can assume were standing, so show that image instead
            return still;
        }
    }

    public void keyPressed(KeyEvent e) {
        ... same stuff
        state = "walking";
    }

    public void keyReleased(KeyEvent e) {
        ... same stuff
        state = "standing";
    }
}
public class Dude{
私人智能漫游索引;
私有图像漫游帧;
私人影像静止;
私有字符串状态;
公子哥们(){
//比如说,我们开始站着
state=“standing”;
//在我们的静止框架内
still=(新的ImageIcon(“Ken3.png”)).getImage();
//将行走帧设置为0以开始
行走指数=0;
walkingFrames=新图像[3];//或在行走动画中有多少图像
//遍历每一帧并将图像初始化为{index}.png

对于(int i=0;i如果我正确理解了这个问题,那么您正在寻找一个keyListener。在这种情况下,您可以做两件事。添加KeyBinding或添加keyListener。 请记住,我还没有在你的模板上测试过代码,它在AWT框架中对我很有效。希望能有所帮助

首先实现EventListener到Dude

public class Dude implements KeyListener{ ...
要使KeyListener正常工作,必须有一个可聚焦组件

    component.addKeyListener(this);
    component.setFocusable(true);
    component.setFocusTraversalKeysEnabled(false);
下面的方法将通过KeyListener实现

@Override
public void keyPressed(KeyEvent e) {
    int x = e.getKeyCode();

    switch(x){
        case KeyEvent.VK_UP:
            // Do Your stuff
            break;
        case KeyEvent.VK_DOWN:
            // Do Your stuff
            break;
        case KeyEvent.VK_RIGHT:
            // Do Your stuff
            break;
        case KeyEvent.VK_LEFT:
            // Do Your stuff
            break;
    }
}

添加注释的简单方法:创建一个图像数组,然后当您按下一个键时,它会将一个图像添加到数组索引中,从而更改图片。@ns47741,我是java新手,还不知道怎么做。好吧,我完全复制了它,但是“行走”、“站立”和长度(在这个walkingIndex=(walkingIndex+1)%walkingFrames.length;)其中包含错误。错误在“站立”和“行走”中显示“无效字符常量”…长度显示“长度无法解析或不是字段”