// $Id: Bouncer.java,v 1.4 1998/04/16 20:46:09 gjb Exp $ /** Bouncer applet CSE 341, Java Project Bounce a ball around The ball should bounce off of all the ways, and should accelerate towards to floor (it should obey the law of gravity) */ import java.awt.*; import java.applet.Applet; /** Ball is just a ball that will bounce around the frame. It has a color and a size, both of which are hard coded (feel free to make them parameters). Ball's know how to compute their nextPosition(), and how to draw() themselves onto a given Graphics device. */ class Ball { // Constants. public static final Color COLOR = Color.blue; public static final int DIAMETER = 50; // Instance variables accessible to objects in the same package. int xpos; int ypos; // Private instance variables. int jumpIncrementX; // the x component of the velocity int jumpIncrementY; // the y component of the velocity /** Construct a ball. */ Ball(Bouncer bouncer) { xpos = 0; ypos = 0; jumpIncrementX = 4; jumpIncrementY = 3; } /** Set the ball's position to a given location. */ public void setPosition(int x, int y) { xpos = x; ypos = y; } /** Update the ball's position based on its old position and its velocity (the jumpIncrement vector) */ public void nextPosition() { xpos += jumpIncrementX; ypos += jumpIncrementY; } /** Draw the ball */ public void draw(Graphics g) { int radius = DIAMETER/2; g.setColor(COLOR); g.fillOval(xpos - radius, ypos - radius, DIAMETER, DIAMETER); } } /** Controlling class to simulate a ball bouncing */ public class Bouncer extends Applet implements Runnable { // Public instance variables. public Ball ball; // Private instance variables. private Thread myThread; private boolean freeze = false; // These are the size of the frame for the Applet private int width = 500; private int height = 400; // 100 ms delay = 1/10 sec private int cmsAnimationDelay = 100; /** Initialize the application. */ public void init () { ball = new Ball(this); } /** Create a thread and make this thread's target the applet. */ public void start () { if (myThread == null) { myThread = new Thread(this, "Bouncer"); myThread.start(); } } /** Keep the ball(s) moving */ public void run() { while (myThread != null) { // slow things down for looks. try { myThread.sleep(cmsAnimationDelay); } catch (InterruptedException e) { /* just catch it -- do nothing */ } if (!freeze) ball.nextPosition(); repaint(); } } /** Stop the ball from moving */ public void stop() { myThread.stop(); myThread = null; } /** Draw the scene */ public void paint(Graphics g) { ball.draw(g); } /** Accessor function just returns the width of the applet */ public int getWidth() { return width; } /** Accessor function just returns the height of the applet */ public int getHeight() { return height; } /** Accessor function just returns the width and height as a Dimension */ public Dimension getDimension() { return new Dimension(width, height); } /** Reset the ball to this position as the user drags it around. @deprecated */ public boolean mouseDrag(Event e, int x, int y) { ball.setPosition(x, y); return false; // pass this event up the window hierarchy } /** Called when the mouse goes down. Stop moving @deprecated */ public boolean mouseDown(Event e, int x, int y) { freeze = true; return false; // pass this event up the window hierarchy } /** Called when the mouse goes up. Start moving again. @deprecated */ public boolean mouseUp(Event e, int x, int y) { freeze = false; // redraw the screen entirely repaint(); return false; // pass this event up the window hierarchy } /** main just builds our Bouncer applet, and displays the applet in a frame that we resize to be the size of the bouncer applet, then starts the applet. By providing this main method, we can invoke the applet by simply running "java Bouncer" (when Bouncer.class is in our $CLASSPATH). Without this main method, we'd have to use "appletviewer Bouncer.html" to run the applet. Bouncer.html: Java disabled */ public static void main(String args[]) { Bouncer b = new Bouncer(); b.init(); // Create a window hierarchy and hook it up. Frame f = new Frame("Bouncer"); f.add(b); // resize has been deprecated by Sun -- if you are using // >= JDK 1.1, you can use: // f.setSize(b.getDimension()); // and it will avoid the compiler warning f.resize(b.getWidth(), b.getHeight() ); f.show(); // Start the app (this is done by the appletviewer or Web browser // for us automatically, but it's not done if we're running the app // from the command line). b.start(); } }