簡體   English   中英

在Java中實現計時器

[英]Implementing a timer in java

 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矩形(並非一次繪制所有矩形)。

由於sleep和TimeUnit會凍結paintComponent ,所以我有點無能為力。我試圖使用計時器進行延遲,但是我失敗了。在這種情況下,我不知道如何使用計時器。

如何在矩形之間設置時間延遲?

您應該先看看如何 在Swing中 使用Swing計時器並發

您必須將Swing Timer視為偽循環,它在每次滴答時都會更新一些值,該值在調用repaintpaintComponent方法被調用時可以適當地更新UI。

計時器

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-單擊面板以啟動計時器

您不需要for循環即可完成此工作。您可以使用以下擺動計時器:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM