左右两个球在某个坐标处相遇时相互碰撞。我已经完成了我在互联网上搜索的内容并且它工作得很好,但我需要一个开始,暂停和恢复按钮。看看我完成了什么:
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();
}
}
主要的train.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
答案 0 :(得分:1)
一个简单的解决方法是不让“运行”控制循环,而只是确定是否调用了update方法。这样你就不会破坏循环而必须重新启动。