Results 1 to 5 of 5
Thread: bouncing Balls
- 09-13-2012, 11:37 PM #1
Member
- Join Date
- Sep 2012
- Posts
- 3
- Rep Power
- 0
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();
}
}
- 09-13-2012, 11:46 PM #2
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.
- 09-13-2012, 11:57 PM #3
Member
- Join Date
- Sep 2012
- Posts
- 26
- Rep Power
- 0
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.
and then you will see multiple balls.Java Code:/** * Move the ball one step (dx, dy) */ public void move() { x += dx + (int) (Math.random() * 10); y += dy + (int) (Math.random() * 10); }
- 09-14-2012, 12:05 AM #4
Member
- Join Date
- Sep 2012
- Posts
- 3
- Rep Power
- 0
Re: bouncing Balls
Thank u sir , may the lord bless u with new methods !!! :D
- 09-14-2012, 12:16 AM #5
Member
- Join Date
- Sep 2012
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
More bouncing balls...
By ShaolinMunky in forum New To JavaReplies: 5Last Post: 06-10-2012, 09:43 AM -
i can not see balls bouncing and background is not black please help!|
By aaminamirza in forum AWT / SwingReplies: 2Last Post: 02-13-2012, 06:45 AM -
ArrayList and Bouncing Balls
By jamie23 in forum AWT / SwingReplies: 1Last Post: 02-20-2011, 06:54 PM -
Multiple bouncing balls
By Algar in forum AWT / SwingReplies: 2Last Post: 04-24-2008, 08:35 PM -
Movement of balls
By BlitzA in forum New To JavaReplies: 8Last Post: 01-09-2008, 03:30 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks