Harry Martland
Harry Martland

Reputation: 624

multi threaded graphics using a timer?

My lecturer gave me an assignment asking for a multi threaded graphics program where a ball bounces around a jframe. He wanted each ball to have its own thread. Upon marking he told me that a timer is multi threaded and this is the best way to do this. I used a new Thread for each ball I know this is costly but he told us to use a thread for each ball. Is the timer class multi threaded?

using a timer

Ball b = new Ball(x, y);
BallMover bm = new BallMover(b)//adds the ball to a new instance of an actionListner
theTimer.addActionListener(bm);

using threads

Ball b = new Ball(x,y);
BallMover bm = new BallMover(b);//adds the ball to a new instance of a Thread
bm.start();

Upvotes: 0

Views: 98

Answers (1)

Andrew Thompson
Andrew Thompson

Reputation: 168795

From the docs for the Swing Timer.

Although all Timers perform their waiting using a single, shared thread (created by the first Timer object that executes), the action event handlers for Timers execute on another thread -- the event-dispatching thread. This means that the action handlers for Timers can safely perform operations on Swing components. However, it also means that the handlers must execute quickly to keep the GUI responsive.

Upvotes: 2

Related Questions