- import java.awt.*;
- import java.awt.geom.*;
- import java.lang.Number;
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.Random;
- /**
- * Class BallDemo - provides two short demonstrations showing how to use the
- * Canvas class.
- *
- * @author Michael Kolling and David J. Barnes
- * @version 2006.03.30
- */
- public class BallDemo
- {
- private Canvas myCanvas;
- private ArrayList <BouncingBall> ballResevoir;
- private Random resevoirCapacity;
- private Random randomXPos;
- private Random randomYPos;
- private Random randomBallSize;
- /**
- * Create a BallDemo object. Creates a fresh canvas and makes it visible.
- */
- public BallDemo(int width, int height)
- {
- myCanvas = new Canvas("Ball Demo", width, height);
- myCanvas.setVisible(true);
- ballResevoir = new ArrayList<BouncingBall>();
- resevoirCapacity = new Random();
- randomXPos = new Random();
- randomYPos = new Random();
- randomBallSize = new Random();
- }
- /**
- * Demonstrate some of the drawing operations that are
- * available on a Canvas object.
- */
- public void drawDemo()
- {
- myCanvas.setFont(new Font("helvetica", Font.BOLD, 14));
- myCanvas.setForegroundColor(Color.red);
- myCanvas.drawString("We can draw text, ...", 20, 30);
- myCanvas.wait(1000);
- myCanvas.setForegroundColor(Color.black);
- myCanvas.drawString("...draw lines...", 60, 60);
- myCanvas.wait(500);
- myCanvas.setForegroundColor(Color.gray);
- myCanvas.drawLine(200, 20, 300, 50);
- myCanvas.wait(500);
- myCanvas.setForegroundColor(Color.blue);
- myCanvas.drawLine(220, 100, 370, 40);
- myCanvas.wait(500);
- myCanvas.setForegroundColor(Color.green);
- myCanvas.drawLine(290, 10, 320, 120);
- myCanvas.wait(1000);
- myCanvas.setForegroundColor(Color.gray);
- myCanvas.drawString("...and shapes!", 110, 90);
- myCanvas.setForegroundColor(Color.red);
- // the shape to draw and move
- int xPos = 10;
- Rectangle rect = new Rectangle(xPos, 150, 30, 20);
- // move the rectangle across the screen
- for(int i = 0; i < 200; i ++) {
- myCanvas.fill(rect);
- myCanvas.wait(10);
- myCanvas.erase(rect);
- xPos++;
- rect.setLocation(xPos, 150);
- }
- // at the end of the move, draw once more so that it remains visible
- myCanvas.fill(rect);
- }
- /**
- * Generate a random number to fill the ball resevoir
- * @ return a random number between x and y
- */
- /* public int fillBallResevoir(int maxCapacity)
- {
- int numberOfBalls = resevoirCapacity.nextInt(maxCapacity);
- if (numberOfBalls < 2)
- {
- fillBallResevoir(maxCapacity);
- // ballResevoir.add(BouncingBall);
- }
- else
- {
- return numberOfBalls;
- }
- return maxCapacity;
- }*/
- /**
- * generate a random number for ball size
- * @ return a random number between 20 and maximum for ball size.
- *
- */
- /*public int setBallSize(int maxSize)
- {
- Iterator<BouncingBall> it = ballResevoir.iterator();
- while(it.hasNext())
- {
- ballSize = randomBallSize.nextInt(maxSize);
- if(ballSize > 20 & ballSize<60)
- {
- System.out.println("ball size" + ballSize);
- return ballSize;
- }
- else
- {
- setBallSize(maxSize);
- }
- }
- {
- return maxSize;
- }
- }*/
- /* Simulate two bouncing balls
- */
- public int setballXPos()
- {
- return randomXPos.nextInt();
- }
- public int setballYPos (int ceiling)
- {
- return randomYPos.nextInt(ceiling);
- }
- public void bounce(int ceiling)
- {
- int ground = 400; // position of the ground line
- myCanvas.setVisible(true);
- // draw the ground
- myCanvas.drawLine(50, ground, 550, ground);
- BouncingBall ball = new BouncingBall(setballXPos(), setballYPos(), 16, Color.blue, ground, myCanvas);
- ball.draw();
- BouncingBall ball2 = new BouncingBall(70, 80, 20, Color.red, ground, myCanvas);
- ball2.draw();
- // make them bounce
- boolean finished = false;
- while(!finished) {
- myCanvas.wait(50); // small delay
- ball.move();
- ball2.move();
- // stop once ball has travelled a certain distance on x axis
- if(ball.getXPosition() >= 550 && ball2.getXPosition() >= 550) {
- finished = true;
- }
- }
- ball.erase();
- ball2.erase();
- }
- public void drawFrame()
- {
- int cHeight = new Double(myCanvas.getHeight()).intValue();
- int cWidth = new Double(myCanvas.getWidth()).intValue();
- int fHeight = cHeight -40;
- int fWidth = cWidth -40;
- Rectangle frame = new Rectangle(0, 0, cWidth, cHeight);
- myCanvas.fill(frame);
- myCanvas.wait(200);
- myCanvas.eraseRectangle (20, 20, fWidth, fHeight);
- }
- }