import java.awt.*;

public class New_Box extends java.applet.Applet{

private Button go_button;
private Button stop_button;
private Simulation simul;
private Thread runner;
private int xsize, ysize;
private Image image2;
private Graphics g2;

public Ball ball1;

// determine size of applet, create buttons, create balls 

public void init(){
xsize=this.size().width;
ysize=this.size().height;
go_button=new Button("Go");
stop_button=new Button("Stop");

ball1 = new Ball(Color.red,10,xsize,ysize);

simul = new Simulation(this);

add(go_button);
add(stop_button);}


// create a thread to control the Simulation 

public void start(){
runner = new Thread(simul);
runner.start();
runner.suspend();}

// stop thread when browser leaves page

public void stop(){
if(runner!=null)
runner.stop();
}

// paint method handles drawing

public void paint(Graphics g){

// double buffer to help flicker

if(g2==null) paintSetup();

g2.setColor(Color.black);
g2.fillRect(0,0,xsize,ysize);

g2.setColor(Color.white);
g2.fillRect(10,10,xsize-20,ysize-20);

// tell the Ball to draw itself to the screen 

ball1.draw(g2);

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

}

private void paintSetup(){
image2=createImage(xsize,ysize);
g2=image2.getGraphics();
}


// this method allows us to start and stop the motion of the ball
// by starting and stopping its execution thread

public boolean action(Event evt, Object arg){

if(evt.target instanceof Button)
if("Go".equals((String) arg)){
runner.resume();
return true;}
if("Stop".equals((String) arg)){
runner.suspend();
return true;}


return false;
}

public void update(Graphics g){
paint(g);
}

}


