2015-10-28 28 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()方法