import java.awt.Color; // Yes, we care about colors import java.awt.Frame; // The window that holds our baloon import java.awt.Graphics; // Our handy-dandy paintbrush import java.awt.Rectangle; // For the bounds /** * A simple application that animates an expanding circle. * * @author Samuel A. Rebelsky * @version 1.0 of March 2000 */ public class Balloon extends Frame implements Runnable { // +--------+-------------------------------------------------- // | Fields | // +--------+ /** * The color of the balloon. */ protected Color color; /** * The radius of the balloon. */ protected int radius; /** * The x coordinate of the center of the balloon. */ protected int xcoord; /** * The y coordinate of the center of the balloon. */ protected int ycoord; /** * The amount to change the radius by. */ protected int deltaRadius; /** * The thread of computation associated with this object. */ protected Thread thread; // +--------------+-------------------------------------------- // | Constructors | // +--------------+ public Balloon(Color initialColor) { this.color = initialColor; this.radius = 10; this.deltaRadius = 5; this.xcoord = 50; this.ycoord = 300; this.setSize(400,400); this.show(); this.thread = new Thread(this); this.thread.setPriority(Thread.MIN_PRIORITY); this.thread.start(); } // Balloon // +-----------+----------------------------------------------- // | Modifiers | // +-----------+ /** * Update the balloon. */ public void update() { this.radius = this.radius + this.deltaRadius; this.xcoord = this.xcoord + 1; this.ycoord = this.ycoord - 2; } // update() // +-------------------------+--------------------------------- // | Overriden Frame Methods | // +-------------------------+ /** * Paint the current version of the balloon. */ public void paint(Graphics paintBrush) { // System.err.println("Painting ..."); Rectangle bounds = this.getBounds(); if (this.radius < 10) { this.deltaRadius = 5; } else if (2*this.radius > bounds.width) { this.deltaRadius = -5; } // Paint the balloon paintBrush.setColor(this.color); paintBrush.fillOval(this.xcoord-this.radius, this.ycoord-this.radius, this.radius*2, this.radius*2); } // paint(Graphics) // +------------------+---------------------------------------- // | Runnable Methods | // +------------------+ public void run() { while (true) { try { this.thread.sleep(200); } catch (InterruptedException e) { } update(); repaint(); } // while } // run() // +------+---------------------------------------------------- // | Main | // +------+ public static void main(String[] args) { new Balloon(Color.red); } } // class Balloon