Click here to Skip to main content
15,889,732 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
Java
 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();
 }
}


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?
Posted
Updated 12-Aug-14 3:40am
v2
Comments
Mehdi Gholam 12-Aug-14 0:46am    
... and what is your question/problem?
Richard MacCutchan 12-Aug-14 9:48am    
Use your debugger to find where the exception occurs, and look at all the variables to see which one has the out of bounds value.

1 solution

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

Will throw
ArrayIndexOutOfBoundsException
if not inside the bounds of the image.
Guard for that scenario by adding something like;
C#
try
{
 int x=me.getX();
 int y=me.getY();
 if (x < buff.getWidth() && y < buff.getHeight()) {
     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);
}


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.

Hope this helps,
Fredrik

ps. Your algorithm will probably stack-overflow on all but really small images.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900