Phr0zen_Penguin

GraviBounce.java - 10 Bouncing Balls

Feb 14th, 2015
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.29 KB | None | 0 0
  1. /*==================================================================================================
  2.   --------------------------------------------------------------------------------------------------
  3.   [GraviBounce.java]
  4.   A real-time simulation of bouncing balls due to the effect of gravity on them.
  5.  
  6.         (c) Damion 'Phr0z3n.dev' Tapper, 2015.
  7.         Email: [email protected]
  8.         Website: L337Stuff.Blogspot.com
  9.   --------------------------------------------------------------------------------------------------
  10.   ==================================================================================================*/
  11. import java.applet.Applet;
  12. import java.awt.Rectangle;
  13. import java.awt.geom.Point2D.Double;
  14. import java.util.ArrayList;
  15. import java.awt.Graphics;
  16. import java.awt.Image;
  17. import java.awt.Color;
  18.  
  19. public class GraviBounce extends Applet implements Runnable
  20. {
  21.     private                 Rectangle           rect;
  22.     private final           int                 COUNT = 10;
  23.     private                 ArrayList<Double>   dPoint = new ArrayList<Double>(COUNT); // for regular arrays choke on my big fat... ambitions. ;)
  24.     private                 ArrayList<Double>   dPointVelocity = new ArrayList<Double>(COUNT);
  25.     private                 boolean             running = false;
  26.     private final   static  int                 diameter = 10; // a constant initial value which cannot be changed at runtime
  27.     private final   static  double              gravity = 0.3; // "
  28.     private                 Image               image = null;
  29.  
  30.     public void init()
  31.     {
  32.         rect = this.getBounds();
  33.  
  34.             for(int i = 0; i < COUNT; i++)
  35.             {
  36.                 dPoint.add(new Double(-100, 0.0));
  37.                 dPointVelocity.add(new Double());
  38.             }
  39.     }
  40.     public void start()
  41.     {
  42.         if(!running) // if the animation is not started...
  43.         {
  44.             new Thread(this).start();
  45.  
  46.             running = true; // prepare the variable responsible for doing accordingly
  47.         }
  48.     }
  49.     public void stop()
  50.     {
  51.         running = false; // stops the animation
  52.     }
  53.     public void run()
  54.     {
  55.         try
  56.         {
  57.             while(running)
  58.             {
  59.                 Thread.sleep(33);
  60.                 this.repaint();
  61.             }
  62.         }
  63.         catch(Exception e)
  64.         {
  65.             System.err.println(e.getStackTrace());
  66.             System.exit(1);
  67.         }
  68.     }
  69.  
  70.     public void update(Graphics g) // where rock stars live: and misbehave, horribly...
  71.     {
  72.             for(int i = 0; i < COUNT; i++) // addressing each ball's bouncability individually
  73.             {
  74.                     if((dPoint.get(i).x < -diameter) || (dPoint.get(i).x > rect.width)) // if a ball is currently between the two sides of the window (aka in sight)...
  75.                     {
  76.                         dPoint.set(i, new Double((double)randInt(0, rect.width), (double)randInt(-rect.height, 0))); // the x and y (random) position of each ball is set to their initial starting point
  77.                         /**
  78.                          * randInt is used to position each ball horizontally at some point between the two sides of the window.
  79.                          * The said function is then used to place the same ball vertically in space at some point above the window.
  80.                          * Such an action result in an uneven bounce motion of each of the balls.
  81.                          **/
  82.  
  83.                         dPointVelocity.set(i, new Double(dPointVelocity.get(i).x, 0.0)); // the vertical velocity of each ball is set to 0 so it will start to fall from its random position above the window
  84.  
  85.                         // Dear Mr. Arthur Griffith, I am officially what you would refer to as a 'Bad Ass Mofo'.
  86.  
  87.                             do{ // The sideways motion of the ball...
  88.                                 dPointVelocity.set(i, new Double((double)randInt(-2, 2), dPointVelocity.get(i).y));
  89.                             }while(dPointVelocity.get(i).x == 0.0);
  90.                     }
  91.  
  92.                     if(dPointVelocity.get(i).y > 0.0) // if the ball is moving down...
  93.                         if(((int)(dPoint.get(i).y) + diameter) > rect.height) // ...if it has reached the bottom...
  94.                         {
  95.                             dPointVelocity.set(i, new Double(dPointVelocity.get(i).x, -dPointVelocity.get(i).y)); // ...bounce the ball by reversing its direction
  96.                             dPointVelocity.set(i, new Double(dPointVelocity.get(i).x, (dPointVelocity.get(i).y * 0.95))); // the diminishing incessant bounce action
  97.                             // Word to earthlings: Ignore my logics, only sluts may dare.
  98.                             dPoint.set(i, new Double(dPoint.get(i).x, (double)(rect.height - diameter)));
  99.                         }
  100.  
  101.                 dPointVelocity.set(i, new Double(dPointVelocity.get(i).x, (dPointVelocity.get(i).y + gravity))); // the vertical velocity of the ball is adjusted by the pull of gravity
  102.                 /**
  103.                  * NOTE:
  104.                  * The horizontal speed of each ball is never adjusted.
  105.                  **/
  106.  
  107.                 dPoint.set(i, new Double((dPoint.get(i).x + dPointVelocity.get(i).x), (dPoint.get(i).y + dPointVelocity.get(i).y))); // the position of each ball is then adjusted by its two velocity components
  108.             } // Next ball...
  109.  
  110.             if(image == null)
  111.                 image = this.createImage(rect.width, rect.height); // the image of the action is created...
  112.  
  113.         this.paint(image.getGraphics()); // ...and passed on for painting
  114.         g.drawImage(image, 0, 0, this);
  115.     }
  116.  
  117.     public void paint(Graphics g)
  118.     {
  119.         g.setColor(Color.lightGray);
  120.         g.fillRect(0, 0, rect.width, rect.height); // ...fills the background with solid color
  121.  
  122.         g.setColor(new Color(0, 0, 128));
  123.  
  124.             for(int i = 0; i < COUNT; i++)
  125.                 g.fillOval((int)dPoint.get(i).x, (int)dPoint.get(i).y, diameter, diameter); // paints each ball in the array at its current x and y position
  126.     }
  127.  
  128.     private static int randInt(int low, int high) // Utility methods are placed at the bottom of the class only because they are the foundation of the code :)
  129.     {
  130.         return(((int)((high - low + 1) * (Math.random()))) + low);
  131.     }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment