2012-11-23 55 views
0

我编写了绘制轮子(6段圆圈)和每段不同颜色的程序。和动画轮..停止无限循环paint()方法?用paint()添加JButton?

这里是代码:

public class ExamWheel extends JFrame implements ActionListener{ 
     JButton b_start = new JButton("Start"); 
     JButton b_stop = new JButton("Stop"); 
     Thread th; 
     Boolean doDraw = true; 

     public ExamWheel(){ 
      setSize(400,400); 
      setVisible(true); 
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      setLocationRelativeTo(null); 
      setTitle("Wheel.."); 
      //add(b_start); 
      this.setLayout (new FlowLayout()); 
      this.add(b_stop); 
      b_start.addActionListener(this); 
     } 

     public void actionPerformed(ActionEvent e) { 
      if(e.getSource()==b_start) 
       doDraw=true; 
     } 

     public void paint(Graphics graphics) { 
     if (doDraw){ 
      super.paint(graphics); 
      Graphics2D g = (Graphics2D) graphics; 
      g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 

      try{ 
      // draw the circle 
      for(int i=0; ; i=i+1){ 
       g.setColor(Color.CYAN); 
       g.fillArc(50, 50, 300, 300, i+0, 60); 
       th.sleep(1); 
       g.setColor(Color.red); 
       g.fillArc(50, 50, 300, 300, i+60, 60); 
       th.sleep(1); 
       g.setColor(Color.green); 
       g.fillArc(50, 50, 300, 300, i+120, 60); 
       th.sleep(1); 
       g.setColor(Color.blue); 
       g.fillArc(50, 50, 300, 300, i+180, 60); 
       th.sleep(1); 
       g.setColor(Color.gray); 
       g.fillArc(50, 50, 300, 300, i+240, 60); 
       th.sleep(1); 
       g.setColor(Color.pink); 
       g.fillArc(50, 50, 300, 300, i+300, 60); 
       th.sleep(1); 
      } 
      } 
      catch(InterruptedException e){ 
       Thread.currentThread().interrupt(); 
      } 
     } 
     } 

     public static void main(String[] args) { 
      ExamWheel f = new ExamWheel(); 
     } 

    } 

问题:它是无限循环,我不能停止或关闭框架。

,所以我不得不想法:

我创建布尔变量(doDraw)与真正的价值,并添加一个JButton,当单击该按钮的变量将变更为假,并在paint()方法我会如果第一次绘制条件()

问题:我不能用paint()添加JButton到Frame,所以我该怎么做?

注意:我试着使用paintComponent(),但循环(for with thread)不起作用。

IT解决 感谢皮特Kirham

我加入定时器和的paintComponent()

public class ExamWheel extends JPanel implements ActionListener { 
    int i=0; 
    Timer tm = new Timer(10, this); 
    public void paintComponent(Graphics graphics) { 
     super.paintComponent(graphics); 
     Graphics2D g = (Graphics2D) graphics; 
     g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 

     g.setColor(Color.CYAN); 
     g.fillArc(50, 50, 300, 300, i+0, 60); 
     g.setColor(Color.red); 
     g.fillArc(50, 50, 300, 300, i+60, 60); 
     g.setColor(Color.green); 
     g.fillArc(50, 50, 300, 300, i+120, 60); 
     g.setColor(Color.blue); 
     g.fillArc(50, 50, 300, 300, i+180, 60); 
     g.setColor(Color.gray); 
     g.fillArc(50, 50, 300, 300, i+240, 60); 
     g.setColor(Color.pink); 
     g.fillArc(50, 50, 300, 300, i+300, 60); 
     tm.start(); 
    } 
    public void actionPerformed(ActionEvent e) { 
     i++; 
     repaint(); 
    } 
    public static void main(String[] args) { 
     ExamWheel wh = new ExamWheel(); 
     JFrame jf = new JFrame(); 
     jf.setSize(500,500); 
     jf.setResizable(false); 
     jf.setVisible(true); 
     jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     jf.setLocationRelativeTo(null); 
     jf.setTitle("Wheel.."); 
     jf.add(wh); 
    } 
+0

为什么你在paint()中有无限循环,gui会在一定的时间间隔内调用paint()。你永远不会分配'th'值。睡一个你应该调用'Thread.sleep();'的线程。 –

回答