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

bouncingball3

By: ace on Mar 10th, 2010  |  syntax: None  |  size: 4.54 KB  |  hits: 6  |  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.  
  4. /**
  5.  * Class BouncingBall - a graphical ball that observes the effect of gravity. The ball
  6.  * has the ability to move. Details of movement are determined by the ball itself. It
  7.  * will fall downwards, accelerating with time due to the effect of gravity, and bounce
  8.  * upward again when hitting the ground.
  9.  *
  10.  * This movement can be initiated by repeated calls to the "move" method.
  11.  *
  12.  * @author Bruce Quig
  13.  * @author Michael Kolling (mik)
  14.  * @author David J. Barnes
  15.  *
  16.  * @version 2006.03.30
  17.  */
  18.  
  19. public class BouncingBall
  20. {
  21.     private static final int GRAVITY = 3;  // effect of gravity
  22.  
  23.     private int ballDegradation = 2;
  24.     private Ellipse2D.Double circle;
  25.     private Color color;
  26.     private int diameter;
  27.     private int xPosition;
  28.     private int yPosition;
  29.     private final int groundPosition;      // y position of ground
  30.     private Canvas canvas;
  31.     private int ySpeed = 1;    // initial downward speed
  32.     private int ceilingPosition;
  33.     private int leftWallPosition;
  34.     private int rightWallPosition;
  35.     private boolean rightVector;
  36.     private boolean downVector;
  37.    
  38.     /**
  39.      * Constructor for objects of class BouncingBall
  40.      *
  41.      * @param xPos  the horizontal coordinate of the ball
  42.      * @param yPos  the vertical coordinate of the ball
  43.      * @param ballDiameter  the diameter (in pixels) of the ball
  44.      * @param ballColor  the color of the ball
  45.      * @param groundPos  the position of the ground (where the wall will bounce)
  46.      * @param drawingCanvas  the canvas to draw this ball on
  47.      */
  48.     public BouncingBall(int xPos, int yPos, int ballDiameter, Color ballColor,
  49.                         int groundPos, Canvas drawingCanvas, int ceilingPos, int leftWallPos, int rightWallPos)
  50.     {
  51.         xPosition = xPos;
  52.         yPosition = yPos;
  53.         color = ballColor;
  54.         diameter = ballDiameter;
  55.         groundPosition = groundPos;
  56.         canvas = drawingCanvas;
  57.         ceilingPosition = ceilingPos;
  58.         leftWallPosition = leftWallPos;
  59.         rightWallPosition = rightWallPos;
  60.         rightVector = true;
  61.         downVector = true;
  62.     }
  63.  
  64.     /**
  65.      * Draw this ball at its current position onto the canvas.
  66.      **/
  67.     public void draw()
  68.     {
  69.         canvas.setForegroundColor(color);
  70.         canvas.fillCircle(xPosition, yPosition, diameter);
  71.     }
  72.  
  73.     /**
  74.      * Erase this ball at its current position.
  75.      **/
  76.     public void erase()
  77.     {
  78.         canvas.eraseCircle(xPosition, yPosition, diameter);
  79.     }    
  80.  
  81.     /**
  82.      * Move this ball according to its position and speed and redraw.
  83.      **/
  84.     public void move()
  85.     {
  86.         // remove from canvas at the current position
  87.         erase();
  88.      
  89.        
  90. /* compute new position
  91.         ySpeed += 2;
  92.         yPosition += ySpeed;
  93.         xPosition +=2;*/
  94.         if (rightVector = true)
  95.         {
  96.              xPosition +=2;
  97.             }
  98.            
  99.         if (rightVector = false)
  100.         {
  101.             xPosition -=2;
  102.         }
  103.        
  104.         if (downVector = true)
  105.         {
  106.             yPosition += 2;
  107.         }
  108.        
  109.         if (downVector = false)
  110.     {
  111.         yPosition -= 2;
  112.     }
  113.            
  114.        
  115.  
  116.         // check if it has hit the ground
  117.        if(yPosition >= (groundPosition - diameter) && ySpeed > 0)
  118.        {
  119.             yPosition = (groundPosition - diameter);
  120.             downVector = false;
  121.            
  122.             //ySpeed = -ySpeed + ballDegradation;
  123.         }
  124.        
  125.         // check to see if it has hit the ceiling
  126.        if(yPosition <= (ceilingPosition + diameter))
  127.         {
  128.             yPosition = (ceilingPosition + diameter);
  129.             downVector = true;
  130.         }
  131.        
  132.         // check to see if it has hit the left wall
  133.       if (xPosition >= leftWallPosition + diameter)
  134.         {
  135.             xPosition = (leftWallPosition + diameter);
  136.             rightVector = true;
  137.         }
  138.        
  139.        // check to see if it has hit the right wall
  140.       if (xPosition <= rightWallPosition - diameter)
  141.        {
  142.            xPosition = (rightWallPosition - diameter);
  143.            rightVector = false;
  144.         }
  145.        
  146.        
  147.  
  148.         // draw again at new position
  149.         draw();
  150.     }    
  151.  
  152.     /**
  153.      * return the horizontal position of this ball
  154.      */
  155.     public int getXPosition()
  156.     {
  157.         return xPosition;
  158.     }
  159.  
  160.     /**
  161.      * return the vertical position of this ball
  162.      */
  163.     public int getYPosition()
  164.     {
  165.         return yPosition;
  166.     }
  167. }