gpt4 book ai didi

java - Java 中的关键事件 - 初学者帮助

转载 作者:行者123 更新时间:2023-12-01 19:22:01 27 4
gpt4 key购买 nike

因此,我在学习 Java 的同时一直在编写一个简单的程序,该程序会显示一个小矩形屏幕,上面有一个正在移动的圆圈。背景和圆圈也会改变颜色和速​​度。

现在我正在尝试添加KeyEvents,这样当用户键入字符时,圆圈就会改变方向。我已经尝试掌握 KeyEvent 功能几个小时了,但我陷入了困境。

如何更改以下代码以使程序响应键盘输入:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import java.awt.event.KeyEvent;
import javax.swing.*;
import javax.swing.JFrame;

public class MovingCircle3 extends JFrame implements ActionListener {

Ellipse2D.Double myEllipse;
Rectangle2D.Double backgroundRectangle;
private int paintTally = 0; // Counts # of repaints.
private int timerSpeed = 500; // Holds speed of timer. Initially: 500ms.
private Timer timerOjbect;
private char shapeMoveInput = 'd';

public MovingCircle3() {
myEllipse = new Ellipse2D.Double(30, 30, 20, 20); // Ellipse starting point
backgroundRectangle = new Rectangle2D.Double(0, 0, 400, 300); // Background.
this.timerOjbect = new Timer(500, this);
timerOjbect.start(); //Creates and starts timer.
}

public static void main(String[] args) {
System.out.print("Game Controls: \n l = Move left. \n r = Move right. \n u = Move up. \n d = Move down. \n ENTER COMMAND: "); // Game controls.

MovingCircle3 b = new MovingCircle3();

b.setVisible(true);
b.setSize(400, 300);
b.setVisible(true);
b.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public void keyPressed(KeyEvent e) {
try {
shapeMoveInput = e.getKeyChar();
} catch (Exception ex) {
shapeMoveInput = 'd';
}
}

public void actionPerformed(ActionEvent ae) {
//This will be called by the Timer
if (shapeMoveInput == 'l') {
myEllipse.setFrame(myEllipse.getX() - 1, myEllipse.getY(), myEllipse.getWidth(), myEllipse.getHeight()); //Move 1 x-pixel left
} else if (shapeMoveInput == 'r') {
myEllipse.setFrame(myEllipse.getX() + 1, myEllipse.getY(), myEllipse.getWidth(), myEllipse.getHeight()); //Move 1 x-pixel right
} else if (shapeMoveInput == 'u') {
myEllipse.setFrame(myEllipse.getX(), myEllipse.getY() - 1, myEllipse.getWidth(), myEllipse.getHeight()); //Move 1 y-pixel up
} else if (shapeMoveInput == 'd') {
myEllipse.setFrame(myEllipse.getX(), myEllipse.getY() + 1, myEllipse.getWidth(), myEllipse.getHeight()); //Move 1 y-pixel
}
repaint();
}

public void paint(Graphics g) {
paintTally++; // Increments by one for every repaint().
if (paintTally % 25 == 0) { // Take modulus 25 of paintTally to execute every 25 paints.
timerSpeed = (int) (timerSpeed / 2);
timerOjbect.setDelay(timerSpeed); // Divide speed by 2 and see to timer length.
}
int isPaintTen = (int) (paintTally / 10); // Divide current count by 10.
Graphics2D g2 = (Graphics2D) g;
if ((isPaintTen % 2) == 0) { // Take modulus to set if #/10 is odd or even.
g2.setColor(Color.YELLOW);
g2.fill(backgroundRectangle);
g2.setColor(Color.RED);
g2.draw(myEllipse);

} else if ((isPaintTen % 2) == 1) {
g2.setColor(Color.RED);
g2.fill(backgroundRectangle);
g2.setColor(Color.YELLOW);
g2.draw(myEllipse);
}
}
}

最佳答案

1)你永远不应该(除非你真的知道你在做什么)重写JFrame的paint方法。自定义绘制是通过重写 JPanel 的 PaintComponent(...) 方法来完成的。然后将自定义面板添加到 JFrame。您在原始帖子的评论中给出了 Swing 教程的链接。请阅读“自定义绘画”部分以获取更多信息和工作示例。

2) 然后您需要将 KeyListener 添加到面板。但是,KeyEvent 仅传递给具有焦点的组件,因此您需要使用面板上的 setFocusable(...) 方法使面板可聚焦。

但是,KeyListener 并不是解决此问题的正确方法。您应该使用比使用 KeyListener 灵活得多的键绑定(bind)。 Swing 教程中再次有一个关于“如何使用键绑定(bind)”的部分。

关于java - Java 中的关键事件 - 初学者帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3714022/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com