Hello;

Well, I need to create a program that randomly inserts new different sized spheres into a swing panel and animates them [movement is delimited by the width and height of the frame - when they collide with the frame they bounce to a random direction].
How could I accomplish this tedious task?
With swing timers or thread pools ?
So I need to create a class that describes the sphere [its speed, dimensions, movement] and call the class when a certain criteria is satisfied [for the random insertion]. Any kind help would be great, because Im stuck:)

Joey

Here's an overview of a typical way to do this:
Create the spheres as instances of a Sphere class- each Sphere with its own size, position, velocity. Maintain a list of all the spheres you have created. In your main method start a javax.swing.Timer to call an update method on each sphere in the list every n millisecs (eg 60 mSec) - Sphere's update method updates the position and velocity appropriately. After calling all the update methods call repaint() for the panel. This ensures that the physics of moving and bouncing etc proceeds in real time.
In the panel's paintComponent method call a draw method for each sphere. Sphere's draw method just draws the sphere at its latest position. This ensures that the panel stays up to date, but doesn't interfere with the physics if there are extra or skipped repaints (eg if the window is moved or resized etc etc).

Here's an overview of a typical way to do this:
Create the spheres as instances of a Sphere class- each Sphere with its own size, position, velocity. Maintain a list of all the spheres you have created. In your main method start a javax.swing.Timer to call an update method on each sphere in the list every n millisecs (eg 60 mSec) - Sphere's update method updates the position and velocity appropriately. After calling all the update methods call repaint() for the panel. This ensures that the physics of moving and bouncing etc proceeds in real time.
In the panel's paintComponent method call a draw method for each sphere. Sphere's draw method just draws the sphere at its latest position. This ensures that the panel stays up to date, but doesn't interfere with the physics if there are extra or skipped repaints (eg if the window is moved or resized etc etc).

Could you please explain the Sphere's update method? :)

Each sphere has a position and a velocity. The update method is called at regular intervals. Every time the update method is called you update the position according to the velocity. The you check to see if you've hit the boundary, in which case you have to change the velocity to represent the new direction. So regardless of what's going on with the panel, you are maintaining the position and velocity correctly all the time.

Each sphere has a position and a velocity. The update method is called at regular intervals. Every time the update method is called you update the position according to the velocity. The you check to see if you've hit the boundary, in which case you have to change the velocity to represent the new direction. So regardless of what's going on with the panel, you are maintaining the position and velocity correctly all the time.

That sounds rather complicating. Well, better now then never I guess. Im gonna try to do that and post back with further problems.

Yes, just get stuck in! The whole update method is maybe 20 easy lines of code.

Yes, just get stuck in! The whole update method is maybe 20 easy lines of code.

Well it shows only one sphere out of 10 :(
But it does move..

package sphere.it.moves;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.Random;
import javax.swing.Timer;

public class Start{

	private static final long serialVersionUID = 8945195249313614872L;

	private static final int WINDOW_WIDTH=800;
	private static final int WINDOW_HEIGHT=600;

	Random rand = new Random();
	private Timer timer;
	private Image sphere;
	private double position;
	private double velocity;
	
private double radius;

	public Start()
	{
		int i =0;
		final List<Sphere> spheres=SphereHelper.setSpheres(); //my list of spheres

		timer = new Timer(60,new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {

				for(int i =0;i<spheres.size();i++) //for each sphere get &set their parametars
				{
					 velocity=spheres.get(i).getVelocity();
					 radius=spheres.get(i).getRadius();
					 spheres.get(i).setPosition(spheres.get(i).getPosition()+velocity);

					 if (spheres.get(i).getPosition()-radius < 0)
					 {
						 spheres.get(i).setVelocity(-velocity);
						 spheres.get(i).setPosition(radius);
					 }
					 else if (spheres.get(i).getPosition()  > WINDOW_WIDTH)
					 {
						 spheres.get(i).setVelocity(-velocity);
						 spheres.get(i).setPosition(WINDOW_WIDTH);
					 }
					 sphere=spheres.get(i).getSphImg(); //place the current sphere image
					 position =spheres.get(i).getPosition(); and position here
					 repaint();
				 }
			 }
		});
		timer.start();
	}

	public void paintComponent(Graphics graph) {   //so that I could repaint them here
		super.paintComponent(graph);
		g.drawImage(sphere,(int)(position-radius),450,null);
	}
}

In your code to move the spheres you have (correctly)
for(int i =0;i<spheres.size();i++) //for each sphere ...

but in paintComponent you have
g.drawImage(sphere,... (one sphere only)

in paintComponent you need to paint all the spheres.

In your code to move the spheres you have (correctly)
for(int i =0;i<spheres.size();i++) //for each sphere ...

but in paintComponent you have
g.drawImage(sphere,... (one sphere only)

in paintComponent you need to paint all the spheres.

How could I do that? Do I need to create a container, and repaint that?

If that is painting 1 sphere OK, then you need to put in a loop like you did for the movement, and paint each sphere one at a time in the loop. I can't say more becuase I don't have all the code, and the structure is not clear.
ps: I'm going offline now, so good luck.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.