The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
February 2001

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

MultiThreaded Clock Applet

Posted by Monte Gardner on February 27, 2001 at 11:57 AM

I'm trying to make a simple clock
Applet that displays the time
using the Calendar class.
I have a clock class that
descends from (extends) the Thread class.

It has a method calld updateClock() which updates
the labels of the buttons in the applet. I know
this method works because I have a button which
activates it. When I click the button, the clock
is properly updated.
However, I don't want to have to click a button
every time I want the time, so I put continuous
calls to upDateClock and paint() in the
Start() method of the Clock class. When I call
the start method from the applet however, the
applet just doesn't paint untill the start method
is done. Here's a copy of the start method

/**
* continually calls update clock 10 times
* and requessts that the applet be repainted
*/
public void start()
{
//10 seconds of ticking
while (true)
try
{
Thread.sleep(1000); //1 second
}
catch (InterruptedException e)
{
System.out.println(e);
}//end catch

updateClock(); //updates the button labels

System.out.println("Calling paint()");
parent.paint(parent.getGraphics()); //repaints the applet
parent.hour.paint(parent.getGraphics());
}//end infinite while
}//end method

In the applet console, I can see the "Calling paint" message,
so I know paint is beeing called, but the applet just stays
gray unless i force it to be repainted by placing another
window over it.

I guess the short way of saying this is how do you call
the paint method of an applet from within a thread?





Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us