gpt4 book ai didi

java - 为什么这段代码不绘制和移动矩形?

转载 作者:行者123 更新时间:2023-12-02 06:18:04 24 4
gpt4 key购买 nike

我编写了一个关于简单游戏的代码,但它不起作用。我的意思是矩形没有出现,因此我无法移动它。我也没有从 ide 收到任何错误消息。我花了好几次试图解决这个问题,但毫无结果。非常感谢任何提示或建议。谢谢。

我写的代码:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;



public class DataGame extends JFrame {
// Create text fields for balls are left and time elapsed
private JTextField jtfBallsRemain, jtfTimeElapsed;
// Button "New game"
private JButton jbtStartNewGame = new JButton("New game");

private Game canvas = new Game();

// the main method
public static void main(String[] args) {
JFrame frame = new DataGame();
frame.setTitle("Test data game ");
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(470, 300);
frame.setVisible(true);
}

public DataGame() {
// Use panel1 to hold text fields, labels and the button
JPanel panel = new JPanel();

// Add panel to south
add(panel, BorderLayout.SOUTH);
panel.add(new JLabel("Balls remain"));
panel.add(jtfBallsRemain = new JTextField(4));
panel.add(new JLabel("Time elapsed"));
panel.add(jtfTimeElapsed = new JTextField(4));
panel.add(jbtStartNewGame);

jtfBallsRemain.setEditable(false);
jtfTimeElapsed.setEditable(false);

add(canvas, BorderLayout.CENTER); // Add canvas to centre

// register listener
jbtStartNewGame.addActionListener(new StartNewGame());

canvas.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON1)
canvas.moveUpRect();
else if (e.getButton() == MouseEvent.BUTTON3)
canvas.moveDownRect();
}
});

}

class StartNewGame implements ActionListener { // inner class
@Override
public void actionPerformed(ActionEvent e) {
canvas.StartNewGame();
}
}

}

class Game extends JPanel {
//The ball
private int radius = 10;
private int x;
private int y;
private int dx = 5;
private int dy = 5;

//The rectangle
private int w = 15;
private int h = 80;
private int dy1 = 5;
private int y1 = ((getHeight() / 2) - (h / 2));
private int x1 = (getWidth() - 50);

private Timer timer = new Timer(20, new TimerListener());

public void moveDownRect() {
y1 += dy1;
repaint();

}

public void moveUpRect() {
y1 -= dy1;
repaint();

}

public void StartNewGame() {

timer.start();

}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.GREEN);
g.fillOval(x, y, 2 * radius, 2 * radius);
g.setColor(Color.BLUE);
g.fillRect(x1, y1, w, h);
}

class TimerListener implements ActionListener { /*
* make the TimerListener an
* inner class of Ball,
* which allows it to access
* the variable and methods
* of Ball
*/
@Override
public void actionPerformed(ActionEvent e) {

if (x < 0 || x + (2 * radius) > getWidth()) {

dx *= -1;

}

if (y < 0 || y + (2 * radius) > getHeight()) {

dy *= -1;
}

x += dx;
y += dy;

repaint();
}
}

}

最佳答案

x1变量被求值时;

private int x1 = (getWidth() - 50);

getWidth 将返回 0,这意味着 x1 将等于 -50,将其渲染到屏幕之外

更好的解决方案可能是在主窗口可见后进行后初始化

关于java - 为什么这段代码不绘制和移动矩形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21272230/

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