Problems with a clock element

Good Morning,

I just need your advice. I am trying to code a clock element with the java2d. What i thought about is just a string that is repainted each second or minute, depending on the format of the time.

Of course, I don't want to block the main thread with this task, so i tryed to put it into its own one, that can run in the background, calculating the time and write it out.

And this is where my problems start. The element lies in a JPanel and will be called and started by the paintComponent() - function of the JPanel. The call will start the clock thread, and within this thread, i will clear and repaint the timestring.

The thread is started proparly, the functions are called and everything - but nothing appears on the screen.

I think the problem is more the call of the function than the function itself - do I have to go through the JPanel each time I want to repaint the string? Thats what I want to avoid.

This is the run()-function of the clock-element. the Graphics2D will be stored within the class with the first call by the JPanel.

publicvoid run(){

mRunning =true;

while(mRunning){

mText = DateFormat.getTimeInstance(mType, Locale.GERMAN).format(new Date());

if(mTransform){

//Just to draw the string

this.transformTimeElement();

}else{

//Just to draw the string

this.drawTimeElement();

}

try{

Thread.sleep(mSleep);

}catch (InterruptedException e){

//empty by purpose

}

}

}

[2263 byte] By [Velligisa] at [2007-11-27 8:14:17]
# 1

import java.awt.*;

import java.text.DateFormat;

import java.util.*;

import javax.swing.*;

public class TimeSetup extends JPanel implements Runnable {

Thread thread = null;

String mText;

protected void paintComponent(Graphics g) {

super.paintComponent(g);

g.drawString(mText, 25, 25);

}

public Dimension getPreferredSize() {

return new Dimension(240, 50);

}

public void run() {

boolean mRunning = true;

long mSleep = 500;

int mType = DateFormat.MEDIUM;

while(mRunning) {

mText = DateFormat.getTimeInstance(mType, Locale.GERMAN).format(new Date());

repaint();

try {

Thread.sleep(mSleep);

} catch (InterruptedException e) {

//empty by purpose

}

}

}

private void start() {

thread = new Thread(this);

thread.setPriority(Thread.NORM_PRIORITY);

thread.start();

}

public static void main(String[] args) {

TimeSetup test = new TimeSetup();

test.start();

JOptionPane.showMessageDialog(null, test, "", -1);

}

}

crwooda at 2007-7-12 19:58:48 > top of Java-index,Security,Cryptography...
# 2
thanks!
Velligisa at 2007-7-12 19:58:48 > top of Java-index,Security,Cryptography...