数组索引outof bound [英] array index outof bound

查看:102
本文介绍了数组索引outof bound的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

	}
}



错误

java.lang.ArrayIndexOutOfBoundsException:坐标超出界限!

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)

在洪水填充算法中不会完全他们抛出异常


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"

推荐答案

我会在这里开始搜索:



getting_lastpixel.java - 第49行



您可以通过在首选项中搜索并标记该功能来在eclipse中打开行号。



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



这有助于调试。
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.


关于调试Java代码的教程怎么样? Google [ ^ ]是你的朋友。
What about a tutorial on debugging Java code? Google[^] is your friend.


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>


这篇关于数组索引outof bound的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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