Help needed with the Code

kartik rathore
Greenhorn
Posts: 14
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.

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);

for(int i=0;i<1000;i++)
{
move(p.getBounds());
p.setBounds(100, 100, 350, 350);
p.paintComponents(p.getGraphics());
}
}

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;
}
if((x>bounds.getMinX()) && (x<bounds.getMaxX()))
{
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()))
{
if((y>bounds.getMinY()) && (y<bounds.getCenterY()))
{
x-=dx;
}
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()))
{
if((x>bounds.getMinX()) && (x<bounds.getCenterX()))
{
y+=dy;
}
if((x>bounds.getCenterX()) && (x<bounds.getMaxX()))
{
y-=dx;
}
}
// for quadrant 3 and 4
if((y>bounds.getCenterY()) && (y<bounds.getMaxY()))
{
if((x>bounds.getCenterX()) && (x<bounds.getMaxX()))
{
y-=dx;
}
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 ]

pete stein
Bartender
Posts: 1561
Ease Up
Use Code Tags

pete stein
Bartender
Posts: 1561
Next, you would benefit by reading up on a few things:

1) How to draw graphics in Swing. This often involves overriding the paintComponent method of a JPanel (your panel "p" would do nicely here) and drawing inside of this method using the Graphics object passed to the method by the JVM. You would likely change your ball positions and then call p.repaint() to update the image. There are several excellent tutorials on this in the Sun Swing and Graphics tutorial pages.

2) How to do animation. Simple animations would best be coded using a Swing Timer (javax.swing.Timer). Calling Thread.sleep() on the event dispatch thread as you are doing is only going to be guaranteed to put your whole app to sleep for x seconds (here for 10 seconds).

Good luck!

Craig Wood
Ranch Hand
Posts: 1535