Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Physics class:
- package balls;
- import java.awt.Color;
- import java.awt.Component;
- import java.awt.Rectangle;
- import java.util.ArrayList;
- public class Physics extends Thread {
- private ArrayList<Ball> balls;
- private Component parent;
- private Component boundary;
- private long lastTime;
- private long sleepTime;
- private boolean done;
- public Physics(Component parent, Component boundary, int framesPerSecond) {
- this.parent = parent;
- this.boundary = boundary;
- sleepTime = 1000 / framesPerSecond;
- lastTime = System.currentTimeMillis();
- balls = new ArrayList<Ball>();
- done = false;
- start();
- }
- public synchronized void stopIt() {
- done = true;
- }
- private synchronized boolean getDone() {
- return done;
- }
- public synchronized void addBall(Ball ball) {
- balls.add(ball);
- }
- private synchronized void handleBalls() {
- long currentTime;
- currentTime = System.currentTimeMillis();
- for (int i = 0; i < balls.size(); i++)
- setBallPosition(currentTime - lastTime, balls.get(i));
- for (int i = 0; i < balls.size(); i++)
- for (int j = i + 1; j < balls.size(); j++)
- if (balls.get(i).intersectsBall(balls.get(j))) {
- balls.get(i).setCollided();
- balls.get(j).setCollided();
- }
- lastTime = currentTime;
- }
- private void setBallPosition(long timeDiff, Ball ball) {
- double deltaTime = timeDiff / 1000.0;
- double oldX = ball.getX();
- double oldY = ball.getY();
- double radius = ball.getRadius();
- double velocityX = ball.getVelocityX();
- double velocityY = ball.getVelocityY();
- double newX = oldX + velocityX * deltaTime;
- double newY = oldY + velocityY * deltaTime;
- Rectangle bounds = boundary.getBounds();
- ball.setX(newX);
- ball.setY(newY);
- if (newX - radius <= 0) {
- newX = 2 * radius - newX;
- ball.setVelocityX(Math.abs(velocityX));
- }
- if (newX + radius >= bounds.width) {
- newX = 2 * (bounds.width - radius) - newX;
- ball.setVelocityX(-Math.abs(velocityX));
- }
- if (newY - radius <= 0) {
- newY = 2 * radius - newY;
- ball.setVelocityY(Math.abs(velocityY));
- }
- if (newY + radius >= bounds.height) {
- newY = 2 * (bounds.height - radius) - newY;
- ball.setVelocityY(-Math.abs(velocityY));
- }
- }
- public void run() {
- while (!getDone()) {
- handleBalls();
- parent.repaint();
- try {
- sleep(sleepTime);
- } catch (InterruptedException e) {
- }
- }
- }
- }
- Ball class:
- package balls;
- import java.awt.Color;
- import java.awt.Graphics;
- public class Ball {
- private double x, y; // position
- private final double radius;
- private Color color;
- private double velocityX, velocityY;
- public Ball(final double x, final double y, final double radius,
- final Color color, final double velocityX, final double velocityY) { //added velx and vely... i'll remove if wrong
- this.x = x;
- this.y = y;
- this.radius = radius;
- this.color = color;
- // this.velocityX = this.velocityY = 0;
- this.velocityX = 10;
- this.velocityY = 10;
- } //Ball object
- public void draw(final Graphics g) {
- final double xAdjust = x - radius;
- final double yAdjust = y - radius;
- final double diameter = 2 * radius;
- g.setColor(color);
- g
- .fillOval((int) xAdjust, (int) yAdjust, (int) diameter,
- (int) diameter);
- } //draw()
- public double getX() {
- return x;
- } //getX()
- public double getY() {
- return y;
- } //getY()
- public double getRadius() {
- return radius;
- } //getX()
- public Color getColor() {
- return color;
- } //getColor()
- public double getVelocityX() {
- return velocityX;
- } //getVelocityX()
- public double getVelocityY() {
- return velocityY;
- } //getVelocityY()
- public void setX(double x) {
- this.x = x;
- } //setX()
- public void setY(double y) {
- this.y = y;
- } //setY()
- public void setColor(Color color) {
- this.color = color;
- } //setColor()
- public void setVelocityX(double velocityX) {
- this.velocityX = velocityX;
- } //setVelocityX()
- public void setVelocityY(double velocityY) {
- this.velocityY = velocityY;
- } //setVelocityY()
- public boolean intersectsBall(Ball b) {
- double x1 = b.getX();
- double y1 = b.getY();
- double rad1 = b.getRadius();
- double x2 = x;
- double y2 = y;
- double rad2= radius;
- double distance = Math.sqrt(((y2-y1)*(y2-y1))-(((x2-x1)*x2-x1)));
- if (distance == ( rad1+(rad2)) )
- return true;
- else return false;
- } //intersectsBall()
- public void setCollided() {
- this.setColor(Color.red);
- } //setCollided()
- } //Ball class
- BallsApplet class:
- // Brock Haun
- // HELP! Nothing is working for part 3. I did everything, but no actual balls are produced on the applet.
- package balls;
- import java.awt.Color;
- import java.awt.Graphics;
- import java.awt.Rectangle;
- import java.util.Random;
- import javax.swing.JApplet;
- public class BallsApplet extends JApplet {
- private int numBalls = 20; //just for private usage. makes code more readable.
- private Ball balls[] = new Ball[numBalls];
- private boolean stop = false; //private usage
- private Physics physics = new Physics(this,this,10);
- @Override
- public void init() {
- setSize(400, 500);
- //ball = new Ball(30, 30, 10, Color.GREEN);
- for (int i=0;i<numBalls;i++) {
- balls[i]= makeRandomBall();
- physics.addBall(balls[i]);
- } //for
- //physics.addBall(ball);
- } //init()
- @Override
- public void paint(Graphics g) {
- Rectangle bounds = getBounds();
- g.setColor(Color.lightGray);
- g.fillRect(0, 0, bounds.width, bounds.height);
- int redBalls = 0;
- int greenBalls = 0;
- for (int i=0;i<numBalls;i++) {
- balls[i].draw(g);
- if (balls[i].getColor().equals(Color.red))
- redBalls++;
- else greenBalls++;
- } //for
- //string output
- String greenCount = "Green balls: " + greenBalls;
- String redCount = "Red balls: " + redBalls;
- g.drawString(greenCount, 20, 20);
- g.drawString(redCount, 20, 30);
- g.drawString("Brock Haun" , 300, 10);
- if (greenBalls == 0)
- physics.stopIt();
- } //paint()
- private int randomRange(int low, int high) {
- Random r = new Random();
- int randomInt = r.nextInt(high)+ low;
- return randomInt;
- } //randomRange()
- private Ball makeRandomBall() {
- Random r = new Random();
- int randX = r.nextInt(180)+21; //instructions say between 20 and 200, not inclusive so ceiling=179,floor=21. rand starts at 0.
- int randY = r.nextInt(180)+21;
- int randRad = r.nextInt(7)+3; //same as before, but BETWEEN 2 and 10, so 3-9
- //making random values for x and y is a 2 step process since we can't declare a negative range.
- //first we make a random int (0 or 1); then we take -1 and square it by the random int (0 or 1). if it is 0,
- //the sign won't change (stay positive). if it is 1, the sign will become negative.
- int sign = r.nextInt(2);
- double randVelX = (Math.pow(-1, sign)*(r.nextInt(41)));
- double randVelY = (Math.pow(-1, sign)*(r.nextInt(41)));
- Ball randomBall = new Ball(randX,randY,randRad,Color.green, randVelX, randVelY);
- return randomBall;
- } //makeRandomBall()
- } //BallsApplet class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement