hi i am trying to make a bouncing ball program and have written the code for it which is below. i seem to have some problem with it
anybody help ???
the problem is i dnt seem to have the ball moving.
help please
code is as folllows ............
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.Ellipse2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Ball extends JFrame
{
private double x=2; // initial x axis
private double y=2; // initial y axis
private double width=16; // width
private double height=16; // height
private JPanel p;
private Color black;
private double dx=2; // incrfementing x by 2
private double dy=2; // incrementing y by 2
public Ball() throws InterruptedException
{
super();
setSize(450,450);
p=new JPanel();
p.setSize(400, 400);
p.setBounds(100, 100, 350, 350);
add(p,BorderLayout.CENTER);
for(int i=0;i<1000;i++)
{
move(p.getBounds());
p.setBounds(100, 100, 350, 350);
p.paintComponents(p.getGraphics());
Thread.sleep(10);
}
}
public static void main(
String[] args) throws InterruptedException {
Ball f = new Ball();
f.show();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
public void move(Rectangle bounds)
{
if(x<bounds.getMinX())
{
x=bounds.getMinX();
x+=dx; // incrementing the x axis
}
if(x>bounds.getMaxX())
{
x=bounds.getMaxX()-width;
x-=dx;
}
// within quadrants
if((x>bounds.getMinX()) && (x<bounds.getMaxX()))
{
// for quadrant 1 and quadrant 2
if((x<bounds.getCenterX()) && (x>bounds.getMinX()))
{
if((y>bounds.getMinY()) && (y<bounds.getCenterY()))
{
x-=dx;
}
if((y>bounds.getCenterY()) && (y<bounds.getMaxY()))
{
x+=dx;
}
}
// for quadrant 3 and 4
if((x>bounds.getCenterX()) && (x<bounds.getMaxX()))
{
// quadrant 4
if((y>bounds.getMinY()) && (y<bounds.getCenterY()))
{
x-=dx;
}
// quadrant 3
if((y>bounds.getCenterY()) && (y<bounds.getMaxY()))
{
x+=dx;
}
}
}
//////////////////////////////////////////////////////////
// y axis
////////////////////////////////////////////////////////
if(y<bounds.getMinY())
{
y=bounds.getMinY();
y+=dy; // incrementing the y-axis
}
if(y>bounds.getMaxY())
{
y=bounds.getMaxY()-height;
y-=dy;
}
// within the quadrant ofr y axis
if((y>bounds.getMinY()) && (y<bounds.getMaxY()))
{
// for quadrant 1 and 3
if((y<bounds.getCenterY()) && (y>bounds.getMinY()))
{
// for quadrant 1
if((x>bounds.getMinX()) && (x<bounds.getCenterX()))
{
y+=dy;
}
// for quadrant 3
if((x>bounds.getCenterX()) && (x<bounds.getMaxX()))
{
y-=dx;
}
}
// for quadrant 3 and 4
if((y>bounds.getCenterY()) && (y<bounds.getMaxY()))
{
// quadrant 4
if((x>bounds.getCenterX()) && (x<bounds.getMaxX()))
{
y-=dx;
}
// for quadrant 2
if((x>bounds.getMinX()) && (x<bounds.getCenterX()))
{
y+=dx;
}
}
}
}
public Ellipse2D getshape()
{
return (new Ellipse2D.Double(x, y, width, height));
}
public void paintComponents(Graphics g)
{
Graphics2D g2=(Graphics2D) g;
g2.setColor(Color.BLACK);
g2.fill(getshape());
}
private void setColor(Color BLACK) {
throw new UnsupportedOperationException("Not yet implemented");
}
}
[ October 28, 2008: Message edited by: Bear Bibeault ]