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

frame3

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