Results 1 to 5 of 5

Thread: bouncing Balls

  1. #1
    mystery81 is offline Member
    Join Date
    Sep 2012
    Posts
    3
    Rep Power
    0

    Default bouncing Balls

    Hi !

    I am new to Java and trying to write a program with a few bouncing balls ! after running the application i only get one ball bouncing. here is the code any idea what 2 do in order to have all the balls in the panel ?

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;

    /**
    * BouncePanel represents a canvas (drawing area) for objects of type Ball. A
    * timer-object periodically updates the canvas (in the run method), by moving
    * and repainting the objects.
    */

    public class BouncePanel extends JPanel implements ActionListener,
    MouseMotionListener {

    private int width = 1800, height = 900;
    private Ball[] theBall ;
    private Rectangle pad; // The to bounce of the ball

    private Timer timer;

    public BouncePanel() {
    this.setPreferredSize(new Dimension(width, height));
    this.setBackground(Color.lightGray);
    //Rectangle bounds =new Rectangle (0,0,width ,height);


    // Create and initialize a ball
    theBall=new Ball[4];
    for (int k=0 ; k<theBall.length ;k++){

    theBall[k] = new Ball(4 ,4 ,6 , Color.green);
    theBall[k].setVelocity(15, 15);
    }
    // Create the pad
    pad = new Rectangle(width / 2, height - 40, 40, 5);

    // Initialize event handling (mouse motion)
    this.addMouseMotionListener(this);

    }

    public void startAnimation() {
    // Create, and start, the timer-object responsible for the animation
    timer = new Timer(10, this); // Signal every 10 milliseconds
    // (actionPerformed is called)
    timer.start();
    }

    /**
    * Update the position of the ball and repaint. This method is called each
    * time the Timer produces an event.
    */
    public void actionPerformed(ActionEvent event) {
    // Is the ball outside the panel?
    for (int k=0 ; k<theBall.length ;k++){
    constrainBall(theBall[k]);
    // Move the ball
    theBall[k].move();
    // Check ball against the pad
    checkForCollisionWithPad(theBall[k]);
    // Draw the ball (request to call paintComponent(g))
    repaint();
    }

    }
    /**
    * Define what to draw. Called by repaint().
    */
    public void paintComponent(Graphics g) {
    super.paintComponent(g);

    // The pad
    g.setColor(Color.white);
    g.fillRect(pad.x, pad.y, pad.width, pad.height);
    for (int k=0 ; k<theBall.length ; k++){
    // The ball
    theBall[k].paint(g);
    }
    }
    /**
    * Called when the mouse is dragged (left button down)
    */
    public void mouseDragged(MouseEvent e) {
    Point point = e.getPoint();

    // Keep the pad inside the panel
    if (point.x < 0) {
    pad.x = 0;
    } else if (point.x > this.getWidth() - pad.width) {
    pad.x = this.getWidth() - pad.width;
    } else {
    pad.x = point.x;
    }
    }

    public void mouseMoved(MouseEvent e) {
    // Do nothing
    }

    /**
    * Check if the ball collides with the pads upper edge. If so, bounce the
    * ball vertically.
    */
    private void checkForCollisionWithPad(Ball ball) {
    double radius = ball.getRadius();
    double x1 = ball.getX(), y1 = ball.getY();
    double dx = ball.getDx(), dy = ball.getDy();
    double y2 = y1 + ball.getDy();
    // Does the line between the current and the next position of the ball
    // intersect the upper edge of the pad? If so - bounce off.
    if ((y1 + radius <= pad.y && y2 + radius >= pad.y)
    || (y2 - radius <= pad.y && y1 - radius >= pad.y)) {
    double intersectX = x1 - dx * (y1 - pad.y) / dy;
    if (intersectX >= pad.x && intersectX <= pad.x + pad.width) {
    ball.setVelocity(dx, -dy);
    }
    }
    }

    /**
    * Keep the ball inside the panel/canvas
    */
    private void constrainBall(Ball ball) {
    double x = ball.getX(), y = ball.getY();
    double dx = ball.getDx(), dy = ball.getDy();
    double radius = ball.getRadius();

    // If outside the box - calculate new dx and dy
    if (x < radius)
    dx = Math.abs(dx);
    else if (x > width - radius)
    dx = -Math.abs(dx);
    if (y < radius)
    dy = Math.abs(dy);
    else if (y > height - radius)
    dy = -Math.abs(dy);

    ball.setVelocity(dx, dy);
    }

    private static final long serialVersionUID = 1L;
    }







    import java.awt.Color;
    import java.awt.Graphics;


    /**
    * A representation of a ball moving inside a box. The method paint(Graphics g)
    * makes it possible to draw a ball in a graphical environment.
    */

    public class Ball {

    private double x, y; // position of center
    private double dx, dy; // velocity
    private double radius;
    private Color color;
    //private Rectangle box;
    public Ball(double x0, double y0, double r ,Color col ) { //#
    x = x0;
    y = y0;
    radius = 10;
    color = col;
    }

    public void setColor (Color col){

    color = col ;

    }


    //public void setBoundingBox(Rectangle box_){
    // box = box_;




    public double getX() {
    return x;
    }

    public double getY() {
    return y;
    }

    public double getRadius() {
    return radius;
    }

    public double getDx() {
    return dx;
    }

    public double getDy() {
    return dy;
    }

    public void setVelocity(double dx_, double dy_) {
    dx = dx_;
    dy = dy_;
    }

    /**
    * Move the ball one step (dx, dy)
    */
    public void move() {
    x += dx;
    y += dy;
    }

    /**
    * Draw the ball on the screen
    */
    public void paint(Graphics g) {
    g.setColor(color);
    g.fillOval((int) (x - radius), (int) (y - radius), (int) (2 * radius),
    (int) (2 * radius));
    }
    }



    import javax.swing.*;

    /**
    * Main creates a window (JFrame) containing the BouncePanel.
    */

    public class Juggler {
    public static void main(String[] args) {
    // Create a window (frame)
    JFrame frame = new JFrame("The Juggler");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.setResizable(false);

    // Create a canvas (BouncePanel) and add it to the window.
    BouncePanel panel = new BouncePanel();
    frame.add(panel);
    frame.pack();

    frame.setVisible(true);
    panel.startAnimation();
    }
    }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,762
    Rep Power
    25

    Default Re: bouncing Balls

    Pleae edit your post and wrap the code in code tags. See: BB Code List - Java Programming Forum
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    JavaAdviser is offline Member
    Join Date
    Sep 2012
    Posts
    26
    Rep Power
    0

    Default Re: bouncing Balls

    All the balls are in the panel. :)

    The thing is, that they are perfectly overlapping.

    I've edited the move() method in the Ball class like this.
    Java Code:
    /**
       * Move the ball one step (dx, dy)
       */
      public void move() {
        x += dx + (int) (Math.random() * 10);
        y += dy + (int) (Math.random() * 10);
      }
    and then you will see multiple balls.

  4. #4
    mystery81 is offline Member
    Join Date
    Sep 2012
    Posts
    3
    Rep Power
    0

    Default Re: bouncing Balls

    Thank u sir , may the lord bless u with new methods !!! :D

  5. #5
    mystery81 is offline Member
    Join Date
    Sep 2012
    Posts
    3
    Rep Power
    0

Similar Threads

  1. More bouncing balls...
    By ShaolinMunky in forum New To Java
    Replies: 5
    Last Post: 06-10-2012, 10:43 AM
  2. Replies: 2
    Last Post: 02-13-2012, 07:45 AM
  3. ArrayList and Bouncing Balls
    By jamie23 in forum AWT / Swing
    Replies: 1
    Last Post: 02-20-2011, 07:54 PM
  4. Multiple bouncing balls
    By Algar in forum AWT / Swing
    Replies: 2
    Last Post: 04-24-2008, 09:35 PM
  5. Movement of balls
    By BlitzA in forum New To Java
    Replies: 8
    Last Post: 01-09-2008, 04:30 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •