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

    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(Color.red);

    world.setVisible(true);

    }



    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].setColor(ballColor);

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

    ballThread[i].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(40);

    } catch (InterruptedException e) {}



    }

    }

    }


    private class CloseQuit extends WindowAdapter{
    public void windowClosing(WindowEvent e){
    System.exit(0);
    }
    }

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

    super.paint(g);

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

    ballArray[i].paint(g);

    }

    }

    }











    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 = Color.blue; //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);
    }
    }



    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
    Posts
    1
    Rep Power
    0

    Default

    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
    Posts
    150
    Rep Power
    5

Similar Threads

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

Posting Permissions

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