This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help needed with the Code

 
kartik rathore
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
pete stein
Bartender
Posts: 1561
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please have a look here:
Ease Up
Use Code Tags
 
pete stein
Bartender
Posts: 1561
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic