在每个时钟刻度上改变背景颜色

时间:2014-05-03 10:56:37

标签: java eclipse swing

我正在尝试在此数字时钟的每个刻度上添加不断变化的背景颜色,但我不知道从哪里开始。这是我的代码

的标记部分
public void start()
{
    if ( runner==null) runner =new Thread(this);
    runner.start();
}
public void run()
{
    while(runner==Thread.currentThread())
    {
        repaint();
        try {Thread.sleep(1000);}
        catch(InterruptedException e) {}

    }
}

其他信息,如果有帮助:

public Clockclass()
{
  super ("javaclock"); setSize(300,100);
  setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
  setVisible(true);
  clockFont= new Font("Serif", Font.BOLD,50);
  Container contentArea =getContentPane();
  ClockPanel timeDisplay = new ClockPanel();
 contentArea.add(timeDisplay);
setContentPane(contentArea); 
 start();
}

class ClockPanel extends JPanel
{
    public void paintComponent (Graphics painter)
    {
        painter.setColor(Color.cyan);
        painter.fillRect(0,0,300,100);
        painter.setFont(clockFont);
        painter.setColor(Color.white);
        painter.drawString(timeNow(), 60, 40);
    }
}

public String timeNow(){
Calendar now =Calendar.getInstance();
int hrs=now.get(Calendar.HOUR_OF_DAY);
int min=now.get(Calendar.MINUTE);
int sec=now.get(Calendar.SECOND);
String time= zero(hrs)+":"+zero(min)+":"+zero(sec);
return time;
}


public String zero(int num)
{
    String number= (num<10)?("0"+num):(""+num);
    return number;

    }

提前致谢!

1 个答案:

答案 0 :(得分:2)

除非您必须使用javax.swing.Timer,否则它将在事件调度线程中执行tick事件通知。

这样您就可以通过直接传递颜色或自动调整颜色来简单地提供新颜色或触发ClockPanel内的颜色变化......

您应该始终致电super.paintComponentpaintComponent也应该是protected,没有理由任何机构应该在类上下文之外调用

请查看How to use Swing Timer了解详情

例如......

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Clock {

    public static void main(String[] args) {
        new Clock();
    }

    public Clock() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private int count;

        public TestPane() {
            Timer timer = new Timer(1000, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    count++;
                    repaint();
                }
            });
            timer.start();
        }

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

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.setColor(count % 2 == 0 ? Color.RED : Color.YELLOW);
            int dimeter = Math.min(getWidth(), getHeight());
            int x = (getWidth() - dimeter) / 2;
            int y = (getHeight()- dimeter) / 2;
            g2d.fillOval(x, y, dimeter, dimeter);
            g2d.dispose();
        }

    }

}