随着时间的推移逐个绘制形状而不是一次全部绘制形状

时间:2017-03-26 18:27:59

标签: java swing graphics paintcomponent

我想创建一条线,并在2秒后打破另一条线,依此类推。 如果我绘制一条线并用Thread.sleep()暂停程序执行并再次在paintComponent()方法中绘制一条线,那么首先程序停止2秒然后同时绘制两条线。 如何克服这个问题?

1 个答案:

答案 0 :(得分:3)

这是真正的共同要求和问题。您应首先查看Concurrency in SwingHow to use Swing Timers,了解有关如何解决基本问题的一些基本信息。

您还应该查看Painting in AWT and SwingPerforming Custom Painting,了解有关绘画如何在Swing中工作的更多信息

您想要关注的核心设计选择是:

  1. 不要阻止事件调度线程,这将阻止它绘制任何内容或响应新事件
  2. 不要在EDT上下文之外更新用户界面
  3. 绘画应该绘画状态。它不应该专注于尽可能做出逻辑决策,相反,它应该依赖于一个或多个模型来为其提供绘制自己所需的信息
  4. import java.awt.Dimension;
    import java.awt.EventQueue;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Shape;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.geom.Line2D;
    import java.util.ArrayList;
    import java.util.List;
    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 List<Shape> shapes;
            private int yPos = 0;
    
            public TestPane() {
                shapes = new ArrayList<>(25);
    
                Timer timer = new Timer(2000, new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        yPos += 10;
                        Line2D line = new Line2D.Double(0, yPos, getWidth(), yPos);
                        shapes.add(line);
                        repaint();
                    }
                });
                timer.setInitialDelay(2000);
                timer.start();
            }
    
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(200, 200);
            }
    
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2d = (Graphics2D) g.create();
                for (Shape line : shapes) {
                    g2d.draw(line);
                }
                g2d.dispose();
            }
    
        }
    
    }