Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 1 of 1

Thread: Boucing ball from stanford uni

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Boucing ball from stanford uni

    hi

    This code was written using the API library. I am only in my second year and would like some help using jdk equivalents to pause, Goval to add and remove a shape and a random colour generator. here is the java file, it requires some tweaking. acm.jar has been attached in the zip so you can see what Ive done so far. However Acm is now disfunct.


    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */
    package bouncingball;

    import acm.graphics.*;
    import acm.program.*;
    import acm.util.RandomGenerator;
    import java.awt.Color;
    import java.awt.event.*;



    /**
    *
    * @author Kuser
    */
    public class BouncingBall extends GraphicsProgram implements Runnable
    {

    private static final int DIAM_BALL = 30;
    private static final double GRAVITY = 3;
    private static final int DELAY = 50;
    private static final double X_START = DIAM_BALL/2;
    private static final double Y_START = 360;
    private static final double X_VEL = 5;
    private static final double BOUNCE_REDUCE = 0.85;

    private double xVel = X_VEL;
    private double yVel = 0.0;
    private GOval ball;



    /**
    * @param args the command line arguments
    */

    public void run()
    {
    // TODO code application logic here
    addMouseListeners();
    setup();
    while (ball.getX() < getWidth())
    {
    moveBall();
    checkForCollision();
    pause(DELAY);
    }

    }

    public void mouseClicked(MouseEvent e)
    {
    RandomGenerator randomColor = new RandomGenerator();
    ball.setColor(Color.white);
    ball = new GOval(X_START, Y_START, DIAM_BALL, DIAM_BALL);
    ball.setFilled(true);
    ball.setColor(randomColor.nextColor());
    add(ball, e.getX(), e.getY());

    }

    public void setup()
    {
    ball = new GOval(X_START, Y_START, DIAM_BALL, DIAM_BALL);
    ball.setFilled(true);
    ball.setColor(Color.magenta);
    add(ball);
    }


    public void moveBall()
    {
    yVel += GRAVITY;
    ball.move(xVel, yVel);
    }

    public void checkForCollision()
    {
    if (ball.getY() > getHeight() - DIAM_BALL)
    {
    yVel = -yVel * BOUNCE_REDUCE;

    double diff = ball.getY() - (getHeight() - DIAM_BALL);
    ball.move(0, -2*diff);
    }
    }
    }
    Attached Files Attached Files


Similar Threads

  1. java ceaser cipher stanford lectures
    By forms in forum What's Wrong With My Code?
    Replies: 16
    Last Post: July 25th, 2011, 05:23 PM
  2. Cant get the ball to bounce around the canvass
    By RajivdeCosta in forum What's Wrong With My Code?
    Replies: 16
    Last Post: June 30th, 2011, 10:18 AM
  3. Bouncing Ball Program Random Color Change
    By coderEvolution in forum What's Wrong With My Code?
    Replies: 10
    Last Post: March 3rd, 2011, 03:01 PM
  4. Need help with a third ball in game.
    By vlan in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 12th, 2010, 03:35 PM