简体   繁体   English

Java:通过KeyListener使用不同的键移动第二个矩形

[英]Java: Moving second rectangle using different keys with KeyListener

I am making a little program where two rectangles drive around a race car track. 我正在编写一个小程序,其中两个矩形围绕赛车轨道行驶。 When I run the program everything goes as planned and I can move the black rectangle around the track using the arrow keys. 当我运行程序时,一切都按计划进行,我可以使用箭头键在轨道周围移动黑色矩形。 If I want to move the red rectangle using W,A,S,D, what should I do? 如果要使用W,A,S,D移动红色矩形,该怎么办?

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;

public class first extends JLabel implements ActionListener, KeyListener {

Timer t = new Timer(5, this);
double x = 0, y = 25, velx = 0, vely = 0;


public first() {
    t.start();
    addKeyListener(this);
    setFocusable(true);
    setFocusTraversalKeysEnabled(false);
}

public static void main(String[] args) {
    JLabel jl = new JLabel();
    JPanel jp = new JPanel();
    JFrame f = new JFrame();
    first m = new first();

    f.add(m);
    f.setVisible(true);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setSize(1650,1080);
    f.setExtendedState(JFrame.MAXIMIZED_BOTH);

    //The image below is a race car track I drew in Microsoft Paint
    jl.setIcon(new ImageIcon("C:\\Users\\Jack\\Pictures\\JavaImages\\racetrack2.png"));

    f.add(jp);
    jp.add(jl);

    f.validate();
}

public void paintComponent (Graphics g) {
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D) g;     
    g2.setColor(Color.RED);
    g2.fill(new Rectangle2D.Double(0, 80, 90, 50));

    g2.setColor(Color.BLACK);
    g2.fill(new Rectangle2D.Double(x, y, 90, 50));
}


public void actionPerformed(ActionEvent e) {
    repaint();
    x += velx;
    y += vely;
}

public void up() {
    vely = -3.5;
    velx = 0;
}

public void down() {
    vely = 3.5;
    velx = 0;
}

public void left() {
    velx = -4.5;
    vely = 0;
}

public void right() {
    velx = 4.5;
    vely = 0;
}

/*public void upStopped() {
    velx = 0;
    vely = 0;
}

public void downStopped() {
    velx = 0;
    vely = 0;
}

public void leftStopped() {
    velx = 0;
    vely = 0;
}

public void rightStopped() {
    velx = 0;
    vely = 0;
}*/

public void keyPressed(KeyEvent e) {
    int code = e.getKeyCode();
    if(code == KeyEvent.VK_UP) {
        up();
    }
    if(code == KeyEvent.VK_DOWN) {
        down();
    }
    if(code == KeyEvent.VK_LEFT) {
        left();
    }
    if(code == KeyEvent.VK_RIGHT) {
        right();
    }

    if(code == KeyEvent.VK_W) {

    }
    if(code == KeyEvent.VK_S) {

    }
    if(code == KeyEvent.VK_A) {

    }
    if(code == KeyEvent.VK_D) {

    } 
}

public void keyReleased(KeyEvent e) {
    /*int code2 = e.getKeyCode();
    if(code2 == KeyEvent.VK_UP) {
        upStopped();
    }

    if(code2 == KeyEvent.VK_DOWN) {
        downStopped();
    }

    if(code2 == KeyEvent.VK_LEFT) {
        leftStopped();
    }

    if(code2 == KeyEvent.VK_RIGHT) {
        rightStopped();
    }*/
}

public void keyTyped(KeyEvent e) {}

}

I can move the black rectangle around the track using the arrow keys. 我可以使用箭头键在轨道周围移动黑色矩形。 If I want to move the red rectangle using W,A,S,D, what should I do? 如果要使用W,A,S,D移动红色矩形,该怎么办?

  1. Use Key Bindings not a KeyListener. 使用Key Bindings而不是KeyListener。
  2. You also need to use a Timer to start the animation when a key is pressed and stop the animation when a key is released, since events can only be generated for a single key at a time. 您还需要使用计时器来在按下某个键时启动动画,并在释放一个键时停止动画,因为一次只能为单个键生成事件。

Check out Motion Using The Keyboard for more information about the above concepts. 有关使用上述概念的更多信息,请参见使用键盘进行运动 The keyboardAnimation code is a complete working example implementing both of the concepts. keyboardAnimation代码是实现两个概念的完整工作示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM