使用箭头键

时间:2015-05-10 13:04:03

标签: java keylistener keyevent

我正在尝试开发一个简单的游戏,您可以使用左右箭头键移动矩形并使用空格键进行拍摄。

我添加了KeyListener和其他人,但是当我运行它时,我没有任何按键或其他按键的输出。

我有两个班级:

PaintDemo类:

package game;

import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class PaintDemo {

    public static void main(String[] args) {

        JFrame mainFrame = new JFrame("Game 1.1");
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainFrame.setBounds(10, 10, 400, 400);
        mainFrame.setLayout(new BorderLayout());

        JPanel mainPanel = new JPanel();
        mainPanel.setBackground(Color.WHITE);

        PaintComponent paintPanel = new PaintComponent();

        mainFrame.add(mainPanel, BorderLayout.PAGE_START);
        mainFrame.add(paintPanel, BorderLayout.CENTER);

        mainFrame.setVisible(true);

    }
}

PaintComponent类:

package game;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.geom.Rectangle2D;
import javax.swing.JPanel;


public class PaintComponent extends JPanel implements KeyListener {
    int dx = 200;
    int dy = 300;
    int my = 300;
    public Rectangle2D rec = new Rectangle2D.Double(dx , dy, 30, 10);

    public PaintComponent() {
        this.addKeyListener(this);
        this.setBackground(Color.white);
    }

    public void shoot(KeyEvent evt){
        if (evt.getKeyCode() == KeyEvent.VK_SPACE){
        my -= 7;
        repaint();
        }
    }

    public void moveRec(KeyEvent evt){
        switch(evt.getKeyCode()){
            case KeyEvent.VK_LEFT:
                System.out.println("test");
                dx -= 2;
                rec.setRect(dx, dy, 30, 10);
                repaint();
            case KeyEvent.VK_RIGHT:
                dx += 2;
                rec.setRect(dx, dy, 30, 10);
                repaint();
        }
    }

    @Override
    protected void paintComponent(Graphics grphcs){
        super.paintComponent(grphcs);
        Graphics2D gr = (Graphics2D) grphcs;
        gr.draw(rec);
    }


    @Override
    public void keyTyped(KeyEvent e) {
        System.out.println("2");
        shoot(e);
    }


    @Override
    public void keyPressed(KeyEvent e) {
        moveRec(e);

    }

    @Override
    public void keyReleased(KeyEvent e) {

    }
}

拍摄方法不完整。

2 个答案:

答案 0 :(得分:0)

在这里将repaint()更改为paintComponent并添加Graphics g = getGraphics();

public void moveRec(KeyEvent evt){
    switch(evt.getKeyCode()){
        case KeyEvent.VK_LEFT:
            System.out.println("test");
            dx -= 2;
            rec.setRect(dx, dy, 30, 10);
            Graphics g = getGraphics();
            paintComponent(g);
            break;
        case KeyEvent.VK_RIGHT:
            dx += 2;
            rec.setRect(dx, dy, 30, 10);
            Graphics g = getGraphics();
            paintComponent(g);
            break;
    }
}    

让我知道这是否有效:)



编辑:
添加此行

this.setFocusable(true);

到PaintComponent构造函数,它将使按键经过。

答案 1 :(得分:0)

您忘了做两件事,第一件事是您的面板应该是可调焦的,在PaintComponent构造函数this.setFocusable(true)中添加它,第二件是您没有break切换案例,为每个案例添加break语句,然后它应该有用。

switch (evt.getKeyCode()) {
case KeyEvent.VK_LEFT:
    System.out.println("test");
    dx -= 2;
    rec.setRect(dx, dy, 30, 10);
    repaint();
    break;
case KeyEvent.VK_RIGHT:
    dx += 2;
    rec.setRect(dx, dy, 30, 10);
    repaint();
    break;
}