gpt4 book ai didi

java - 暂停/恢复 java 小程序

转载 作者:行者123 更新时间:2023-12-01 14:41:44 24 4
gpt4 key购买 nike

左侧和底部的两个球在某个坐标处相遇时相互碰撞。我已经按照我在互联网上搜索的内容进行了操作,效果非常好,但我需要一个开始、暂停和恢复按钮。看看我完成了什么:

import java.applet.Applet;
import java.awt.Button;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class train extends Applet implements Runnable,ActionListener {
private volatile boolean runs = true;
private Image i;
private Graphics doubleG;
Ball b, b2;
Button x,y,z;
Thread thread = new Thread(this);
@Override
public void init(){
setSize(800, 600);
x = new Button("Action!");
y = new Button("Stop");
z = new Button("Resume!");
add(x);
add(y);
add(z);


y.addActionListener(new ActionListener() {

@SuppressWarnings("deprecation")
public void actionPerformed(ActionEvent e)
{

runs = false;
repaint();
}
});


z.addActionListener(new ActionListener() {

@SuppressWarnings("deprecation")
public void actionPerformed(ActionEvent e)
{

try {

runs = true;
b.update(this);
repaint();
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();

}

b2.update2(this);
repaint();
}
});

}





@Override
public void start(){
x.addActionListener(this);
b = new Ball(100, 100);
b2 = new Ball(500, 500);
}
@Override

public void run(){

while(runs){
b.update(this);
b2.update2(this);

repaint();
try {
Thread.sleep(17);
} catch (InterruptedException e) {
//TODO Auto-generated catch block
// e.printStackTrace();
}
}
}



@Override
public void stop(){


}
@Override
public void destroy(){

}
@Override
public void update(Graphics g) {
// TODO Auto-generated method stub
if(i == null){
i = createImage(this.getSize().width, this.getSize().height);
doubleG = i.getGraphics();
}
doubleG.setColor(getBackground());
doubleG.fillRect(0, 0, this.getSize().width, this.getSize().height);

doubleG.setColor(getForeground());
paint(doubleG);

g.drawImage(i, 0, 0, this);

}

@Override
public void paint(Graphics g){
b.paint(g);
b2.paint(g);
}

public void actionPerformed(ActionEvent e) {
thread.start();

}


}

对于主要列车.class 和:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.ActionListener;


public class Ball {


private int x;
private int y;
private double dx = 7.9;
private double dy = 7;
private int radius = 20;


public Ball() {
// TODO Auto-generated constructor stub
}


public Ball(int i, int j) {
// TODO Auto-generated constructor stub
x = i;
y = j;

}
public void update(train sp){
x += dx;

// if(x + dx > sp.getSize().width - 300){
// dx=0;
// }
}


public void paint(Graphics g){
g.setColor(Color.GREEN);


g.fillOval(x-radius, y-radius, radius * 2, radius * 2);

}
public void update2(train sp){
y -= dy;
if(y - dy < sp.getSize().height - 470){

x += dx;
y -= dy;



// if(y < sp.getSize().height - 470){
// y = sp.getSize().height -470;
// dy *= energyloss;
// dy = -dy;
// }else{

// dy += gravity * dt;
// y += dy*dt + .5 * gravity * dt * dt;
}
//}
}
public void update(ActionListener actionListener) throws InterruptedException {

x += dx;
}

public void update2(ActionListener actionListener) {

train tr = new train();


if(y - dy < tr.getSize().height - 470){
x += dx;
y -= dy;
}else{
y-=dy;

}





}
}

我想做的是制作一个恢复按钮。我已经完成了开始和暂停,但是当我单击恢复按钮时,它一次只移动 1 个坐标。我需要它就像开始、暂停和正常播放一样。请帮忙。 T_T

最佳答案

一个简单的解决方法是不让“runs”控制循环,而只是确定是否调用 update 方法。这样你就不会打破循环而必须重新启动。

关于java - 暂停/恢复 java 小程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15884511/

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