/* * Class: BalloonApplet * * Author: Your_Name(s) * based on a template provided by Alyce Brady * with assistance from: people who helped (including instructor/TAs) * * Creation date: 2 March 2003 * Modifications: * Date Name reason * ---- ---- ------ * * License: * This program is free software; you can redistribute it * and/or modify it under the terms of the GNU General Public * License as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. */ import java.applet.Applet; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.geom.Ellipse2D; import java.awt.geom.Line2D; import java.awt.geom.Rectangle2D; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Random; import javax.swing.Timer; /** * The BalloonApplet class implements an applet that contains a balloon * race simulation. It [need some more details here!] * * @author Your Name */ public class BalloonApplet extends Applet { // Named constants that specify how far a balloon may move in one timestep. // MAX_DISTANCE must be strictly greater than MIN_DISTANCE. private static final int MIN_DISTANCE = 10; private static final int MAX_DISTANCE = 70; // Instance Variables: Encapsulated data for applet private int balloonHeight; private int balloonWidth; private int overallBalloonHeight; private int overallBalloonWidth; private Balloon b1; private int startLineX; private int finishLineX; private Line2D.Double startLine; private Line2D.Double finishLine; private int labelHeight; private Timer timer; private int numSteps = 0; // temporary kludge; should be deleted /** Initializes applet information when applet is loaded into the system. **/ public void init() { // Construct the balloon. balloonHeight = Integer.parseInt(getParameter("BalloonHeight")); balloonWidth = balloonHeight; overallBalloonHeight = balloonHeight; overallBalloonWidth = balloonWidth; b1 = new Balloon(getRandomColor(), getRandomAltitude(), MIN_DISTANCE, MAX_DISTANCE, getHeight() - overallBalloonHeight); // Construct the start line. labelHeight = 20; // height of start/finish labels startLineX = 30; int bottomOfLine = getHeight() - labelHeight; int lineLength = 20; startLine = new Line2D.Double(startLineX, bottomOfLine - lineLength, startLineX, bottomOfLine); // Set up the timer for the animation and start it going. The timer // delay gives the user time to view each "snapshot" in the animation. // Each time the timer goes off, the step method will be called. int delay = 500; // time to view the display -- 500 milliseconds timer = new Timer(delay, new ActionListener() // new class created on-the-fly { public void actionPerformed(ActionEvent evt) { step(); } }); timer.start(); } /** Paints the contents of the applet. Called by the browser or * appletviewer whenever the window has to be redrawn. Is also * called whenever a method in this class calls the repaint method. **/ public void paint(Graphics g) { // We're going to use Graphics2D package. Graphics2D g2 = (Graphics2D)g; // Draw the start line. g2.setColor(Color.black); g2.setFont(new Font("SansSerif", Font.BOLD, labelHeight)); g2.draw(startLine); g2.drawString("S", startLineX, getHeight()); // Calculate pixel coordinates of upper-left of balloon, then draw. int x = getRandomX(); // kludge!!! int y = getHeight() - overallBalloonHeight - getRandomAltitude(); // kludge!!! Ellipse2D.Double balloon = new Ellipse2D.Double(x, y, balloonWidth, balloonHeight); g2.setColor(getRandomColor()); // kludge!!! g2.fill(balloon); g2.setColor(Color.black); g2.draw(balloon); } /** Advances one step in the animation. **/ public void step() { // Has leading edge of balloon crossed finish line? // (Temp kludge is actually a test of whether we have run 5 steps.) if ( numSteps >= 5 ) timer.stop(); else { // Should move the balloon before repainting!!! repaint(); // will call paint with correct graphics context numSteps++; // temporary kludge! } } /** Gets a random altitude between 1/2 and 3/4ths of the height of * the window. **/ public int getRandomAltitude() { Random generator = RandNumGenerator.getInstance(); int halfAltitude = getHeight() / 2; int threeFourthsAltitude = 3 * getHeight() / 4; int range = threeFourthsAltitude - halfAltitude + 1; int altitudeWithinRange = generator.nextInt(range); return altitudeWithinRange + halfAltitude; } /** Gets a random color. **/ public Color getRandomColor() { Random generator = RandNumGenerator.getInstance(); int redAmount = generator.nextInt(256); int greenAmount = generator.nextInt(256); int blueAmount = generator.nextInt(256); return new Color(redAmount, greenAmount, blueAmount, 70); } /** Gets a random x value for the upper-left corner of a balloon. * (Temporary!!!) **/ public int getRandomX() { Random generator = RandNumGenerator.getInstance(); return generator.nextInt(getWidth() - overallBalloonWidth); } }