import javabook.*; import java.awt.*; /** * DrawingLab3 * Inspired by a lab from Jeff Ondich * @author Dave Musicant * @version 1.0 10/2/01 * * 0. Compile it, run it, and figure out how it works. * 1. To slow the motion down or speed it up, change the value inside the * "for" statement. * 2. Try to get the ball to bounce from side to side instead of up and down. * 3. Can you get the colors to change gradually from red to blue instead of * abruptly? */ class DrawingLab3 { // Data values private int centerX; private int centerY; private int radius; private boolean goingUp; private Color currentColor; private DrawingBoard drawingBoard; private MessageBox messageBox; private InputBox inputBox; /** * Constructor */ public DrawingLab3() { // Create objects for the DrawingBoard and other input / output drawingBoard = new DrawingBoard("Time to do some drawing"); messageBox = new MessageBox(drawingBoard); inputBox = new InputBox(drawingBoard); // Make the drawingBoard visible and bring to front drawingBoard.setVisible(true); drawingBoard.toFront(); } /** * Method that does the actual drawing. */ public void animate() { Dimension screenSize = drawingBoard.getSize(); int screenWidth = screenSize.width; int screenHeight = screenSize.height; Color backgroundColor = new Color(255,255,255); // Intialize locations radius = 50; centerX = screenWidth / 2; centerY = radius; goingUp = true; currentColor = new Color(255,0,0); while (true) { // Draw the circle drawingBoard.setColor(currentColor); drawingBoard.drawCircle(centerX,centerY,radius); // Wait a while for (int i=0; i<10000; i++) { }; // Erase the circle drawingBoard.setColor(backgroundColor); drawingBoard.drawCircle(centerX,centerY,radius); // Move the circle newCenter(screenWidth,screenHeight); } } public void newCenter(int width, int height) { if (goingUp) centerY = centerY - 1; else centerY = centerY + 1; if (centerY >= height - radius) { goingUp = true; currentColor = new Color(255,0,0); } else if (centerY <= radius) { goingUp = false; currentColor = new Color(0,0,255); } } }