Java中的Flood Fill算法

编程入门 行业动态 更新时间:2024-10-23 01:43:10
本文介绍了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算法

本文发布于:2023-11-30 01:26:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1648258.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:算法   Java   Flood   Fill

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!