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

box bounce 4

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