达到0时定时器闪烁

时间:2014-01-08 04:21:16

标签: java timer flicker

导入库

public class Countdown1 extends Applet implements Runnable {
// getting user input

    String input = JOptionPane.showInputDialog("Enter seconds: ");
 // converting string to integer

    int counter = Integer.parseInt(input);

    Thread countdown;

    public void start() {
        countdown = new Thread(this);
        countdown.start();

    }

// executed by thread
    public void run() {

        Timer timer;

        timer = new Timer(1000, new ActionListener() /* counting down time inputted */ {
            public void actionPerformed(ActionEvent evt) {
                if (counter > 0) {

                    counter--;
// repainting each second

                    repaint();
                }
            }
        });
// timer started 

        timer.start();

    }

    public void paint(Graphics g) {
//painting text and time 

        g.setFont(new Font("Times New Roman", Font.BOLD, 35));
        g.drawString("Seconds: " + String.valueOf(counter), 260, 210);
        setBackground(Color.orange);
        setForeground(Color.magenta);

// change background to cyan when timer reaches 0
        if (counter == 0) {
            setBackground(Color.cyan);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

问题不是你的Timer(虽然我确实怀疑是否需要在一个单独的线程中启动它),问题在于覆盖paint

Applet这样的顶级容器不是双缓冲的,这意味着每个绘制操作都会拼命地发送到底层的Graphics设备。

现在你可以通过员工来完成这个双缓冲过程,或者你可以......

  • JApplet改为
  • 创建一个自JPanel之类的自定义组件,并覆盖它的paintComponent方法,将自定义绘图移动到此方法
  • 将此组件添加到applet中。

它应该解决眼前的问题......

您还应该避免在任何setForeground方法中调用setBackgroundpaint。实际上,您应该避免在任何repaint方法中调用任何可能调用paint的方法......

查看Performing Custom Painting

我很确定String input = JOptionPane.showInputDialog("Enter seconds: ");这是一个坏主意。相反,您应该在UI中提供某种控制或选项来更改此值...

原始示例

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JApplet;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.Timer;

public class Countdown1 extends JApplet {

    @Override
    public void init() {
        add(new CounterPane());
    }

    public class CounterPane extends JPanel {

        String input = JOptionPane.showInputDialog("Enter seconds: ");

        int counter = Integer.parseInt(input);

        public CounterPane() {

            Timer timer;

            timer = new Timer(1000, new ActionListener() /* counting down time inputted */ {
                public void actionPerformed(ActionEvent evt) {
                    System.out.println(counter);
                    if (counter > 0) {

                        counter--;

                        setBackground(Color.orange);
                        setForeground(Color.magenta);

                        if (counter <= 0) {
                            setBackground(Color.cyan);
                        }

                        repaint();
                    }
                }
            });

            timer.start();

        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setFont(new Font("Times New Roman", Font.BOLD, 35));
            g.setColor(getForeground());
            g.drawString("Seconds: " + String.valueOf(counter), 260, 210);
        }

    }
}