使用重绘和计时器每秒旋转一行

时间:2013-09-15 17:22:30

标签: java swing timer paint repaint

所以我试图使用Timer和paint方法每秒旋转一行。但是,我不太清楚最新情况。以下是一些相关方法:

public static ActionListener taskPerformer = new ActionListener() {

    public void actionPerformed(ActionEvent e) {
        Clock cl = new Clock();
        seconds++;
        cl.repaint();
    }
};


public void paint(Graphics g){
    super.paint(g);
    Graphics2D g2 = (Graphics2D) g;
    for(int c = 0; c<10; c++){
        g2.setPaint(Color.BLACK);
        g2.drawOval(90-c/2,90-c/2,500+c,500+c); //thick outlined circle
    }
    g2.setPaint(Color.WHITE);
    g2.fillOval(90,90,501,501);
    g2.setPaint(Color.BLACK);
    g2.rotate(Math.toRadians(seconds*6));
    g2.drawLine(340,340,340,90);    

}

线路保持静止。但是,如果我添加

System.out.println("tick");

到我的actionPerformed方法,命令行每秒吐出“tick”3次。关于为什么会发生这些事情的任何想法?

某些背景信息:

public static int seconds = 0;
public static int minutes = 0;
public static int hours = 0;
public static Clock cl = new Clock();
private ActionListener taskPerformer = new ActionListener() {

    public void actionPerformed(ActionEvent e) {
        System.out.println("tick");
        seconds++;
        cl.repaint();
    }
};
public static Timer timer = new Timer(1000,taskPerformer);

public static void main(String[] args){
    Clock cl = new Clock();
    init();
    SwingUtilities.invokeLater(new Runnable(){
        public void run() {
            createAndShowGUI();
        }
    });
}
public static void init(){
    timer.start();
}
public Clock() {
    super("Clock");
    timer.addActionListener(taskPerformer);

}

1 个答案:

答案 0 :(得分:1)

您正在每个刻度线创建一个新时钟:

public void actionPerformed(ActionEvent e) {
    Clock cl = new Clock();
    ...

相反,您应该使用现有实例。

// A field in the class:
Clock cl = new Clock();
...

// removed static so that it can access cl
private ActionListener taskPerformer = new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        seconds++;
        cl.repaint();
    }
};

如果你不需要在其他地方访问它,你也可以让时钟成为动作监听器中的一个字段。

另请注意,您通常不应该覆盖paint(),而应覆盖paintComponent()。有关自定义绘画的更多信息here.

修改 现在有更多的代码可用,可以说如果你让时钟和动作监听器保持静态,那么它应该可行。 然而,您需要在相关组件准备就绪后启动计时器:

public static void main(String[] args){
    // Removed spurious clock here
    SwingUtilities.invokeLater(new Runnable(){
        public void run() {
            createAndShowGUI();
            // Start the timer once the components are ready
            init();
        }
    });
}

上面提到的关于在动作监听器中创建时钟的观点仍然存在。