2016-04-09 34 views
1
import java.awt.Graphics; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.JLabel; 
import java.awt.*; 
import javax.swing.*; 
import java.util.Timer; 
import java.awt.event.*; 
import java.awt.event.*; 
import javax.swing.*; 

class autos extends JLabel 
{ 
    @SuppressWarnings("serial") 
    int z=100,i=50; 
    public static void main(String[] args) 
    { 

     JFrame frame=new JFrame("Rectangle"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setVisible(true); 
     frame.setSize(1000,1000); 
     frame.add(new autos());  
    } 

    @Override 
    public void paintComponent(Graphics g) 
    { 
     for(i=1;i<=7;i++) 
     { 
      g.drawRect(z,100,100,100); 
      z=z+120; 
      //timer delay 
     } 

    } 
} 

你好,我试图创建一个Java程序,一个接一个地拖延(不是所有的一次)绘制mre矩形。在java中实现一个定时器

由于睡眠和TimeUnit将冻结paintComponent,我有点无知。我试图用一个计时器来延迟,但我失败了。我不明白如何使用计时器在这种情况下。

如何在矩形之间进行时间延迟?

+0

*“由于睡眠和TIMEUNIT将冻结'paintComponent'” *和延迟不会呢? – user2004685

+2

您似乎在使用'java.util.Timer',尝试['javax.swing.Timer'](https://docs.oracle.com/javase/8/docs/api/javax/swing/Timer的.html)。另请参阅[如何使用Swing定时器](https://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html)。 –

+0

@ user2004685“延迟”是什么意思? –

回答

3

你应该考虑看看How to use Swing TimersConcurrency in Swing

你开始将Swing Timer想象为伪循环,每当它跳动时,就会更新一些值,当您调用repaint和您的paintComponent方法时,您可以适当地更新UI

Timer

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 java.awt.event.MouseAdapter; 
import java.awt.event.MouseEvent; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.Timer; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class Test { 

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

    public Test() { 
     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 = 0; 

     public TestPane() { 
      Timer timer = new Timer(500, new ActionListener() { 
       @Override 
       public void actionPerformed(ActionEvent e) { 
        count++; 
        if (count == 7) { 
         ((Timer)e.getSource()).stop(); 
        } 
        repaint(); 
       } 
      }); 
      addMouseListener(new MouseAdapter() { 
       @Override 
       public void mouseClicked(MouseEvent e) { 
        timer.stop(); 
        count = 0; 
        timer.start(); 
       } 
      }); 
     } 

     @Override 
     public Dimension getPreferredSize() { 
      return new Dimension((120 * 7) + 100, 300); 
     } 

     @Override 
     protected void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      Graphics2D g2d = (Graphics2D) g.create(); 
      super.paintComponent(g); 
      int x = 100; 
      for (int rect = 1; rect <= count; rect++) { 
       g2d.drawRect(x, 100, 100, 100); 
       x += 120; 
       //timer delay 
      } 
      g2d.dispose(); 
     } 
    } 
} 

PS-单击面板,以得到定时器启动

+0

我不想让它开始点击,我试图让它在程序启动后立即开始绘制运行。 –

+0

@DanielTork那么?当你想要的时候启动它,我只需点击鼠标就可以启动它,因为我可以控制它,并且你可以看到它是如何工作的,现在由你来满足你的要求,但是,有一个基本的想法,可以演示和工作 – MadProgrammer

+0

好吧,但我在timer.stop()和timer.start()方面出现错误:无法引用在封闭范围中定义的非最终本地变量定时器 –

-1

你并不需要一个循环做到这一点job.You可以使用摆动计时器是这样的:

int delay=500;// delay time in ms 
Timer t=new Timer(delay,new ActionListener() { 

    @Override 
    public void actionPerformed(ActionEvent e){ 
     autos.this.getGraphics().drawRect(z,100,100,100); // draw rectangle in your panel's graphics 
     repaint(); // this line calls paint component to show changes 
     i++; 
     z+=120; 
     if (i>70) t.stop(); // for loop's condition 
    } 
}); 
+1

做autos.this.getGraphics()。drawRect(z,100,100,100);然后重绘将最好清除用户界面,最坏的情况下,它会使其闪光。你不应该叫getGraphics,它不是如何定制绘画作品 – MadProgrammer

+0

我应该把它放在paintComponent中,不是吗? –

+0

@DanielTork不! – MadProgrammer