Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*==================================================================================================
- --------------------------------------------------------------------------------------------------
- [GraviBounce.java]
- A real-time simulation of bouncing balls due to the effect of gravity on them.
- (c) Damion 'Phr0z3n.dev' Tapper, 2015.
- Email: [email protected]
- Website: L337Stuff.Blogspot.com
- --------------------------------------------------------------------------------------------------
- ==================================================================================================*/
- import java.applet.Applet;
- import java.awt.Rectangle;
- import java.awt.geom.Point2D.Double;
- import java.util.ArrayList;
- import java.awt.Graphics;
- import java.awt.Image;
- import java.awt.Color;
- public class GraviBounce extends Applet implements Runnable
- {
- private Rectangle rect;
- private final int COUNT = 10;
- private ArrayList<Double> dPoint = new ArrayList<Double>(COUNT); // for regular arrays choke on my big fat... ambitions. ;)
- private ArrayList<Double> dPointVelocity = new ArrayList<Double>(COUNT);
- private boolean running = false;
- private final static int diameter = 10; // a constant initial value which cannot be changed at runtime
- private final static double gravity = 0.3; // "
- private Image image = null;
- public void init()
- {
- rect = this.getBounds();
- for(int i = 0; i < COUNT; i++)
- {
- dPoint.add(new Double(-100, 0.0));
- dPointVelocity.add(new Double());
- }
- }
- public void start()
- {
- if(!running) // if the animation is not started...
- {
- new Thread(this).start();
- running = true; // prepare the variable responsible for doing accordingly
- }
- }
- public void stop()
- {
- running = false; // stops the animation
- }
- public void run()
- {
- try
- {
- while(running)
- {
- Thread.sleep(33);
- this.repaint();
- }
- }
- catch(Exception e)
- {
- System.err.println(e.getStackTrace());
- System.exit(1);
- }
- }
- public void update(Graphics g) // where rock stars live: and misbehave, horribly...
- {
- for(int i = 0; i < COUNT; i++) // addressing each ball's bouncability individually
- {
- 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)...
- {
- 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
- /**
- * randInt is used to position each ball horizontally at some point between the two sides of the window.
- * The said function is then used to place the same ball vertically in space at some point above the window.
- * Such an action result in an uneven bounce motion of each of the balls.
- **/
- 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
- // Dear Mr. Arthur Griffith, I am officially what you would refer to as a 'Bad Ass Mofo'.
- do{ // The sideways motion of the ball...
- dPointVelocity.set(i, new Double((double)randInt(-2, 2), dPointVelocity.get(i).y));
- }while(dPointVelocity.get(i).x == 0.0);
- }
- if(dPointVelocity.get(i).y > 0.0) // if the ball is moving down...
- if(((int)(dPoint.get(i).y) + diameter) > rect.height) // ...if it has reached the bottom...
- {
- dPointVelocity.set(i, new Double(dPointVelocity.get(i).x, -dPointVelocity.get(i).y)); // ...bounce the ball by reversing its direction
- dPointVelocity.set(i, new Double(dPointVelocity.get(i).x, (dPointVelocity.get(i).y * 0.95))); // the diminishing incessant bounce action
- // Word to earthlings: Ignore my logics, only sluts may dare.
- dPoint.set(i, new Double(dPoint.get(i).x, (double)(rect.height - diameter)));
- }
- 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
- /**
- * NOTE:
- * The horizontal speed of each ball is never adjusted.
- **/
- 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
- } // Next ball...
- if(image == null)
- image = this.createImage(rect.width, rect.height); // the image of the action is created...
- this.paint(image.getGraphics()); // ...and passed on for painting
- g.drawImage(image, 0, 0, this);
- }
- public void paint(Graphics g)
- {
- g.setColor(Color.lightGray);
- g.fillRect(0, 0, rect.width, rect.height); // ...fills the background with solid color
- g.setColor(new Color(0, 0, 128));
- for(int i = 0; i < COUNT; i++)
- 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
- }
- 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 :)
- {
- return(((int)((high - low + 1) * (Math.random()))) + low);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment