by using Flood Fill algorithm i have done this
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import java.awt.Point;
import java.io.File;
class mm1
{
private static void repaintAndDelay(BufferedImage image)
{
_imageLabel.setIcon(new ImageIcon(image));
_imageLabel.repaint();
try { Thread.sleep(1); } catch (Exception ignore) {}
}
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);
if (image.getRGB(p.x,p.y)==initialColor)
{
x = p.x; y = p.y;
image.setRGB(x, y, fillColor);
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));
repaintAndDelay(image);
}
}
}
public static int packRgb(int r,int g,int b)
{
return (r*256+g)*256+b;
}
static JLabel _imageLabel;
public static void main(String[] args) throws Exception
{
BufferedImage image=ImageIO.read(new File("test1.png"));
JLabel imageLabel=new JLabel();
_imageLabel = imageLabel;
imageLabel.setIcon(new ImageIcon(image));
javax.swing.JFrame window=new javax.swing.JFrame();
window.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
window.add(imageLabel);
window.pack();
window.setVisible(true);
int yellow = packRgb(255,0,0);
int x=100, y=100;
floodFill(image,x,y, yellow);
imageLabel.setIcon(new ImageIcon(image));
}
}</point></point>