
import java.awt.*;
import java.applet.Applet;
import java.io.*;
import java.awt.event.*;

public class Program3 extends Applet implements Runnable,
	ActionListener,	AdjustmentListener, ItemListener {;

private Scrollbar s;
private Button bstart, bstop;
private Thread t;
private Checkbox cbyes, cbno;
private CheckboxGroup cbgroup;
private Choice ch;
private myCanvas mc;
private Dimension d;
private int delay = 50;
private Label l;

public void init(){

	String att = getParameter("delay");
	if (att != null)
	delay = Integer.parseInt(att);
	d = getSize();
	mc = new myCanvas(d.width, d.height-100);
	add(mc);
	t = new Thread(this, "t");
	t.start();
		

	bstart = new Button("Start");
	add(bstart);
	bstart.addActionListener(this);
	bstart.setEnabled(false);


	bstop = new Button("Stop");
	add(bstop);
	bstop.addActionListener(this);
	bstop.setEnabled(true);

	ch = new Choice();
	add(ch);
	ch.addItem( "Left" );
	ch.addItem( "Right" );
	ch.addItem( "Up" );
	ch.addItem( "Down" );
	ch.addItemListener(this);

	cbgroup = new CheckboxGroup();
	cbno = new Checkbox("No Gravity", cbgroup, true);
	cbyes = new Checkbox("Gravity", cbgroup, false);	
	add(cbno);
	add(cbyes);
	cbno.addItemListener(this);
	cbyes.addItemListener(this);


	s = new Scrollbar(Scrollbar.VERTICAL, 0, 1, 0, 6);
	add(s);
	s.addAdjustmentListener(this);
	s.setEnabled(false);
	l = new Label("Gravity Adjuster");
	add(l);

}  //end of init()

public void run(){
	Thread current = Thread.currentThread();
	while( t == current )
	{
		try{
			current.sleep(delay);
	           } catch (InterruptedException e){} 
		mc.repaint();
	}
}

public void start(){
	if ( t == null)
	{
		t = new Thread(this);
		t.start();
	}
}

public void stop(){
	t = null;
}

public void actionPerformed(ActionEvent e){

	String str = e.getActionCommand();

	//System.out.print( str );

	if (str == "Start")
	{	
		bstart.setEnabled(false);
		bstop.setEnabled(true);
		t = new Thread(this);
		t.start();
	}
	if (str == "Stop")
	{
		bstart.setEnabled(true);
		bstop.setEnabled(false);
		t = null;
	}
}	

// *********************************************
// *********************************************
// *********************************************



public void itemStateChanged(ItemEvent e){
	Object obj = e.getItem();
	System.out.print(obj);
	
	if (obj == "Left")
		mc.SBS(-2, 0);
	if (obj == "Right")
		mc.SBS(2, 0);
	if (obj == "Up")
		mc.SBS(0, -2);
	if (obj == "Down")
		mc.SBS(0, 2);

	if (obj == "No Gravity")
		s.setEnabled(false);
	if (obj == "Gravity")
		s.setEnabled(true);
}



public void adjustmentValueChanged(AdjustmentEvent e){
	int g = s.getValue();
	mc.SBS(0, g);

}





} //end of pp3 class

// *********************************************
// *********************************************
// *********************************************

class myCanvas extends Canvas{

	private Dimension d;
	private int x, y; // (x, y), location of the ball
	private int r = 20;  // radius
	private int dx = 2, dy = 4;
	private Image im;
	private Graphics offscreen;
	
	Color color = Color.green;
	
public myCanvas(int w, int h){

	d = new Dimension(w, h);
	x = r;
	y = r;
}

public Dimension getMinimumSize(){
	return d;
}

public Dimension getPreferredSize(){
	return d;
}

public void update(Graphics g){
    // create the off-screen image buffer 
    // if it is invoked the first time 
    if (im == null) 
	{
      		im = createImage(d.width, d.height);
      		offscreen = im.getGraphics();
    	}

    // draw the background 
	offscreen.setColor(Color.white);
	offscreen.fillRect(0,0,d.width,d.height); 

	offscreen.setColor(Color.black);
	offscreen.drawRect(0,0,d.width-1,d.height-1);

    // adjust the poistion of the ball 
    // reverse the direction if it touches 
    // any of the four sides 

	int w = d.width;
	int h = d.height;

	x = x + dx;
	y = y + dy;
	
	if ( ( ( dx < 0 ) && ( x <= r ) ) || ( ( dx > 0 ) && ( x >= ( w - r ) ) ) )
		dx = -dx;
	if ( ( ( dy < 0 ) && ( y <= r ) ) || ( ( dy > 0 ) && ( y >= ( h - r ) ) ) )
		dy = -dy; 
	



    // draw the ball
    offscreen.setColor(color);
    offscreen.fillOval(x-r , y-r , 2*r, 2*r);  

    // copy the off-screen image to the screen 
    g.drawImage(im, 0, 0, this);
}

public void SBS(int vx, int vy){
	dx = dx + vx;
	dy = dy + vy;
}


}  //end of myCanvas class


