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

boxbounce 3

By: ace on Mar 3rd, 2010  |  syntax: None  |  size: 4.93 KB  |  hits: 29  |  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.ArrayList;
  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 BoxBounce  
  17. {
  18.     private Canvas myCanvas;
  19.     private ArrayList <BouncingBall> ballResevoir;
  20.     private Random resevoirCapacity;
  21.     private Random randomXPos;
  22.     private Random randomYPos;
  23.     private Random randomBallSize;
  24.     private int width;
  25.     private int height;
  26.    
  27.  
  28.     /**
  29.      * Create a BallDemo object. Creates a fresh canvas and makes it visible.
  30.      */
  31.     public BoxBounce(int width, int height)
  32.     {
  33.         myCanvas = new Canvas("Box Bounce", width, height);
  34.         myCanvas.setVisible(true);
  35.         this.width = width;
  36.        this.height = height;
  37.        
  38.     }
  39.  
  40.     /**
  41.      * Demonstrate some of the drawing operations that are
  42.      * available on a Canvas object.
  43.      */
  44.     public void drawDemo()
  45.     {
  46.         myCanvas.setFont(new Font("helvetica", Font.BOLD, 14));
  47.         myCanvas.setForegroundColor(Color.red);
  48.  
  49.         myCanvas.drawString("We can draw text, ...", 20, 30);
  50.         myCanvas.wait(1000);
  51.  
  52.         myCanvas.setForegroundColor(Color.black);
  53.         myCanvas.drawString("...draw lines...", 60, 60);
  54.         myCanvas.wait(500);
  55.         myCanvas.setForegroundColor(Color.gray);
  56.         myCanvas.drawLine(200, 20, 300, 50);
  57.         myCanvas.wait(500);
  58.         myCanvas.setForegroundColor(Color.blue);
  59.         myCanvas.drawLine(220, 100, 370, 40);
  60.         myCanvas.wait(500);
  61.         myCanvas.setForegroundColor(Color.green);
  62.         myCanvas.drawLine(290, 10, 320, 120);
  63.         myCanvas.wait(1000);
  64.  
  65.         myCanvas.setForegroundColor(Color.gray);
  66.         myCanvas.drawString("...and shapes!", 110, 90);
  67.  
  68.         myCanvas.setForegroundColor(Color.red);
  69.  
  70.         // the shape to draw and move
  71.         int xPos = 10;
  72.         Rectangle rect = new Rectangle(xPos, 150, 30, 20);
  73.  
  74.         // move the rectangle across the screen
  75.         for(int i = 0; i < 200; i ++) {
  76.             myCanvas.fill(rect);
  77.             myCanvas.wait(10);
  78.             myCanvas.erase(rect);
  79.             xPos++;
  80.             rect.setLocation(xPos, 150);
  81.         }
  82.         // at the end of the move, draw once more so that it remains visible
  83.         myCanvas.fill(rect);
  84.     }
  85.    
  86.     public void drawBox()
  87.  
  88.     {
  89.        int cHeight = new Double(myCanvas.getHeight()).intValue();
  90.        int cWidth =  new Double(myCanvas.getWidth()).intValue();
  91.        int boxHeight = (cHeight - cHeight/2);
  92.        int boxWidth = (cWidth -cWidth/2);
  93.        Rectangle frame = new Rectangle(20, 20, boxWidth, boxHeight);
  94.          myCanvas.fill(frame);
  95.         myCanvas.wait(200);
  96.          myCanvas.eraseRectangle (40, 40, boxWidth-40, boxHeight-40);
  97.          
  98.         }
  99.          
  100.    
  101.        
  102.     /* Simulate two bouncing balls
  103.      */
  104.    
  105.    public int setballXPos()
  106.    {
  107.        int ballXPos = randomXPos.nextInt(boxWidth+20);
  108.        if (ballXPos > 40 & ballXPos < boxWidth - 40)
  109.         {    
  110.        System.out.println("width " + reportWidth);
  111.        return reportWidth;
  112.     }
  113.    
  114.     else
  115.     {
  116.         setballXPos();
  117.     }
  118.     return ballXPos;
  119. }
  120.    
  121.     public int setballYPos ()
  122.     {
  123.         int reportHeight = randomYPos.nextInt(height/2);
  124.         System.out.println("height " + reportHeight);
  125.         return reportHeight;
  126.     }
  127.    
  128.     public void bounce()
  129.     {
  130.         int ground = 400;   // position of the ground line
  131.        
  132.         myCanvas.setVisible(true);
  133.  
  134.         // draw the ground
  135.         myCanvas.drawLine(50, ground, 550, ground);
  136.  
  137.         BouncingBall ball = new BouncingBall(setballXPos(), setballYPos(), 16, Color.blue, ground, myCanvas);
  138.         ball.draw();
  139.         BouncingBall ball2 = new BouncingBall(70, 80, 20, Color.red, ground, myCanvas);
  140.         ball2.draw();
  141.        
  142.        
  143.         // make them bounce
  144.     boolean finished =  false;
  145.         while(!finished) {
  146.             myCanvas.wait(50);           // small delay
  147.             ball.move();
  148.             ball2.move();
  149.             // stop once ball has travelled a certain distance on x axis
  150.             if(ball.getXPosition() >= 550 && ball2.getXPosition() >= 550) {
  151.                 finished = true;
  152.             }
  153.         }
  154.         ball.erase();
  155.         ball2.erase();
  156.        
  157.        
  158.     }
  159.    
  160.     public void drawFrame()
  161.     {
  162.        int cHeight = new Double(myCanvas.getHeight()).intValue();
  163.        int cWidth =  new Double(myCanvas.getWidth()).intValue();
  164.        int fHeight = cHeight -40;
  165.        int fWidth = cWidth -40;
  166.        Rectangle frame = new Rectangle(0, 0, cWidth, cHeight);
  167.          myCanvas.fill(frame);
  168.          myCanvas.wait(200);
  169.          myCanvas.eraseRectangle (20, 20, fWidth, fHeight);
  170.          
  171.         }
  172.  
  173.    
  174.    
  175. }