gpt4 book ai didi

java - 在我的代码中添加一个计时器

转载 作者:太空宇宙 更新时间:2023-11-04 13:32:31 25 4
gpt4 key购买 nike

我已经搜索并尝试自己实现一个计时器,但我无法弄清楚我的生活。所以我想做的是每五秒我希望随机圆圈改变到一个新的随机位置。看起来很简单,但我无法让它工作。

public class BounceThread implements Runnable {
JFrame frame;
int myX = 500 / 2;
int myY = 500 / 2;;

int X;
int Y;

Canvas canvas;
BufferStrategy bufferStrategy;
BufferStrategy bufferStrategy2;
boolean running = true;
Random rand = new Random();
Random rand2 = new Random();

int n = rand.nextInt(450) + 1;
int n2 = rand.nextInt(450) + 1;

public BounceThread() {
frame = new JFrame("Basic Game");
JPanel panel = (JPanel) frame.getContentPane();
panel.setPreferredSize(new Dimension(500, 500));
panel.setLayout(null);
canvas = new Canvas();
canvas.setBounds(0, 0, 500, 500);
canvas.setIgnoreRepaint(true);
panel.add(canvas);
canvas.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent evt) {
moveIt(evt);
}
});
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setResizable(false);
frame.setVisible(true);
canvas.createBufferStrategy(2);
bufferStrategy = canvas.getBufferStrategy();
canvas.requestFocus();
}

public void run() {
while (running = true) {
Paint();
if ((myX == n || myX == n) && (myY == n2 || myY == n2)) {

n = rand.nextInt(450) + 1;
n2 = rand.nextInt(450) + 1;
n2 = 10 * (Math.round(n / 10));
n = 10 * (Math.round(n / 10));
Paint();

}
}
}

public static void main(String[] args) {
BounceThread ex = new BounceThread();
new Thread(ex).start();
}

public void Paint() {

Graphics2D g = (Graphics2D) bufferStrategy.getDrawGraphics();
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g.clearRect(0, 0, 500, 500);
Graphics2D e = (Graphics2D) bufferStrategy.getDrawGraphics();
e.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
e.clearRect(0, 0, 500, 500);

Paint(g, e);

bufferStrategy.show();
}

protected void Paint(Graphics2D g, Graphics2D e) {
n2 = 10 * (Math.round(n / 10));
n = 10 * (Math.round(n / 10));
g.fillOval(myX, myY, 30, 30);

g.setColor(Color.red);

e.fillOval(n, n2, 30, 30);

e.setBackground(Color.white);
}

public void moveIt(KeyEvent evt) {
switch (evt.getKeyCode()) {
case KeyEvent.VK_DOWN:
if (myY >= 450) {
break;
} else {
myY += 10;
break;
}
case KeyEvent.VK_UP:
if (myY <= 30) {
break;
} else {
myY -= 10;
break;
}
case KeyEvent.VK_LEFT:
if (myX <= 30) {
break;
} else {
myX -= 10;
break;
}
case KeyEvent.VK_RIGHT:
if (myX >= 450) {
break;
} else {
myX += 10;
break;
}
}
}
}

最佳答案

我将从一个组件(例如 JComponent )进行扩展,并让 JFC/Swing 尽可能多地进行提升。请不要调用 canvas.setIgnoreRepaint(true); 并记住在每次移动后调用 repaint();。接下来,您需要一个用于 Timer 事件的 ActionListener。您的 switch 逻辑可以在 if-else 逻辑之外用 break 来表达。类似的东西

public class BounceThread extends JComponent implements ActionListener {
JFrame frame;
int myX = 500 / 2;
int myY = 500 / 2;;

int X;
int Y;

Canvas canvas;
BufferStrategy bufferStrategy;
boolean running = true;
Random rand = new Random();

int n = rand.nextInt(450) + 1;
int n2 = rand.nextInt(450) + 1;

public BounceThread() {
frame = new JFrame("Basic Game");
JPanel panel = (JPanel) frame.getContentPane();
panel.setPreferredSize(new Dimension(500, 500));
panel.setLayout(null);
canvas = new Canvas();
canvas.setBounds(0, 0, 500, 500);
// canvas.setIgnoreRepaint(true);
panel.add(canvas);
canvas.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent evt) {
moveIt(evt);
repaint();
}
});
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setResizable(false);
frame.setVisible(true);
canvas.createBufferStrategy(2);
bufferStrategy = canvas.getBufferStrategy();
canvas.requestFocus();
}

public static void main(String[] args) {
BounceThread ex = new BounceThread();
int delay = 5000; // milliseconds
new Timer(delay, ex).start();
}

@Override
public void repaint() {
Graphics2D g = (Graphics2D) bufferStrategy.getDrawGraphics();
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
Graphics2D e = (Graphics2D) bufferStrategy.getDrawGraphics();
e.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g.clearRect(0, 0, 500, 500);
e.clearRect(0, 0, 500, 500);
Paint(g, e);
bufferStrategy.show();
super.repaint();
}

protected void Paint(Graphics2D g, Graphics2D e) {
g.fillOval(myX, myY, 30, 30);
g.setColor(Color.red);
e.fillOval(n, n2, 30, 30);
e.setBackground(Color.white);
}

public void moveIt(KeyEvent evt) {
switch (evt.getKeyCode()) {
case KeyEvent.VK_DOWN:
if (myY < 450) {
myY += 10;
}
break;
case KeyEvent.VK_UP:
if (myY > 30) {
myY -= 10;
}
break;
case KeyEvent.VK_LEFT:
if (myX > 30) {
myX -= 10;
}
break;
case KeyEvent.VK_RIGHT:
if (myX < 450) {
myX += 10;
}
break;
}
}

@Override
public void actionPerformed(ActionEvent e) {
n = rand.nextInt(450) + 1;
n2 = rand.nextInt(450) + 1;
repaint();
}
}

关于java - 在我的代码中添加一个计时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32028545/

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