Dynamic Clock

Hi,I want to make a very simple dynamic clock using java applets.I know how to get the time and display it but it obviously is static. Should I put it in an endless loop or is there a better way to implement it?Thanx
[251 byte] By [philosophologya] at [2007-10-2 3:26:53]
# 1
There is a clock applet in the demo folder of the JDK, if you installed the demo programs when you install the JDK. Take a look at it.
ChuckBinga at 2007-7-15 22:36:31 > top of Java-index,Java Essentials,Java Programming...
# 2

I read the Clock in the Demo folder. No use!

I still do not understand the concept!

My question is very simple: to update something like a clock constantly what kind of loop should I use if any, also if a loop is to be used would that not use the system memory too much?

I do not want code nor I want to just copy and paste! I'm just trying to learn.

Thanx

philosophologya at 2007-7-15 22:36:31 > top of Java-index,Java Essentials,Java Programming...
# 3
Don't use a loop.Look at javax.swing.Timeror possibly java.util.Timer, but the first will be easier for a GUI app.
FuzzyOniona at 2007-7-15 22:36:31 > top of Java-index,Java Essentials,Java Programming...
# 4

ok, here is my code:

package countDownClock;

import java.applet.Applet;

import java.awt.Graphics;

import java.util.Date;

import java.util.TimerTask;

import java.util.Timer;

public class CountDownClock extends Applet{

private static final long serialVersionUID = 1;

public void paint(Graphics g) {

long delay = 1000;

long period = 1000;

Date dateD = new Date();

String dateS = dateD.toString();

Timer timer = new Timer();

timer.schedule( ? ,delay,period);

g.drawString("" + dateS, 50, 25);

}

}

I do not know how to make a "TimerTask task" that is needed in the place of ?

I want the task to be the g.drawStr....

The Java API is a little confusing on the TimerTask class.

What should I do?

Thanx

philosophologya at 2007-7-15 22:36:31 > top of Java-index,Java Essentials,Java Programming...
# 5

> I want the task to be the g.drawStr....

> The Java API is a little confusing on the TimerTask class.

> What should I do?

Read the API docs again and attempt to implement what you want?

Don't just skim over the API docs and decide that the stuff is 'confusing'.

What you should do is *study* that documentation and experiment.

If you're stuck then, come back here, show your attempts and ask

your questions. Questions such as "what should I do?" don't make it.

kind regards,

Jos

JosAHa at 2007-7-15 22:36:31 > top of Java-index,Java Essentials,Java Programming...
# 6

actually I tried

TimerTask dateTask = g.drawString("" + dateS, 50, 25);

in

package countDownClock;

import java.applet.Applet;

import java.awt.Graphics;

import java.util.Date;

import java.util.TimerTask;

import java.util.Timer;

public class CountDownClock extends Applet{

private static final long serialVersionUID = 1;

public void paint(Graphics g) {

long delay = 1000;

long period = 1000;

Date dateD = new Date();

String dateS = dateD.toString();

Timer timer = new Timer();

timer.schedule(dateTask,delay,period);

TimerTask dateTask = g.drawString("" + dateS, 50, 25);

}

}

but it gives me the type conversion error from Void to TimerTask. It doesn't let me cast it either. Any ideas?

Thanx

philosophologya at 2007-7-15 22:36:31 > top of Java-index,Java Essentials,Java Programming...
# 7

Here is my latest:

Compiles and Runs.

The problem: The applet is empty! No clock!

HELP!

package countDownClock;

import java.applet.Applet;

import java.awt.Graphics;

import java.util.Date;

import java.util.TimerTask;

import java.util.Timer;

public class CountDownClock extends Applet{

private static final long serialVersionUID = 1;

public void paint(final Graphics g) {

long delay = 1000;

long period = 1000;

Date dateD = new Date();

final String dateS = dateD.toString();

Timer timer = new Timer();

TimerTask dateTask = new TimerTask() {

public void run() {

g.drawString("" + dateS, 50, 25);

}

};

timer.scheduleAtFixedRate(dateTask,delay,period);

}

}

philosophologya at 2007-7-15 22:36:31 > top of Java-index,Java Essentials,Java Programming...
# 8
BUMP ... I appreciate any help...
philosophologya at 2007-7-15 22:36:31 > top of Java-index,Java Essentials,Java Programming...
# 9
The OP has abandoned this thread and started a new one here: http://forum.java.sun.com/thread.jspa?threadID=676976
ChuckBinga at 2007-7-15 22:36:31 > top of Java-index,Java Essentials,Java Programming...