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

boxbounce5

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