Java,我的图像不会更新/移动

use*_*322 1 java swing image keylistener repaint

我是Java的新手,但是......在网上阅读了很多内容之后,我一直在努力开发这个游戏并开始使用,我正在使用一些图像.我相信,我正在努力更新他们的立场以证明当然的运动KeyListener.不幸的是,图像仍然在同一个地方并且拒绝移动D:

我确定我的一些代码过于复杂,但这里是:\

public class Game extends javax.swing.JPanel implements ActionListener, KeyListener{

    private Vladimir vlad;
    private Timer timer;

    public Game() {
        addKeyListener(this);
        setPreferredSize(new Dimension(1024,768));
        setDoubleBuffered(true);

        vlad = new Vladimir();
        timer = new Timer(15,this);
        timer.start();
    }

    public void actionPerformed (ActionEvent e){
        repaint();
    }

    private void toggleKey(int keyCode, boolean pressed){
        if (keyCode == 87){ // W
             vlad.move("UP", pressed);
        }

        if (keyCode == 83){ // S
            vlad.move("DOWN", pressed);
        }

        if (keyCode == 65) // A
        {
            vlad.move("LEFT", pressed);
        }

        if (keyCode == 68) // D
        {
            vlad.move("RIGHT", pressed);
        }
    }

    public void keyPressed(KeyEvent e)
    {
        toggleKey(e.getKeyCode(), true);
    }

    public void keyReleased(KeyEvent e)
    {
        toggleKey(e.getKeyCode(), false);
    }

    public void keyTyped(KeyEvent e){

    }

    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        Graphics g2d = (Graphics2D)g;
        g2d.drawImage(vlad.getGrassMap(),0,0,this);
        g2d.drawImage(vlad.getCharOne(),vlad.getX(),vlad.getY(),this);

        repaint();


      }
}
Run Code Online (Sandbox Code Playgroud)

然后..

public class Vladimir{

    private int x;
    private int y;
    private Image grassMapOne;
    private Image charOne;
    private String gMapLocate = "/phantasma/resources/GrassMap1.png";
    private String charOneLocate = "/phantasma/resources/moveright1.png";

    public Vladimir(){
        ImageIcon gMap1 = new ImageIcon(this.getClass().getResource(gMapLocate));
        ImageIcon char1 = new ImageIcon(this.getClass().getResource(charOneLocate));
        grassMapOne = gMap1.getImage();
        charOne = char1.getImage();
        x = 512;
        y = 350;
    }

    public void move(String direction, boolean keyHeld){
        if (direction == "UP"){
            y += 12;
        }

        if (direction == "DOWN"){
            y -= 12;
        }

        if (direction == "LEFT"){
            x -= 12;
        }

        if (direction == "RIGHT"){
            x += 12;
        }
    }

    public int getX(){
        return x;
    }

    public int getY(){
        return y;
    }

    public Image getGrassMap(){
        return grassMapOne;
    }

    public Image getCharOne(){
        return charOne;
    }
}
Run Code Online (Sandbox Code Playgroud)

Hov*_*els 5

首先你需要知道什么是错误才能解决它,所以你应该首先问自己,不是为什么图像没有移动,而是关键的听众甚至在听?如果你添加这样的println语句:

public void keyPressed(KeyEvent e) {
    System.out.println("in keyPressed. keyCode is: " + e.getKeyCode());
    toggleKey(e.getKeyCode(), true);
}
Run Code Online (Sandbox Code Playgroud)

你会发现它不是,它需要一个能够获得焦点且实际上具有焦点的组件.

您需要将JPanel可聚焦属性设置为true,然后acceptFocusInWindow()在渲染后将其设置为true .

更好的是 - 使用键绑定,因为它们在焦点问题上更加灵活,并且是更高级别的构造.事实上,它们是Swing本身使用的组件对按键的反应.

接下来,不要==用来比较Strings,因为这只检查两个String对象是否是同一个,你不关心的东西.相反,你要知道,如果一个String具有相同的字符以相同的顺序另一个String,为此您要使用Stringequals(...)方法或它的equalsIgnoreCase(...)方法.