hi
friends.
I HAD CREATED ONE APPLET WHICH SHOW A CURRENT TIME.
IN
public void paint(Graphics g){
for(;;){
Thread.sleep(100);
//created one date object
Date d=new Date();
String s=d.toString();
g.drawString(s,10,10);
}
To show time i make thread to sleep for some time.
will date object will create for each time.
if so what to do with it.
SadRed - 08 Mar 2007 10:17 GMT
> hi
> friends.
[quoted text clipped - 19 lines]
>
> if so what to do with it.
Your current code does not make thread except application's main
thread.
Java GUI runs as a single thread. If you do a long and time consuming
task, such as your for(;;) loop, your single app thread is fully
occupied by the task and GUI can't have its turn to run.
Long and time consuming task should be run as a new separate thread.
Try this code:
------------------------------------
/*
<applet code="Sagar" width="200" height="50"></applet>
*/
import java.applet.*;
import java.util.*;
import java.awt.*;
public class Sagar extends Applet{
String ds;
public void start(){
new Thread(){
public void run(){
while (true){
ds = (new Date()).toString();
repaint();
try{
Thread.sleep(1000);
}
catch (InterruptedException e){
e.printStackTrace();
}
}
}
}.start();
}
public void paint(Graphics g){
g.drawString(ds, 10, 10);
}
}
--------------------------------------