The Artima Developer Community
Sponsored Link

Java Answers Forum
Increasing ball speed & changing position of start - pls help!!

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Sairah

Posts: 2
Nickname: javanovice
Registered: May, 2002

Increasing ball speed & changing position of start - pls help!! Posted: May 8, 2002 11:43 PM
Reply to this message Reply
Advertisement
I have th following sources code for a class called MultiThreadedWorld. I need to modifiy it so that when the mouse is clicked, the balls are restarted from the top left hand corrner of the window. I suppose this means modifying the line:
ballArray.moveTo(e.getX(), e.getY());

Also, I need to modify this code so that when the mouse is clicked, the speed of the balls doubles from 100 to 50 as in the line:
Thread.sleep(100);

Below is the source code for the class MultiThreadedWorld:


//A multi-bouncing ball animation controlled by threads with

//count of number of hits against boundary



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

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

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

aBall.reflectHorz();

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

}

}

}


I would appreciate any help on this. Thanks

Topic: Telephone book input program Previous Topic   Next Topic Topic: EJB code generation

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use