import java.awt.*;
import java.awt.event.*;
import java.applet.*;

// There is no main

public class Bounce2App extends Applet  {

    private Panel canvas;

    public void init() {

	// putInCenter(); // put frame in center of screen

	setLayout(new BorderLayout());
	canvas = new Panel();
	add(canvas,"Center");

	//	addWindowListener(new WindowTerminator());

	Panel buttonPanel = new Panel();
	Button startButton = new Button("Start");

	startButton.addActionListener(new StartItUp());

	buttonPanel.add(startButton);
	add(buttonPanel,"South");

    }

    private class WindowTerminator extends WindowAdapter {
	public void windowClosing(WindowEvent e) {
	    System.exit(0);	
	}
    }
    private class StartItUp implements ActionListener {
	public void actionPerformed(ActionEvent evt) {
	    Bouncer2App b = new Bouncer2App(canvas);
	    b.start();
	}
    }
}

class Bouncer2App extends Thread {

    private Panel box;
    private static final int XSIZE=10;
    private static final int YSIZE=10;
    private int x=0;
    private int y=0;
    private int dx=2;
    private int dy=2;

    public Bouncer2App(Panel b) {
	box=b;
    }

    public void draw() {
	Graphics g = box.getGraphics();
	g.fillOval(x,y,XSIZE,YSIZE);
	g.dispose();
    }

    public void move() {
	Graphics g = box.getGraphics();
	g.setXORMode(box.getBackground());
	g.fillOval(x,y,XSIZE,YSIZE);
	x+=dx;
	y+=dy;
	Dimension d = box.getSize();
	if (x<0) {
	    x=0;
	    dx=-dx;
	}
	if (x+XSIZE >= d.width) {
	    x=d.width-XSIZE;
	    dx=-dx;
	}
	if (y<0) {
	    y=0;
	    dy=-dy;
	}
	if (y+YSIZE >= d.height) {
	    y=d.height-YSIZE;
	    dy=-dy;
	}
	g.fillOval(x,y,XSIZE,YSIZE);
	g.dispose();
    }

    public void run() {
	draw();
	for (int i=1; i<=1000; i++) {
	    move();
	    try {
                Thread.sleep(700);
            }
            catch(InterruptedException e){
            }

	}
    }
}

