gpt4 book ai didi

java - JAVA如何添加一个按钮来卡住和恢复弹跳球

转载 作者:行者123 更新时间:2023-12-01 13:06:48 25 4
gpt4 key购买 nike

我想在以下代码中添加 2 个按钮,让我在球在窗口上弹跳时“卡住”和“开始”移动。过去一个小时我一直在尝试这样做,但我无法弄清楚。我做了一些工作,但大部分都是废话,如果有人想要,请让我知道发布它(避免它是为了不扩展我的编码)。任何人都可以帮我解决这个问题吗?接受任何建议。谢谢

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

public class BallMoves extends JPanel implements Runnable {
Color color = Color.red;
int dia = 30;
long delay = 40;
private int x = 1;
private int y = 1;
private int dx = 3;
private int dy = 7;

protected void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g.setColor(color);
g.fillOval(x,y,30,30); // adds color to circle
g.setColor(Color.red);
g2.drawOval(x,y,30,30); // draws circle
}



public void run() {
while(isVisible()) {
try {
Thread.sleep(delay);
} catch(InterruptedException e) {
System.out.println("interrupted");
}
move();
repaint();
}
}

public void move() {
if(x + dx < 0 || x + dia + dx > getWidth()) {
dx *= -1;
color = getColor();
}
if(y + dy < 0 || y + dia + dy > getHeight()) {
dy *= -1;
color = getColor();
}
x += dx;
y += dy;
}

private Color getColor() {
int rval = (int)Math.floor(255);
int gval = (int)Math.floor(0);
int bval = (int)Math.floor(0);
return new Color(rval, gval, bval);
}

private void start() {
while(!isVisible()) {
try {
Thread.sleep(25);
} catch(InterruptedException e) {
System.exit(1);
}
}
Thread thread = new Thread(this);
thread.setPriority(Thread.NORM_PRIORITY);
thread.start();
}

public static void main(String[] args) {
BallMoves test = new BallMoves();
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(test);
f.setSize(400,400);
f.setLocation(200,200);
f.setVisible(true);
test.start();
}
}

更新版本

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

public class BallMoves extends JPanel implements Runnable {
Color color = Color.red;
int dia = 30;
long delay = 40;
private int x = 1;
private int y = 1;
private int dx = 3;
private int dy = 7;
private boolean isRunning;

protected void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g.setColor(color);
g.fillOval(x,y,30,30); // adds color to circle
g.setColor(Color.red);
g2.drawOval(x,y,30,30); // draws circle
}

public void run() {
while(isVisible()) {
try {
Thread.sleep(delay);
} catch(InterruptedException e) {
System.out.println("interrupted");
}
move();
repaint();
}
}

public void move() {
if (isRunning) {
if(x + dx < 0 || x + dia + dx > getWidth()) {
dx *= -1;
color = getColor();
}
if(y + dy < 0 || y + dia + dy > getHeight()) {
dy *= -1;
color = getColor();
}
x += dx;
y += dy;
}
}

private Color getColor() {
int rval = (int)Math.floor(255);
int gval = (int)Math.floor(0);
int bval = (int)Math.floor(0);
return new Color(rval, gval, bval);
}

private void start() {
while(!isVisible()) {
try {
Thread.sleep(25);
} catch(InterruptedException e) {
System.exit(1);
}
}
Thread thread = new Thread(this);
thread.setPriority(Thread.NORM_PRIORITY);
thread.start();
}

public static void main(String[] args) {

EventQueue.invokeLater(new Runnable() {

@Override
public void run() {
final BallMoves test = new BallMoves();
JFrame f = new JFrame();

JButton start = new JButton("start");
start.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent arg0) {
test.isRunning = true;
}
});

JButton stop = new JButton("stop");
stop.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent arg0) {
test.isRunning = false;
}
});

JPanel panel = new JPanel();
panel.add(start);
panel.add(stop);

f.add(panel, java.awt.BorderLayout.NORTH);
f.getContentPane().add(test);

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(new Dimension(400, 400));
f.setLocationRelativeTo(null);
f.setVisible(true);
test.start();
}

});



}
}

最佳答案

创建一个标志并在单击按钮时将其切换。

private volatile boolean isRunning;

public void move() {
if (isRunning) {
// your existing code
...
}
}

点击开始按钮

 isRunning = true;

点击停止按钮

 isRunning = false;

关于java - JAVA如何添加一个按钮来卡住和恢复弹跳球,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23204679/

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