0

two balls from left and bottom colliding with each other as they meet at a certain coordinate. I have already did what I searched on the internet and it worked perfectly, but I need a start, pause and resume buttons. Look at what I finished :

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();

     }


}

for the main train.class and :

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;

        }





    }
}

What I want to do is I want to make a resume button. I already finished the start and pause, but when I click the resume button, it just moves 1 coordinate at a time. I need it to just like Start , pause and play normally. Please help. T_T

4

1 回答 1

1

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

于 2013-04-08T16:54:10.967 回答