Can't simultaneously run two threads??
Hey Guys,
I am creating a program which creates two balls (each with their own runnables which control them). This is the important code from the file:
Code :
public BallsFrame() {
setSize(FRAME_WIDTH, FRAME_HEIGHT);
addBall(150,50);
addBall(200,100);
// add two balls to pane
}
// addBall to panel at specified coordinates (x,y)
public void addBall(int x, int y) {
// create coloured ball and add to panel
BallComponent ball = new BallComponent(x, y);
add(ball);
// create a runnable to control ball
Runnable r = new ColoredBallRunnable(ball);
Thread t = new Thread(r);
System.out.println("addBall t started...");
t.start();
System.out.println("addBall t finished...");
}
The ControlBallRunnable is here:
Code :
public class ColoredBallRunnable implements Runnable {
BallComponent ball;
ColoredBallPanel cbPanel;
public ColoredBallRunnable(BallComponent ball){//, ColoredBallPanel cbPanel){
this.ball = ball;
this.cbPanel = cbPanel;
}
public void run() {
class TimerListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
ball.animate();
}
}
TimerListener listener = new TimerListener();
Timer t = new Timer(1000, listener);
t.start();
}
}
To explain this: when the runnable is created, it creates a timer which changes the colour of the ball (using method ball.animate() ) every 1000ms i.e. every second.
Now my problem is this code partially works, it successfully creates one ball (the first time addBall() is called).
It refuses to create more than one ball.
Now, ive researched this problem for the last couple of days and cant seem to find a solution. Please help me. If you need any clarifications, extra code etc, i will provide it.
Re: Can't simultaneously run two threads??
For testing this kind of problem you need a program that compiles, executes and shows the problem.
Please make a small program for testing and post it.
Re: Can't simultaneously run two threads??
Quote:
Originally Posted by
Norm
For testing this kind of problem you need a program that compiles, executes and shows the problem.
Please make a small program for testing and post it.
Hi i tried to make a small version of the program but i couldnt change classes into methods etc, as i am quite new at java. Heres a project folder from netbeans which contains all the source.
Sorry for the inconvenience caused, but this is the closest i can get. Thanks
http://www.filedropper.com/ballsthreads
Re: Can't simultaneously run two threads??
Please post the code here on the forum.
Re: Can't simultaneously run two threads??
Sorry about that. Here is the various pages of the program:
Re: Can't simultaneously run two threads??
Sorry, having 5 separate files makes it hard to copy for testing.
Can you put all the source into a single file to make it easier to copy and paste into my editor for testing?
I got it.
I get this error:
Quote:
cannot find symbol
symbol : class ColoredBallPanel
Quote:
Now my problem is this code partially works, it successfully creates one ball (the first time addBall() is called).
It refuses to create more than one ball.
What is supposed to cause it to create a second ball?
Re: Can't simultaneously run two threads??
Hi i think i found out where i was going wrong. I was adding the component directly to the frame (refer to BallsFrame.java). I think you can only add one component directly to a frame.
Re: Can't simultaneously run two threads??
Yes that's it. Make these changes to see:
Code :
setSize(FRAME_WIDTH, FRAME_HEIGHT);
setLayout(new GridLayout(4,1)); //<<<<<<<<<<<<<<<< ADDED
addBall(10,50);
addBall(200,45);
Re: Can't simultaneously run two threads??
It works!!! BTW i was trying out FlowLayout before you suggested GridLayout, and none of the components were showing on the frame. Do you have any idea why?
Re: Can't simultaneously run two threads??
Re: Can't simultaneously run two threads??
Quote:
Originally Posted by
Norm
I have no ideas.
If i can ask one last question, i am trying to make a frame full of bouncing balls, i.e. each ball controlled by their own runnable, which bounce off the window panes. What would be a good layout manager to use in that instance? Can i use gridlayout still?
Re: Can't simultaneously run two threads??
Change the logic to have a single component with an override to the paintComponent() method that has a list of balls to draw within its area.
Re: Can't simultaneously run two threads??
Quote:
Originally Posted by
Norm
Change the logic to have a single component with an override to the paintComponent() method that has a list of balls to draw within its area.
Unfortunately thats a pre-requisite that i have to have each ball controlled by its own runnable/thread,
Re: Can't simultaneously run two threads??
A thread is not a component. You can have many threads used by or using one component. By component I mean a class that extends the Component class, like JPanel or JButton.
Re: Can't simultaneously run two threads??
Sorry i dont understand how i can override paintcomponent from within the thread, surely you have to set public methods and variables within the paintcomponent to modify it from an altogether different class, the BallsFrame class?
Re: Can't simultaneously run two threads??
You define a component like JPanel and override its paintComponent() method. It will be called when the JPanel's repaint() method is called. There will be a list of the Balls to draw that the paintComponent() method will use to draw the balls.
What are the threads supposed to do?