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 placement1

By: ace on Mar 3rd, 2010  |  syntax: None  |  size: 5.44 KB  |  hits: 10  |  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.    
  25.  
  26.     /**
  27.      * Create a BallDemo object. Creates a fresh canvas and makes it visible.
  28.      */
  29.     public BallDemo(int width, int height)
  30.     {
  31.         myCanvas = new Canvas("Ball Demo", width, height);
  32.         myCanvas.setVisible(true);
  33.         ballResevoir = new ArrayList<BouncingBall>();
  34.        resevoirCapacity = new Random();
  35.        randomXPos = new Random();
  36.        randomYPos = new Random();
  37.        randomBallSize = new Random();
  38.        
  39.        
  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.     /**
  89.      * Generate a random number to fill the ball resevoir
  90.      * @ return a random number between x and y
  91.      */
  92.    
  93.    /* public int fillBallResevoir(int maxCapacity)
  94.     {
  95.       int numberOfBalls = resevoirCapacity.nextInt(maxCapacity);
  96.       if (numberOfBalls < 2)
  97.         {
  98.           fillBallResevoir(maxCapacity);
  99.          // ballResevoir.add(BouncingBall);
  100.         }
  101.         else
  102.         {
  103.             return numberOfBalls;
  104.         }
  105.         return maxCapacity;
  106.     }*/
  107.    
  108.    
  109.     /**
  110.      * generate a random number for ball size
  111.      * @ return a random number between 20 and maximum for ball size.
  112.      *
  113.      */
  114.   /*public int setBallSize(int maxSize)
  115. {
  116.     Iterator<BouncingBall> it = ballResevoir.iterator();
  117.         while(it.hasNext())
  118.         {
  119.             ballSize = randomBallSize.nextInt(maxSize);
  120.             if(ballSize > 20 & ballSize<60)
  121.                 {
  122.                     System.out.println("ball size" + ballSize);
  123.                     return ballSize;
  124.                  
  125.                 }
  126.                 else
  127.                 {
  128.                     setBallSize(maxSize);
  129.                 }
  130.             }
  131.             {
  132.                
  133.                 return maxSize;
  134.         }
  135.     }*/
  136.  
  137.      /* Simulate two bouncing balls
  138.      */
  139.    
  140.    public int setballXPos()
  141.    {
  142.        return randomXPos.nextInt();
  143.     }
  144.    
  145.     public int setballYPos (int ceiling)
  146.     {
  147.         return randomYPos.nextInt(ceiling);
  148.     }
  149.    
  150.     public void bounce(int ceiling)
  151.     {
  152.         int ground = 400;   // position of the ground line
  153.        
  154.         myCanvas.setVisible(true);
  155.  
  156.         // draw the ground
  157.         myCanvas.drawLine(50, ground, 550, ground);
  158.  
  159.         BouncingBall ball = new BouncingBall(setballXPos(), setballYPos(), 16, Color.blue, ground, myCanvas);
  160.         ball.draw();
  161.         BouncingBall ball2 = new BouncingBall(70, 80, 20, Color.red, ground, myCanvas);
  162.         ball2.draw();
  163.        
  164.  
  165.         // make them bounce
  166.       boolean finished =  false;
  167.         while(!finished) {
  168.             myCanvas.wait(50);           // small delay
  169.             ball.move();
  170.             ball2.move();
  171.             // stop once ball has travelled a certain distance on x axis
  172.             if(ball.getXPosition() >= 550 && ball2.getXPosition() >= 550) {
  173.                 finished = true;
  174.             }
  175.         }
  176.         ball.erase();
  177.         ball2.erase();
  178.        
  179.     }
  180.    
  181.     public void drawFrame()
  182.     {
  183.        int cHeight = new Double(myCanvas.getHeight()).intValue();
  184.        int cWidth =  new Double(myCanvas.getWidth()).intValue();
  185.        int fHeight = cHeight -40;
  186.        int fWidth = cWidth -40;
  187.        Rectangle frame = new Rectangle(0, 0, cWidth, cHeight);
  188.          myCanvas.fill(frame);
  189.          myCanvas.wait(200);
  190.          myCanvas.eraseRectangle (20, 20, fWidth, fHeight);
  191.          
  192.         }
  193.  
  194.    
  195.    
  196. }