2014-05-03 78 views
0

我想在此數字時鐘的每個刻度上添加變化的背景顏色,但我不知道從哪裏開始。下面的代碼在每個時鐘刻度上更改背景顏色

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; 

    } 

提前感謝!

回答

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(); 
     } 

    } 

}