/** Example from "Java Certification Exam Guide" by Barry Boone p 255
    Creating threads by implementing Runnable
    Bounces a ball up and down on the screen
*/

import java.awt.*;
import java.applet.Applet;

public class UpDown_1 extends Applet implements Runnable {
    static int RADIUS = 20;
    static int X = 30;
    public int y = 30;    
    Thread t;
    
    public void init() {
        Thread t = new Thread(this);
        t.start();
    }
    
    public void paint(Graphics g) {
        g.setColor(Color.blue);
        g.drawOval(X - RADIUS, y - RADIUS, 2*RADIUS, 2*RADIUS);        
    }
    
    public void run(){
        int yDir = +1;
        int incr = 10;
        int sleepFor = 100;
    
        while(true) {
            y += (incr * yDir);
            repaint();
            
            if(y - UpDown.RADIUS < incr ||
               y + UpDown.RADIUS + incr > 
               getSize().height)
               yDir *= -1;
               
            try{
                t.sleep(sleepFor);
            }catch(InterruptedException e) {}
        } // end while
    }
}