Click here to Skip to main content
15,886,058 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Java
import java.awt.image.BufferedImage;

import javax.imageio.ImageIO;
import javax.swing.JLabel;
import javax.swing.ImageIcon;

import java.awt.Color;
import java.awt.Point;
import java.io.File;
import java.util.ArrayList;

class getting_rightsidepixel {
	static ArrayList l2 = new ArrayList();

	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));
		try {

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

					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);
					// this line can be removed
				}
			}
		} catch (Exception e) {
			// x = p.x; y = p.y;
			image.setRGB(x, y, fillColor); // fill current pixel

			examList.add(new Point(x - 1, y)); // check west neighbor
			examList.add(new Point(x + 1, y)); // check east neighbor
			examList.add(new Point(x, y - 1)); // check north neighbor
			examList.add(new Point(x, y + 1)); // check south neighbor
		}

	}

	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 {
		// create an 200x200 RGB image
		BufferedImage image = ImageIO.read(new File("img0.png"));

		final int height = image.getHeight();
		final int width = image.getWidth();
		int countrow = 0;
		int countcol = 0;
		int newrow = width - 1;
		// System.out.println("weight-height"+newrow );

		for (int i = 0; i < width; i++) {
			for (int j = 0; j < height; j++) {
				if (countrow == newrow) {
					Color c = new Color(image.getRGB(i, j));
					int a = c.getRed();
					int b = c.getGreen();
					int c1 = c.getBlue();
					if (a == 255 && b == 255 && c1 == 255) {
						System.out
								.println("location of x " + i + "j value" + j);
						int yellow = packRgb(255, 0, 0);
						// int x=100, y=100; // make sure (x,y) is within the
						// circle
						floodFill(image, i, j, yellow);
					}
					l2.add(a);

					// System.out.println(l2);
					// System.out.println(countrow);
				}

				countcol++;

			}

			countrow++;

		}

		

		JLabel imageLabel = new JLabel();
		_imageLabel = imageLabel; // make it global
		imageLabel.setIcon(new ImageIcon(image));
		imageLabel.setText("Filling the circle with yellow color ...");

		javax.swing.JFrame window = new javax.swing.JFrame();
		window.setTitle("Macteki flood filler");
		window.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);

		window.add(imageLabel);
            window.pack();
		window.setVisible(true);
		imageLabel.setIcon(new ImageIcon(image));

	}
}

error
"java.lang.ArrayIndexOutOfBoundsException: Coordinate out of bounds!
at sun.awt.image.ByteInterleavedRaster.getDataElements(Unknown Source)
at java.awt.image.BufferedImage.getRGB(Unknown Source)
at getting_lastpixel.floodFill(getting_lastpixel.java:49)
at getting_lastpixel.main(getting_lastpixel.java:101)"
"in that flood fill algorithm will not fully they throw exception"
Posted
Updated 9-Dec-13 4:37am
v2
Comments
Sergey Alexandrovich Kryukov 9-Dec-13 10:42am    
In what line of this code? All you need it to use the debugger. (Did you?) Such bugs are usually easy to find and fix...
—SA
baliram bhande 9-Dec-13 10:53am    
When i get the array out of bound exception i just want to stop further expansion of that pixel which is throwing this exception and let the other pixels continue the loop until they are out of bound. Once each pixel is out of bound i want to stop the execution.
Sergey Alexandrovich Kryukov 9-Dec-13 11:17am    
Whatever you want to do, use the debugger to locate and fix the bug.

What are you talking about it your exception stack tells you about the method "getting_lastpixel", and you don't show it, show some other method? No relevant information, no help. Such problems are really easy to solve, please do it by yourself.

—SA

I would start searching here:

getting_lastpixel.java - Line 49

You can switch on line numbers in eclipse by searching them in the preferences and marking that function.

http://www.mkyong.com/eclipse/how-to-display-line-numbers-in-eclipse/[^]

That helps in debugging.
 
Share this answer
 
Comments
baliram bhande 11-Dec-13 0:50am    
i know very well how debug java program and finding error line no.but i want to handle that exception or want to When i get the array out of bound exception i just want to stop further expansion of that pixel which is throwing this exception and let the other pixels continue the loop until they are out of bound. Once each pixel is out of bound i want to stop the execution.
TorstenH. 11-Dec-13 6:51am    
Then let me introduce you to the concept of Catching and Handling Exceptions.
baliram bhande 11-Dec-13 6:55am    
my error is not like that what u said . my problem is mentioned above
. When i get the array out of bound exception i just want to stop further expansion of that pixel which is throwing this exception and let the other pixels continue the loop until they are out of bound. Once each pixel is out of bound i want to stop the execution.
TorstenH. 11-Dec-13 9:06am    
So how do you want to figure out that you've run outofBound?
You can create some kind of if-clause to prevent that. Or you just let that happen, use the catch to note down the now invalid pixel and carry on with what you've been doing by regarding that list...
What about a tutorial on debugging Java code? Google[^] is your friend.
 
Share this answer
 
Comments
baliram bhande 11-Dec-13 0:50am    
i know very well how debug java program and finding error line no.but i want to handle that exception or want to When i get the array out of bound exception i just want to stop further expansion of that pixel which is throwing this exception and let the other pixels continue the loop until they are out of bound. Once each pixel is out of bound i want to stop the execution.
Java
import java.awt.image.BufferedImage;

import javax.imageio.ImageIO;
import javax.swing.JLabel;
import javax.swing.ImageIcon;

import java.awt.Color;
import java.awt.Point;
import java.io.File;
import java.util.ArrayList;

class getting_leftsidepixel1 {
	static int temp1,temp2;
	static int count=0;
	static int x1;
	static BufferedImage image;
	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));
		System.out.println("array list is "+examList);
		System.out.println("x value is"+x);
		System.out.println("y value is "+y);
		

			while (examList.size() > 0) {
				Point p = examList.remove(0); 
														try
				{
				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)); 
					 repaintAndDelay(image);
					}
				}catch(Exception e)
				{
					
					temp1=x;
					temp2=y;
			}
		} 

	}

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 {
		// create an 200x200 RGB image
 image = ImageIO.read(new File("inputleft1.png"));

		final int height = image.getHeight();
		final int width = image.getWidth();
		System.out.println("height"+height);
		System.out.println("width"+width);
		int countrow = 0;
		int countcol = 0;
		int newrow = width - height;
		for (int i = 0; i < width; i++) {
			for (int j = 0; j < height; j++) {
				if (countrow == newrow) {
					Color c = new Color(image.getRGB(i, j));
					int a = c.getRed();
					int b = c.getGreen();
					int c1 = c.getBlue();
					if (a == 255 && b == 255 && c1 == 255) {						+ "j value" + j);
						int yellow = packRgb(255, 0, 0);
						// int x=100, y=100; // make sure (x,y) is within the
						// circle
						System.out.println(temp1);
						System.out.println(temp2);
						if(temp1!=i&&temp2!=j)
						{
						floodFill(image, i, j, yellow);
						}
					}
					
				}

				countcol++;

			}

			countrow++;

		}

		JLabel imageLabel = new JLabel();
		_imageLabel = imageLabel; // make it global
		imageLabel.setIcon(new ImageIcon(image));
		imageLabel.setText("Filling the circle with red color ...");

		javax.swing.JFrame window = new javax.swing.JFrame();
window.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
window.add(imageLabel);
window.pack();
window.setVisible(true);

		
		imageLabel.setIcon(new ImageIcon(image));
		ImageIO.write(image, "png",new File("finalimage3.png"));

	}
}</point></point>
 
Share this answer
 
v2

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