gpt4 book ai didi

java - 形状在绘图面板java上不移动

转载 作者:太空宇宙 更新时间:2023-11-04 15:11:45 27 4
gpt4 key购买 nike

我正在尝试用 bat (桨)和球制作一个简单的java游戏。到目前为止,我已将这两个对象绘制到面板上,但是我无法让它们移动。我为球棒添加了关键事件,为球添加了 move() 方法。以下是我的所有类(class)。

游戏类:

public class Game extends JFrame {

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Game();
}});
}

MyDrawingPanel myDrawingPanel = new MyDrawingPanel(this);
MyUIPanel myUIPanel = new MyUIPanel(this);


public Game()
{
setSize(1160,660); // you may change frame and panel sizes
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Container cp = getContentPane();
cp.setLayout(new FlowLayout());
cp.add(myDrawingPanel);
cp.add(myUIPanel);

setVisible(true);
}
}

MyDrawingPanel 类:

class MyDrawingPanel extends JPanel {

Game game;

Ball ball = new Ball(this);
Bat bat = new Bat(this);

public MyDrawingPanel(Game game)
{
this.game=game;
setPreferredSize(new Dimension(800,600));
setBackground(Color.RED);
requestFocus();
}

public void paint(Graphics g)
{
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
ball.paint(g2d);

Graphics2D gBat = (Graphics2D) g;
bat.paint(gBat);
}
}

球类:

public class Ball {

int x = 0;
int y = 0;
int xa = 1;
int ya = 1;
private MyDrawingPanel myDrawingPanel;

public Ball(MyDrawingPanel myDrawingPanel) {
this.myDrawingPanel = myDrawingPanel;
}

public void move() {
if (x + xa < 0)
xa = 1;
if (x + xa > myDrawingPanel.getWidth() - 30)
xa = -1;
if (y + ya < 0)
ya = 1;
if (y + ya > myDrawingPanel.getHeight() - 30)
ya = -1;

x = x + xa;
y = y + ya;
}

public void paint(Graphics2D g) {
g.fillOval(x, y, 30, 30);
}
}

bat 类:

public class Bat{

int x = 0;
int xa = 0;
private MyDrawingPanel myDrawingPanel;

public Bat(MyDrawingPanel myDrawingPanel)
{
this.myDrawingPanel = myDrawingPanel;
}

public void move(){
if(x + xa > 0 && x + xa <myDrawingPanel.getWidth()-60 )
x = x + xa;
}

public void paint(Graphics2D g)
{
g.setColor(Color.BLUE);
g.fillRoundRect(x, 500, 100, 20, 10, 10);
}

public void keyPressed(KeyEvent e)
{
if(e.getKeyCode() == KeyEvent.VK_LEFT)
xa = -1;
if(e.getKeyCode() == KeyEvent.VK_RIGHT)
xa = 1;
}

public void keyReleased(KeyEvent e)
{
xa = 0;
}
}

最佳答案

如果没有 KeyListener 实现,keyPressedkeyReleased 等方法不会执行任何操作。所以目前来说,你的方法是没有用的。您应该让 DrawingPanel 类实现 KeyListener,如下所示

public class DrawingPanel extends JPanel implements KeyListener {
...
}

方法 keyPressedkeyReleased 应该位于该类中。您还需要使用 setter 方法来更新 Bat 类中的变量,例如 xxa ,是决定 bat 运动的变量。

因此,在应该位于 DrawingPanel 类中的 keyPressed 中,它可能看起来像这样

public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
bat.setXa(bat.getXa() - 1);
repaint();
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
bat.setXa(1);
repaint();
}
}

注意我如何调用repaint()。这就是您移动某些内容后需要执行的操作。

这应该让您朝着正确的方向开始

<小时/>

编辑

忘记上面答案的大部分内容。

尽管我建议使用键绑定(bind),而不是使用KeyListener。使用KeyListener时您可能会遇到焦点问题。我为 DrawingPanel 实现了键绑定(bind),并且工作正常。它会给你一些可以使用的想法。

class MyDrawingPanel extends JPanel {

Game game;

Ball ball = new Ball(this);
Bat bat = new Bat(this);

public MyDrawingPanel(Game game) {
this.game = game;
setPreferredSize(new Dimension(800, 600));
setBackground(Color.RED);
requestFocus();

Action rightAction = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
bat.x += 10;
repaint();
}
};
Action leftAction = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
bat.x -= 10;
repaint();
}
};

InputMap inputMap = getInputMap(JPanel.WHEN_IN_FOCUSED_WINDOW);
ActionMap actionMap = getActionMap();

inputMap.put(KeyStroke.getKeyStroke("RIGHT"), "rightAction");
actionMap.put("rightAction", rightAction);
inputMap.put(KeyStroke.getKeyStroke("LEFT"), "leftAction");
actionMap.put("leftAction", leftAction);
}

public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
ball.paint(g2d);

Graphics2D gBat = (Graphics2D) g;
bat.paint(gBat);
}
}
<小时/>

参见How to use key bindings

关于java - 形状在绘图面板java上不移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21258058/

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