如何使用 KeyListener 在 JFrame 中移动一个矩形?
How to move a rectangle in a JFrame using KeyListener?
我正在尝试使用 KeyListener 在 JFrame 中移动我创建的矩形(实际上,我正在更改我创建的 X 和 Y 变量)。我想声明我不希望使用键绑定。我想使用 KeyListener。话虽这么说,我在这里似乎找不到我的逻辑有什么问题。
框架(主要)class:
import java.awt.Color;
import javax.swing.JFrame;
@SuppressWarnings ("serial")
public class GameFrame extends JFrame
{
public static void main (String[] args)
{
JFrame frame = new JFrame ("Java Game");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.setSize (1000, 600);
frame.getContentPane().setBackground (Color.WHITE);
frame.getContentPane().add (new GamePaint());
frame.addKeyListener (new GameController());
frame.setVisible (true);
}
}
绘画class:
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JComponent;
@SuppressWarnings ("serial")
public class GamePaint extends JComponent
{
public int x;
public int y;
public void paintComponent (Graphics graphics)
{
super.paintComponent (graphics);
graphics.setColor (Color.BLACK);
graphics.fillRect (x, y, 100, 100);
}
}
控制器class:
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class GameController implements KeyListener
{
GamePaint paint = new GamePaint();
public void keyPressed (KeyEvent event)
{
if (event.getKeyCode() == KeyEvent.VK_LEFT)
{
paint.x--;
paint.repaint();
}
else if (event.getKeyCode() == KeyEvent.VK_RIGHT)
{
paint.x++;
paint.repaint();
}
}
public void keyReleased (KeyEvent event) {}
public void keyTyped (KeyEvent event) {}
}
您正在创建 GamePaint
...
的多个实例
frame.getContentPane().add (new GamePaint());
和
public class GameController implements KeyListener
{
GamePaint paint = new GamePaint();
因此,当您 GameController
控制器尝试更改矩形的 x
/y
位置时,它不会与实际处于打开状态的 GamePaint
实例交互屏幕...
相反,创建 GamePaint
的单个实例,将其添加到框架中并传递对 GameController
的引用
GamePaint gp = new GamePaint();
frame.getContentPane().add (gp);
frame.addKeyListener (new GameController(gp));
并更新 GameController
...
public class GameController implements KeyListener
{
private GamePaint paint;
public GameController(GamePaint paint) {
this.paint = paint;
但是这种方法很奇怪...GamePaint
不是应该接收键盘输入而不是框架吗??
相反,请考虑使用键绑定 API,它解决了与 KeyListener
相关的许多其他问题。有关详细信息,请参阅 How to Use Key Bindings
而不是控制器直接修改 x
/y
属性,它应该在 GamePaint
class 上调用一个方法(类似于 moveLeft
、moveRight
等等...),这样 GamePaint
就可以决定这意味着什么...
但是,就我个人而言,我会在其中注入一个模型,该模型控制实体的位置,GamePaint
简单地绘制并由控制器控制...但这就是我...
我正在尝试使用 KeyListener 在 JFrame 中移动我创建的矩形(实际上,我正在更改我创建的 X 和 Y 变量)。我想声明我不希望使用键绑定。我想使用 KeyListener。话虽这么说,我在这里似乎找不到我的逻辑有什么问题。
框架(主要)class:
import java.awt.Color;
import javax.swing.JFrame;
@SuppressWarnings ("serial")
public class GameFrame extends JFrame
{
public static void main (String[] args)
{
JFrame frame = new JFrame ("Java Game");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.setSize (1000, 600);
frame.getContentPane().setBackground (Color.WHITE);
frame.getContentPane().add (new GamePaint());
frame.addKeyListener (new GameController());
frame.setVisible (true);
}
}
绘画class:
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JComponent;
@SuppressWarnings ("serial")
public class GamePaint extends JComponent
{
public int x;
public int y;
public void paintComponent (Graphics graphics)
{
super.paintComponent (graphics);
graphics.setColor (Color.BLACK);
graphics.fillRect (x, y, 100, 100);
}
}
控制器class:
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class GameController implements KeyListener
{
GamePaint paint = new GamePaint();
public void keyPressed (KeyEvent event)
{
if (event.getKeyCode() == KeyEvent.VK_LEFT)
{
paint.x--;
paint.repaint();
}
else if (event.getKeyCode() == KeyEvent.VK_RIGHT)
{
paint.x++;
paint.repaint();
}
}
public void keyReleased (KeyEvent event) {}
public void keyTyped (KeyEvent event) {}
}
您正在创建 GamePaint
...
frame.getContentPane().add (new GamePaint());
和
public class GameController implements KeyListener
{
GamePaint paint = new GamePaint();
因此,当您 GameController
控制器尝试更改矩形的 x
/y
位置时,它不会与实际处于打开状态的 GamePaint
实例交互屏幕...
相反,创建 GamePaint
的单个实例,将其添加到框架中并传递对 GameController
GamePaint gp = new GamePaint();
frame.getContentPane().add (gp);
frame.addKeyListener (new GameController(gp));
并更新 GameController
...
public class GameController implements KeyListener
{
private GamePaint paint;
public GameController(GamePaint paint) {
this.paint = paint;
但是这种方法很奇怪...GamePaint
不是应该接收键盘输入而不是框架吗??
相反,请考虑使用键绑定 API,它解决了与 KeyListener
相关的许多其他问题。有关详细信息,请参阅 How to Use Key Bindings
而不是控制器直接修改 x
/y
属性,它应该在 GamePaint
class 上调用一个方法(类似于 moveLeft
、moveRight
等等...),这样 GamePaint
就可以决定这意味着什么...
但是,就我个人而言,我会在其中注入一个模型,该模型控制实体的位置,GamePaint
简单地绘制并由控制器控制...但这就是我...