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