摇摆的动画洪水填充多边形计时器? [英] Swing timer for animating flood filling a polygon?

查看:75
本文介绍了摇摆的动画洪水填充多边形计时器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我已经实现,以填补任何形状的算法....但它瞬间填充形状没有任何延迟...我想让它显示一个类型的动画,这样就可以看到如何倾倒填充算法工作时的形状填充

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.

下面是我的算法:

  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.

当如Swing的GUI框架内工作,你不能简单地睡在UI线程上,因为这$ P $从保持屏幕最新pvents UI线程。同样,直到方法存在,UI线程也不能处理涂料的请求。

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. Equally, until the method exists, the UI thread can't process paint requests either.

如果没有更多的情况下,你可以做一些喜欢...

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

其中使用 javax.swing.Timer中来调度一个重复回叫(在这个例子中,每40毫秒),该过程的下一个元素列表中的,这有效地作为一种延迟循环

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

请参阅如何使用Swing定时器了解详细信息

这篇关于摇摆的动画洪水填充多边形计时器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆