简体   繁体   English

使用箭头键移动矩形

[英]Move a rectangle using arrow keys

I am trying to develop a simple game where you can move a rectangle using the left and right arrow keys and to shoot using the spacebar. 我正在尝试开发一个简单的游戏,您可以使用向左和向右箭头键移动矩形并使用空格键进行射击。

I have added a KeyListener and others, but when I run it I have nothing as the output of key pressed or others. 我已经添加了一个KeyListener和其他程序,但是当我运行它时,我没有任何东西可以用作按键或其他程序的输出。

I have two classes: 我有两节课:

PaintDemo class: 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 class: 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) {

    }
}

The shooting method is not complete. 拍摄方法不完整。

Change repaint() to paintComponent here and add Graphics g = getGraphics(); 在这里将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;
    }
}    

Let me know if this works :) 让我知道这个是否奏效 :)



EDIT: 编辑:
Add this line 添加此行

this.setFocusable(true);

to PaintComponent constructor, it will make keypresses go through. 到PaintComponent构造函数,它将使按键通过。

You forgot to do two things, First one is your panel should be focusable, add this in your PaintComponent constructor this.setFocusable(true) and the second one is you have not break the switch case, so add the break statement for each case, then it should work. 您忘记做两件事,第一件事是您的面板应该是可聚焦的,将其添加到您的PaintComponent构造函数this.setFocusable(true) ,第二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;
}

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

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