Java中的Flood Fill算法 [英] Flood Fill algorithm in java

查看:66
本文介绍了Java中的Flood Fill算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在java中实现floodfill算法。当我选择图像中的像素时,所选像素的颜色必须遍布整个图像。我在java中实现了一个仅覆盖左像素的代码。

I am trying to implement floodfill algorithm in java.When I select a pixel in an image the selected pixel's color must be spread throughout the entire image.I have implemented a code in java which covers only left pixels.

 import java.awt.*;
  import java.awt.event.*;
  import java.io.*;
  import java.awt.image.*;
  import javax.imageio.*;
  public class FloodFill extends Frame implements MouseListener
  {
   BufferedImage buff;
   public FloodFill() throws Exception
   {
    setVisible(true);
    setLayout(null);
    setTitle("Fuzzy Select tool");
    buff=ImageIO.read(new File("cif_image.png"));
    setSize(buff.getWidth(),buff.getHeight());
    addMouseListener(this);
    repaint();
   }
   public void mousePressed(MouseEvent me)
   {
   try
   {
    int x=me.getX();
    int y=me.getY();
    int rgb=buff.getRGB(x,y);
    Color c=new Color(rgb);
    System.out.println(c.getRed()+" "+c.getGreen()+" "+c.getBlue());
    floodFill(x,y,c);
   }
   catch(Exception e)
   {
    System.out.println(e);
   }
  }
  public void mouseClicked(MouseEvent me)
  {
  }
  public void mouseReleased(MouseEvent me)
  {
  }
  public void mouseEntered(MouseEvent me)
  {
  }
  public void mouseExited(MouseEvent me)
  {
  }
  public void  floodFill(int x,int y,Color c1) throws Exception
  {
   if(((x<0)||(x>buff.getWidth()))||((y<0)||(y>buff.getHeight())))
   {
    return;
   }
   int temp=buff.getRGB(x,y);
   Color colorTemp=new Color(temp);
   if((colorTemp.getRed()!=c1.getRed())||(colorTemp.getGreen()!=c1.getGreen())||    (colorTemp.getBlue()!=c1.getBlue()))
   {
     buff.setRGB(x,y,c1.getRGB());
     repaint();
   }
   floodFill(x-1,y,c1);
   floodFill(x-1,y-1,c1);
   floodFill(x-1,y+1,c1);
   floodFill(x+1,y,c1);
   floodFill(x+1,y-1,c1);
   floodFill(x+1,y+1,c1);
   floodFill(x,y-1,c1);
   floodFill(x,y+1,c1);
  }
  public void paint(Graphics g)
 {
  g.drawImage(buff,0,0,this);
 }
 public static void main(String args[]) throws Exception
 {
  FloodFill fs=new FloodFill();
 }
}





当我执行此代码时,我无法实现洪水填充。我得到的异常是ArrayIndexOutOfBoundsException:Coordinate Exception。我的基本情况有问题(最有可能)。任何人都可以帮助我吗?



When I execute this code I am not able to achieve floodfill.The exception which I get is ArrayIndexOutOfBoundsException:Coordinate Exception.I have a problem in my base case condition(most probably).Can anyone help me?

推荐答案

我想你'点击图像边界外。

I think you're clicking outside the bounds of the image.
int temp=buff.getRGB(x,y);



将抛出


Will throw

<pre>ArrayIndexOutOfBoundsException</pre> if not inside the bounds of the image.<br />
Guard for that scenario by adding something like;<br />
<pre lang="cs">try<br />
{<br />
 int x=me.getX();<br />
 int y=me.getY();<br />
 if (x < buff.getWidth() && y < buff.getHeight()) {<br />
     int rgb=buff.getRGB(x,y);<br />
     Color c=new Color(rgb);<br />
     System.out.println(c.getRed()+" "+c.getGreen()+" "+c.getBlue());<br />
     floodFill(x,y,c);<br />
 }<br />
}<br />
catch(Exception e)<br />
{<br />
 System.out.println(e);<br />
}</pre><br />
<br />
Also, I think you'll be off by the height of the windows title bar, that is to say clicking at the top of the window will not yield a y-coordinate of 0, but the height of the title bar of the window.<br />
<br />
Hope this helps,<br />
Fredrik<br />
<br />
ps. Your algorithm will probably stack-overflow on all but really small images.


这篇关于Java中的Flood Fill算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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