Results 1 to 3 of 3
  1. #1
    sat27 is offline Member
    Join Date
    Nov 2009
    Rep Power

    Default Help required!!!!

    MultiThreadedBallWorld Class

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



    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 listeners for mouse clicks to frame

    addMouseListener(new BallListener());
    addWindowListener(new CloseQuit());
    //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[i] = new Ball(new Point(100, 15), 5);


    ballArray[i].setMotion(3.0+i, 6.0-i);

    // Create a new Thread to look after the ball referenced

    // by ballArray[i]

    ballThread[i] = new Thread(new BallThread(ballArray[i]));




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


    //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()))


    if ((pos.y < aBall.radius()) ||

    (pos.y > FrameHeight - aBall.radius()))



    // pause the execution of thread so that a mouse event

    // can be given time to run.

    try {


    } catch (InterruptedException e) {}




    private class CloseQuit extends WindowAdapter{
    public void windowClosing(WindowEvent 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[i].moveTo(e.getX(), e.getY());




    public void paint (Graphics g) {


    for (int i = 0; i < NoOfBalls; i++) {





    import java.awt.*;

    public class Ball{
    public Ball (Point lc, int r) { //constructor for new ball
    //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 =; //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.fillOval(loc.x-rad, loc.y-rad, 2*rad, 2*rad);

    1. I cant get the Balls at even positions in ballArray to rest once they reach or have passed through an invisible Horizontal line in the middle of the frame.

    2. i also need to change the colour of the balls: Balls at even positions in ballArray are displayed in blue; balls at odd positions in ballArray are displayed in red.

  2. #2
    Roamer is offline Member
    Join Date
    Dec 2009
    Rep Power


    Sat27, did u manage to get any of the above 2 questions right? I also need help with same questions :D....

  3. #3
    dinosoep is offline Senior Member
    Join Date
    Nov 2009
    Rep Power

Similar Threads

  1. help required
    By anny051 in forum New To Java
    Replies: 2
    Last Post: 07-31-2009, 04:05 AM
  2. what is required for EJB
    By mmc18 in forum Enterprise JavaBeans (EJB)
    Replies: 2
    Last Post: 07-15-2008, 06:16 PM
  3. Help required
    By Azndaddy in forum New To Java
    Replies: 0
    Last Post: 03-27-2008, 01:57 AM
  4. Blx.tld required
    By anki1234 in forum JavaServer Pages (JSP) and JSTL
    Replies: 5
    Last Post: 01-24-2008, 04:17 AM
  5. Urgent Help required!
    By WombRaider in forum New To Java
    Replies: 4
    Last Post: 01-10-2008, 11:26 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts