Date object

S

Sagar

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.
 
S

SadRed

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.

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top