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

random ball placement 2

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