Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
428 views
in Technique[技术] by (71.8m points)

java - Time doesn't repaint in Applet

I have a small problem but I don't know to fix it. I create simple Applet in which should be simple digital clock. I created all methods correctly but repaint methods doesn't repaint my applet. Can you check my code and say where is mistake. Thanks.

public class DigitalClock extends JApplet implements Runnable {

private Thread timeThread;
Date date = new Date();

public void start() {
    timeThread = new Thread(this, "Clock");
    timeThread.start();
}

@Override
public void stop() {
    if (timeThread == null) {
        return;
    }
    timeThread = null;
}

@Override
public void run() {
    while (timeThread != null) {
        repaint();
        try {
            timeThread.sleep(1000);
        } catch (InterruptedException e) {
        }
    }
}

@Override
public void paint(Graphics g) {
    date.setTime(System.currentTimeMillis());
    g.drawString(date.toString(), 50, 95);
}
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Just use a javax.swing.Timer. See how to use Swing Timer

Here's an example. But rather than painting on top-level container like JApplet which you are doing, I'm painting on JPanel which is more recommended

enter image description here

import java.awt.*;
import java.awt.event.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.*;

public class TimerPanel extends JPanel {

    private String dateString;
    private final SimpleDateFormat format;
    private final Font font;
    private final Date date;

    public TimerPanel() {
        format = new SimpleDateFormat("hh:mm:ss a");
        font = new Font("impact", Font.PLAIN, 30);
        date = new Date();

        date.setTime(System.currentTimeMillis());
        dateString = format.format(date);

        Timer timer = new Timer(1000, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                date.setTime(System.currentTimeMillis());
                dateString = format.format(date);
                repaint();
            }
        });
        timer.start();
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        g.setFont(font);
        FontMetrics fm = g.getFontMetrics(font);
        int width = fm.stringWidth(dateString);
        int height = fm.getAscent();
        int x = getWidth() / 2 - width / 2;
        int y = getHeight() / 2 + height / 2;

        g.drawString(dateString, x, y);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(300, 100);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                TimerPanel timer = new TimerPanel();
                JOptionPane.showMessageDialog(
                        null, timer, "Digital Clock", JOptionPane.PLAIN_MESSAGE);
            }
        });
    }
}

More directly answering your question, your code works when I test it, the only thing you're forgetting to do is call super.paint(). Works as expected after doing so

@Override
public void paint(Graphics g) {
    super.paint(g);

You'll want to avoid Thread.sleep though. You may notice this causes flickering. It's preferred to use javax.swing.Timer. That's why I answered with my example first.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...