Pastebin launched a little side project called HostCabi.net, check it out ;-)Don't like ads? PRO users don't see any ads ;-)
Guest

bouncingball resevoir

By: ace on Feb 24th, 2010  |  syntax: None  |  size: 3.81 KB  |  hits: 8  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import java.awt.*;
  2. import java.awt.geom.*;
  3. import java.lang.Number;
  4. import java.util.HashSet;
  5. import java.util.Iterator;
  6. import java.util.Random;
  7.  
  8. /**
  9.  * Class BallDemo - provides two short demonstrations showing how to use the
  10.  * Canvas class.
  11.  *
  12.  * @author Michael Kolling and David J. Barnes
  13.  * @version 2006.03.30
  14.  */
  15.  
  16. public class BallDemo  
  17. {
  18.     private Canvas myCanvas;
  19.     private HashSet <BouncingBall> ballResevoir;
  20.     private Random randomBallPlacement;
  21.    
  22.  
  23.     /**
  24.      * Create a BallDemo object. Creates a fresh canvas and makes it visible.
  25.      */
  26.     public BallDemo(int width, int height,)
  27.     {
  28.         myCanvas = new Canvas("Ball Demo", width, height);
  29.         myCanvas.setVisible(true);
  30.         ballResevoir = new Hashset<BouncingBall>();
  31.         randomBallPlacement = new Random();
  32.        
  33.     }
  34.  
  35.     /**
  36.      * Demonstrate some of the drawing operations that are
  37.      * available on a Canvas object.
  38.      */
  39.     public void drawDemo()
  40.     {
  41.         myCanvas.setFont(new Font("helvetica", Font.BOLD, 14));
  42.         myCanvas.setForegroundColor(Color.red);
  43.  
  44.         myCanvas.drawString("We can draw text, ...", 20, 30);
  45.         myCanvas.wait(1000);
  46.  
  47.         myCanvas.setForegroundColor(Color.black);
  48.         myCanvas.drawString("...draw lines...", 60, 60);
  49.         myCanvas.wait(500);
  50.         myCanvas.setForegroundColor(Color.gray);
  51.         myCanvas.drawLine(200, 20, 300, 50);
  52.         myCanvas.wait(500);
  53.         myCanvas.setForegroundColor(Color.blue);
  54.         myCanvas.drawLine(220, 100, 370, 40);
  55.         myCanvas.wait(500);
  56.         myCanvas.setForegroundColor(Color.green);
  57.         myCanvas.drawLine(290, 10, 320, 120);
  58.         myCanvas.wait(1000);
  59.  
  60.         myCanvas.setForegroundColor(Color.gray);
  61.         myCanvas.drawString("...and shapes!", 110, 90);
  62.  
  63.         myCanvas.setForegroundColor(Color.red);
  64.  
  65.         // the shape to draw and move
  66.         int xPos = 10;
  67.         Rectangle rect = new Rectangle(xPos, 150, 30, 20);
  68.  
  69.         // move the rectangle across the screen
  70.         for(int i = 0; i < 200; i ++) {
  71.             myCanvas.fill(rect);
  72.             myCanvas.wait(10);
  73.             myCanvas.erase(rect);
  74.             xPos++;
  75.             rect.setLocation(xPos, 150);
  76.         }
  77.         // at the end of the move, draw once more so that it remains visible
  78.         myCanvas.fill(rect);
  79.     }
  80.  
  81.     /**
  82.      * Simulate two bouncing balls
  83.      */
  84.     public void bounce(int numberOfBalls)
  85.     {
  86.         int ground = 400;   // position of the ground line
  87.        
  88.         myCanvas.setVisible(true);
  89.  
  90.         // draw the ground
  91.         myCanvas.drawLine(50, ground, 550, ground);
  92.  
  93.         // crate and show the balls
  94.         BouncingBall ball = new BouncingBall(50, 50, 16, Color.blue, ground, myCanvas);
  95.         ball.draw();
  96.         BouncingBall ball2 = new BouncingBall(70, 80, 20, Color.red, ground, myCanvas);
  97.         ball2.draw();
  98.  
  99.         // make them bounce
  100.         boolean finished =  false;
  101.         while(!finished) {
  102.             myCanvas.wait(50);           // small delay
  103.             ball.move();
  104.             ball2.move();
  105.             // stop once ball has travelled a certain distance on x axis
  106.             if(ball.getXPosition() >= 550 && ball2.getXPosition() >= 550) {
  107.                 finished = true;
  108.             }
  109.         }
  110.         ball.erase();
  111.         ball2.erase();
  112.     }
  113.    
  114.     public void drawFrame()
  115.     {
  116.        int cHeight = new Double(myCanvas.getHeight()).intValue();
  117.        int cWidth =  new Double(myCanvas.getWidth()).intValue();
  118.        int fHeight = cHeight -40;
  119.        int fWidth = cWidth -40;
  120.        Rectangle frame = new Rectangle(0, 0, cWidth, cHeight);
  121.          myCanvas.fill(frame);
  122.          myCanvas.wait(200);
  123.          myCanvas.eraseRectangle (20, 20, fWidth, fHeight);
  124.          
  125.         }
  126.  
  127.    
  128.    
  129. }