2015-10-28 27 views
2

我想畫一張每秒更換兩次顏色的光盤。該磁盤在一個擴展JPanel的DrawPanel上繪製,在主要方法中,DrawPanel被添加到一個框架中。 對於colorchanging我使用一個計時器,當我試圖改變主要方法中的DrawPanel的背景(我註釋掉了)。Java顏色變化帶定時器的圖形

有人能告訴我爲什麼它不適用於Graphics g對象或任何其他建議?

我剛剛從主要方法複製代碼,並將其添加到paintComponent()方法,但在這裏它不起作用。

import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.util.Random; 

import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.Timer; 

public class DrawPanel extends JPanel{ 

    public GridBagLayout gbl; 

    //position and dimension 
    int x = 0, y = 0, width = 200, height = 200; 

    public DrawPanel(){ 
     repaint(); 
    } 

    public DrawPanel(GridBagLayout gridBagLayout) { 
     this.gbl = gridBagLayout; 
    } 

    public void paintComponent(Graphics g){ 
     //Overwriting of old picture 
     super.paintComponent(g); 

     ActionListener action = new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent e) { 
       Random gen = new Random(); 

       Color color = new Color(gen.nextInt(256), gen.nextInt(256), gen.nextInt(256)); 

       //Draw color disk 
       g.setColor(color); 
       g.fillArc(x, y, width, height, 0, 360); 
      } 
     }; 

     Timer t = new Timer(500, action); 
     t.setRepeats(true); 
     t.setInitialDelay(0); 
     t.start(); 



     //Draw boundary of circle 
     g.setColor(Color.BLACK); 
     g.drawArc(x, y, width, height, 0, 360); 
    } 


    public static void main(String[] args) { 
     final JFrame frame = new JFrame(); 
     frame.setSize(300, 300); 
     final DrawPanel panel = new DrawPanel(); 
      panel.setOpaque(true); 
      frame.getContentPane().add(panel); 

     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setVisible(true); 

//  ActionListener action = new ActionListener() { 
//   @Override 
//   public void actionPerformed(ActionEvent e) { 
//    Random gen = new Random(); 
//     Color color = new Color(gen.nextInt(256), gen.nextInt(256), gen.nextInt(256)); 
//     panel.setBackground(color);   
//   } 
//  }; 
// 
//  Timer t = new Timer(500, action); 
//  t.setRepeats(true); 
//  t.setInitialDelay(0); 
//  t.start(); 

    } 

} 
+1

繪畫方法不在我們的直接控制之中,只要JVM覺得UI需要重新繪製,並且可能由於許多不同的條件而發生,可能會調用繪畫方法。這不是**建立新計時器的時間! –

回答

2

Graphics對象是短暫的,所以你不應該緩存它,即使編譯器允許。相反,在類的構造函數中建立計時器,設置面板的BG,然後調用重新繪製。例如。

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import java.util.Random; 

public class DrawPanel extends JPanel { 

    Random gen = new Random(); 
    //position and dimension 
    int x = 0, y = 0, width = 200, height = 200; 
    Color drawColor = Color.BLACK; 

    public DrawPanel() { 
     repaint(); 
     ActionListener action = new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent e) { 
       Color color = new Color(gen.nextInt(256), gen.nextInt(256), gen.nextInt(256)); 

       //Draw color disk 
       drawColor = color; 
       DrawPanel.this.repaint(); 
      } 
     }; 

     Timer t = new Timer(500, action); 
     t.setRepeats(true); 
     t.setInitialDelay(0); 
     t.start(); 
    } 

    @Override 
    public void paintComponent(Graphics g) { 
     //Overwriting of old picture 
     super.paintComponent(g); 

     //Draw boundary of circle 
     g.setColor(drawColor); 
     g.drawArc(x, y, width, height, 0, 360); 
    } 

    public static void main(String[] args) { 
     final JFrame frame = new JFrame(); 
     frame.setSize(300, 300); 
     final DrawPanel panel = new DrawPanel(); 
     panel.setOpaque(true); 
     frame.getContentPane().add(panel); 

     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setVisible(true); 
    } 
} 
+1

我認爲你誤解了它的一部分,背景沒有改變,光盤顏色是。此外,我認爲'setBackground'可能會自動重新繪製面板以更新它,我將不得不檢查一次我在計算機上 – phflack

+0

呃,爲了清楚起見,我只想改變光盤的顏色,而不是的背景。背景的顏色應該保持其默認顏色。我只是試着與背景,看看我是否使用計時器是否正確使用。 – mabu

+1

@mabu,你應該像'setDiscColor(...)'一樣創建你的類的屬性。然後,當Timer觸發時,調用此方法將屬性設置爲隨機顏色。然後,setDiscColor(...)方法將調用repaint()。然後在繪畫方法中使用光盤顏色屬性來繪製光盤。繪畫方法不應隨機設置「顏色」,因爲無法控制何時調用繪畫方法。 – camickr

2

圖形對象僅適用於一個平局

這將是更好的,而不是告訴的JPanel改變它的當前顏色(有一個變量),然後告訴它重繪

  1. 添加變量discColor到您的JPanel

  2. 更改繪圖代碼不使用計時器,而是讓它簡單,只畫出盤基於斷discColor

  3. 具有定時功能,更新discColor變量,然後調用面板的repaint()方法