简体   繁体   English

设置用于为洪水填充多边形的动画的摇摆计时器?

[英]Swing timer for animating flood filling a polygon?

i have an algorithm which i have implemented to fill any shape.... But it fills the shape instantly without any delay... I want it to show a type of animation so that it can be seen how a flood fill algorithm works when a shape is filled. 我已经实现了一种可以填充任何形状的算法。...但是它可以立即填充形状而没有任何延迟...我希望它显示一种动画类型,以便可以看到填充算法何时工作形状被填充。

Here is my algorithm: 这是我的算法:

  public static void floodFill(BufferedImage image, int x,int y, int fillColor)
    {
      java.util.ArrayList<Point> examList=new java.util.ArrayList<Point>();

      int initialColor=image.getRGB(x,y);
      examList.add(new Point(x,y));

      while (examList.size()>0)
      {
        Point p = examList.remove(0);  // get and remove the first point in the list
        if (image.getRGB(p.x,p.y)==initialColor)
        {
          x = p.x;  y = p.y;
          image.setRGB(x, y, fillColor);  // fill current pixel

          examList.add(new Point(x-1,y));
          examList.add(new Point(x+1,y));        
          examList.add(new Point(x,y-1));  
          examList.add(new Point(x,y+1));       
        }
      }
    }

where should the start timer be placed? 启动计时器应该放在哪里?

Basically, you need some way that you can wait for a specified period of time and then perform your update. 基本上,您需要某种方式可以等待指定的时间段然后执行更新。

When working within a GUI framework like Swing, you can't simply sleep on the UI thread, as this prevents the UI thread from keeping the screen up to date. 在像Swing这样的GUI框架中工作时,您不能只停留在UI线程上,因为这会阻止UI线程使屏幕保持最新状态。 Equally, until the method exists, the UI thread can't process paint requests either. 同样,在该方法存在之前,UI线程也无法处理绘画请求。

Without more context, you could do something "like"... 没有更多的上下文,您可以做“类似”的事情...

public static void floodFill(final BufferedImage image, int x, int y, final int fillColor) {
    final java.util.ArrayList<Point> examList = new java.util.ArrayList<Point>();

    final int initialColor = image.getRGB(x, y);
    examList.add(new Point(x, y));

    Timer timer = new Timer(40, new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            if (!examList.isEmpty()) {
                Point p = examList.remove(0);  // get and remove the first point in the list
                if (image.getRGB(p.x, p.y) == initialColor) {
                    int x = p.x;
                    int y = p.y;
                    image.setRGB(x, y, fillColor);  // fill current pixel

                    examList.add(new Point(x - 1, y));
                    examList.add(new Point(x + 1, y));
                    examList.add(new Point(x, y - 1));
                    examList.add(new Point(x, y + 1));

                }
                repaint(); // Assuming your painting the results to the screen
            } else {
                ((Timer)e.getSource()).stop();
            }
        }
    });
    timer.start();
}

Which uses a javax.swing.Timer to schedule a repeated call back (in this example, every 40 milliseconds), which the processes the next element in your list, this is effectively acting as a kind of delayed loop 它使用javax.swing.Timer安排重复的回调(在此示例中,每40毫秒一次),该回调处理列表中的下一个元素,这实际上是一种延迟循环

See How to use Swing Timers for more details 有关更多详细信息,请参见如何使用Swing计时器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM