- 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 BoxBounce
- {
- private Canvas myCanvas;
- private ArrayList <BouncingBall> ballResevoir;
- private Random resevoirCapacity;
- private Random randomXPos;
- private Random randomYPos;
- private Random randomBallSize;
- private int width;
- private int height;
- private int boxWidth;
- private int boxHeight;
- /**
- * Create a BallDemo object. Creates a fresh canvas and makes it visible.
- */
- public BoxBounce(int width, int height)
- {
- myCanvas = new Canvas("Box Bounce", width, height);
- myCanvas.setVisible(true);
- this.width = width;
- this.height = height;
- randomXPos = new Random();
- randomYPos = 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);
- }
- public void drawBox()
- {
- int cHeight = new Double(myCanvas.getHeight()).intValue();
- int cWidth = new Double(myCanvas.getWidth()).intValue();
- int boxHeight = (cHeight - cHeight/2);
- int boxWidth = (cWidth -cWidth/2);
- Rectangle frame = new Rectangle(20, 20, boxWidth, boxHeight);
- myCanvas.fill(frame);
- myCanvas.wait(200);
- myCanvas.eraseRectangle (40, 40, boxWidth-40, boxHeight-40);
- this.boxWidth = boxWidth;
- this.boxHeight = boxHeight;
- }
- /* Simulate two bouncing balls
- */
- public int setballXPos()
- {
- System.out.println("box width " + boxWidth);
- int ballXPos = randomXPos.nextInt(boxWidth-20);
- if (ballXPos > 40 && ballXPos < boxWidth - 40)
- {
- System.out.println("Win! " + ballXPos);
- return ballXPos;
- }
- else
- {
- System.out.println("Fail! " + ballXPos);
- ballXPos = setballXPos();
- return ballXPos;
- }
- //System.out.println("Weird!" + ballXPos);
- //return ballXPos;
- }
- public int setballYPos ()
- {
- int ballYPos = randomYPos.nextInt(boxHeight-20);
- System.out.println("ball ypos " + ballYPos);
- return ballYPos;
- }
- public void bounce()
- {
- 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();
- }
- }