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: 3.93 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.  
  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.     /**
  36.      * Constructor for objects of class BouncingBall
  37.      *
  38.      * @param xPos  the horizontal coordinate of the ball
  39.      * @param yPos  the vertical coordinate of the ball
  40.      * @param ballDiameter  the diameter (in pixels) of the ball
  41.      * @param ballColor  the color of the ball
  42.      * @param groundPos  the position of the ground (where the wall will bounce)
  43.      * @param drawingCanvas  the canvas to draw this ball on
  44.      */
  45.     public BouncingBall(int xPos, int yPos, int ballDiameter, Color ballColor,
  46.                         int groundPos, Canvas drawingCanvas, int ceilingPos, int leftWallPos, int rightWallPos,)
  47.     {
  48.         xPosition = xPos;
  49.         yPosition = yPos;
  50.         color = ballColor;
  51.         diameter = ballDiameter;
  52.         groundPosition = groundPos;
  53.         canvas = drawingCanvas;
  54.         ceilingPosition = ceilingPos;
  55.         leftWallPosition = leftWallPos;
  56.         rightWallPosition = rightWallPos;
  57.     }
  58.  
  59.     /**
  60.      * Draw this ball at its current position onto the canvas.
  61.      **/
  62.     public void draw()
  63.     {
  64.         canvas.setForegroundColor(color);
  65.         canvas.fillCircle(xPosition, yPosition, diameter);
  66.     }
  67.  
  68.     /**
  69.      * Erase this ball at its current position.
  70.      **/
  71.     public void erase()
  72.     {
  73.         canvas.eraseCircle(xPosition, yPosition, diameter);
  74.     }    
  75.  
  76.     /**
  77.      * Move this ball according to its position and speed and redraw.
  78.      **/
  79.     public void move()
  80.     {
  81.         // remove from canvas at the current position
  82.         erase();
  83.            
  84.         // compute new position
  85.         ySpeed += 2;
  86.         yPosition += ySpeed;
  87.         xPosition +=2;
  88.  
  89.         // check if it has hit the ground
  90.        if(yPosition >= (groundPosition - diameter) && ySpeed > 0)
  91.        {
  92.             yPosition = (int)(groundPosition - diameter);
  93.             //ySpeed = -ySpeed + ballDegradation;
  94.         }
  95.        
  96.         // check to see if it has hit the ceiling
  97.        if(yPosition <= (ceilingPosition + diameter) && ySpeed > 0)
  98.         {
  99.             yPosition = (int)(ceilingPosition + diameter);
  100.         }
  101.        
  102.         // check to see if it has hit the left wall
  103.       if (xPosition >= leftWallPosition + diameter)
  104.         {
  105.             xPosition = (int)(ceilingPosition + diameter);
  106.         }
  107.        
  108.        // check to see if it has hit the right wall
  109.       if (xPosition <= rightWallPosition + diameter)
  110.        {
  111.            xPosition = (int)(ceilingPosition + diameter);
  112.         }
  113.        
  114.        
  115.  
  116.         // draw again at new position
  117.         draw();
  118.     }    
  119.  
  120.     /**
  121.      * return the horizontal position of this ball
  122.      */
  123.     public int getXPosition()
  124.     {
  125.         return xPosition;
  126.     }
  127.  
  128.     /**
  129.      * return the vertical position of this ball
  130.      */
  131.     public int getYPosition()
  132.     {
  133.         return yPosition;
  134.     }
  135. }