使用箭头键移动矩形
Move a rectangle using arrow keys
我正在尝试开发一个简单的游戏,您可以使用左右箭头键移动矩形并使用空格键射击。
我添加了 KeyListener
和其他,但是当我 运行 它时,我没有任何按键或其他输出。
我有两个 classes:
PaintDemo class:
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);
}
}
PaintComponentclass:
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) {
}
}
拍摄方法不完整
在此处将 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 构造函数,它将使按键通过。
你忘了做两件事,第一是你的面板应该是可聚焦的,在你的 PaintComponent
构造函数中添加它 this.setFocusable(true)
第二是你没有 break
switch case,所以为每个 case 添加 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;
}
我正在尝试开发一个简单的游戏,您可以使用左右箭头键移动矩形并使用空格键射击。
我添加了 KeyListener
和其他,但是当我 运行 它时,我没有任何按键或其他输出。
我有两个 classes:
PaintDemo class:
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);
}
}
PaintComponentclass:
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) {
}
}
拍摄方法不完整
在此处将 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 构造函数,它将使按键通过。
你忘了做两件事,第一是你的面板应该是可聚焦的,在你的 PaintComponent
构造函数中添加它 this.setFocusable(true)
第二是你没有 break
switch case,所以为每个 case 添加 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;
}