Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MultiBallThread

Status
Not open for further replies.

bhavm38

Programmer
Oct 25, 2005
2
GB
I am trying to create a multi threaded ball world in which i have 10 balls in a thread. So far i have a working model but all i need to do now is to get the even numbered balls that is the ball controlled by threads 0,2,4,6,and 8 to stop moving when they hit they intersect with a line in the middle of the frame. I have tried several different methods and am really struggling. I would appreciate any help you may be able to give. The code i have produced so far is below. I know i need to modify the class Ballthread but struggling quite a bit on this.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MultiThreadedBallWorld extends JFrame{

public static void main (String args[]){
MultiThreadedBallWorld world = new MultiThreadedBallWorld(Color.red);
world.show();
}

public static final int FrameWidth = 600;
public static final int FrameHeight = 400;

//array to hold balls
private Ball [ ] ballArray;
private static final int NoOfBalls = 10;
//array to hold references to threads
private Thread [ ] ballThread;
//a variable to hold count of hits
private int hits;
//a label to display number of hits
private Label hitsLabel;

private MultiThreadedBallWorld(Color ballColor) {
setSize(FrameWidth, FrameHeight);
setTitle("Multi Threaded Ball World");
//add listener for mouse clicks to frame
addMouseListener(new BallListener());
//create arrays for balls and threads
ballArray = new Ball[NoOfBalls];
ballThread = new Thread[NoOfBalls];
//create balls and threads and add to arrays
for (int i = 0; i < NoOfBalls; i++) {
ballArray = new Ball(new Point(100, 15), 5);
ballArray.setColor(ballColor);
ballArray.setMotion(3.0+i, 6.0-i);

// Create a new Thread to look after the ball referenced
// by ballArray
ballThread = new Thread(new BallThread(ballArray));
ballThread.start();
}
}

private class BallThread implements Runnable {
//the action taken by the thread that controls the ball, run
//overrides run in Thread.
Ball aBall;
public BallThread (Ball b) { aBall = b; }
public void run() {
while (true) {
aBall.move();
//check whether ball intersects boundary and,
//if so, change direction of motion

Point pos = aBall.location();
if ((pos.x < aBall.radius()) ||
(pos.x > FrameWidth - aBall.radius()))
aBall.reflectHorz();
if ((pos.y < aBall.radius()) ||
(pos.y > FrameHeight - aBall.radius()))
aBall.reflectVert();
repaint();

// pause the execution of thread so that a mouse event
// can be given time to run.
try {
Thread.sleep(50);
} catch (InterruptedException e) {}
}
}
}
private class BallListener extends MouseAdapter {
//action to be taken when mouse is clicked
public void mousePressed (MouseEvent e) {
//move all balls to position specified by
//the position of mouse cursor when
//mouse is clicked
for (int i = 0; i < NoOfBalls; i++) {
ballArray.moveTo(e.getX(), e.getY());
}
}
}
public void paint (Graphics g) {
super.paint(g);
for (int i = 0; i < NoOfBalls; i++) {
ballArray.paint(g);
}
}
}



The ball class is as follows


//a generic round colored object that moves

import java.awt.*;

public class Ball{

//constructor for new ball
public Ball (Point lc, int r)
{

//ball centre at point loc, radius rad
loc = lc;
rad = r;
}

protected Point loc; //position in window
protected int rad; //radius of ball
protected double changeInX = 0.0; //horizontal change in ball position in one cycle
protected double changeInY = 0.0; //vertical change in ball position in one cycle
protected Color color = Color.red; //colour of ball

//methods that set attributes of ball
public void setColor(Color newColor) {color = newColor;}
public void setMotion(double dx,double dy)
{changeInX = dx; changeInY = dy;}

//methods that access attributes of ball
public int radius() {return rad;}
public Point location() {return loc;}

//methods to reverse motion of the ball
public void reflectVert(){ changeInY = -changeInY; }
public void reflectHorz(){ changeInX = -changeInX; }

//methods to move the ball
public void moveTo(int x, int y) {loc.move(x,y);}
public void move(){loc.translate((int)changeInX, (int)changeInY);}

//method to display ball
public void paint (Graphics g) {
g.setColor(color);
g.fillOval(loc.x-rad, loc.y-rad, 2*rad, 2*rad);
}
}
 
Interesting,

I just wrote my own version of this for MIDP using java microedition so it can run on a palm pilot. In the ball parameters I allow the screen height and width to be adjust and I check to see if the ball's X and Y have reached that limit if they have I reverse their motion or change their trajectories. You can do the same thing, "If this ball has reached my line limit, reverse direction (Meaning, starting subtraction from X or Y so the balls move backwards)

-Ron

typedef map<GiantX,gold, less<std::shortestpathtogold> > AwesomeMap;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top