线程"main"中的异常java.lang.ArrayIndexOutOfBoundsException:坐标超出范围setRGB [英] Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Coordinate out of bounds setRGB

查看:208
本文介绍了线程"main"中的异常java.lang.ArrayIndexOutOfBoundsException:坐标超出范围setRGB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是编程新手,目前正在开发一个程序,该程序可以将图像左右旋转.我能够使用上下颠倒方法,但无法向右旋转(顺时针旋转90度).它不断给我带来错误,并且在查看其他示例时也不确定为什么.任何帮助将不胜感激.

I am new to programming and I'm currently working on a program that rotates an image to the right and upside down. I was able to get the upside down method working but not the rotate to the right (90 degrees clockwise). It keeps giving me an out of bounds error, and I'm not sure why as I have looked at other examples. Any help would be appreciated.

这是我正在研究的方法:

Here's is the method that I'm working on:

public Image rotateRight()
{
  Image right = new Image (this);
  img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  int width = right.img.getWidth();
  int height = right.img.getHeight();


  for (int i = 0; i < width; i++)
        for (int j = 0; j < height; j++)
        {
            this.img.setRGB(height-j-1,i,right.img.getRGB(i,j));

        }
   return right;
}

这是其余的代码:

import java.awt.image.*;
import java.io.*;
import javax.imageio.*;

public class Image {

private BufferedImage img = null;
int width;
int height;

private Image()
{
}

public Image (int w, int h)
{
    img = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB );
   width = w;
    height = h;
}

public Image (Image anotherImg)
{
width = anotherImg.img.getWidth();
    height = anotherImg.img.getHeight();
    img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB );

    for (int i = 0; i < height; i++)
        for (int j = 0; j < width; j++)
        {
            this.img.setRGB(j,i,anotherImg.img.getRGB(j,i)) ;
        }

}


public String toString()
{
    return "Width of Image:" +width+"\nHeight of Image:"+height;
}


public Image (String filename)
{
    try
    {
        img = ImageIO.read(new File(filename));
        width = img.getWidth();
         height = img.getHeight();
    }
    catch (IOException e)
    {
            System.out.println(e);
    }
}

public void save(String filename, String extension)
{
    try
    {
        File outputfile = new File(filename);
        ImageIO.write(img, extension, outputfile);
    }
    catch (IOException e)
    {
        System.out.println(e);
    }
}
public Image copy()
{
    Image copiedImage = new Image(this);
    return copiedImage;
}

主要内容:

  public static void main (String args[])
{
    Image srcimg = new Image("apple.jpg");

    System.out.println(srcimg);

  Image copy = srcimg.copy();
    copy.save("apple-copy.jpg", "jpg");
  Image copy2 = srcimg.copy();


  Image right = copy2.rotateRight();

  right.save("apple-rotateRight.jpg", "jpg");



}   

推荐答案

旋转图像时出现ArrayIndexOutOfBoundsException的原因如上所述.超出范围.可能是您的i变量超出了范围,也可能是j变量超出了范围,通常只需在for循环中添加一条print语句,然后检查两个值中的哪一个超出范围,即可轻松进行测试界限.尝试自己解决这些问题是一个好习惯,因为您将开始学习导致这些问题的原因以及问题所在.

The reason you are getting an ArrayIndexOutOfBoundsException when rotating your image is as stated. Something is out of bounds. It could be either your i variable that has exceeded its bounds or your j variable that has exceeded its bounds and this is generally easy to test for by just adding a print statement within your for loop and checking which one of the two values is out of bounds. It is good practice to try to resolve these problems yourself as you will start learning what causes these and where the problem lies.

无论如何我的漫无目的.您似乎遇到的问题是,您试图在不改变图像尺寸的情况下转动图像.

Anyways enough of my rambling. The problem that you seem to have is that you are trying to turn the image without changing the size of the image.

您要创建一个具有与原始图片相同的宽度和高度参数的新图片

You are creating a new Image with the same width and height parameters as the original

img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB );

但是,当您要将图像旋转90度时,实际上需要翻转width和height参数.如果您考虑一下,当将图像旋转90度时,宽度将变为高度,而高度将变为宽度,这是有道理的.

However when you want to rotate an image by 90 degrees you actually want to flip the width and height parameters. If you think about it, it makes sense when you rotate an image by 90 degrees the width will become the height and the height will become the width.

所以您的问题在这里:

this.img.setRGB(height-j-1,i,right.img.getRGB(i,j));

在您的情况下,setRGB函数中x参数的范围是从0到图像的宽度,y参数的范围是从0到图像的高度.因此,因为您的高度变量与宽度不同.例如,如果您的WIDTH为200,而HEIGHT为100.将其放入函数中时,x参数的最大值将为:

In your case the bounds for the x parameter in the setRGB function is from 0 to the WIDTH of your image and the y parameter is from 0 to the HEIGHT of your image. Therefore because your height variable is different from your width. If for example your WIDTH is 200 and your HEIGHT is 100. When you put this in to the function the greatest value for the x parameter will be:

'100-199-1 = -100'显然超出范围.但是,如果我们将您的代码更改为.

'100 - 199 - 1 = -100' which is clearly out of bounds. However if we change your code to.

img = new BufferedImage(height, width, BufferedImage.TYPE_INT_RGB );

现在,当我们做与以前相同的事情时,我们将获得最大可能的值.

now when we do the same thing as before where we get the maximum possible value.

宽度= 100,高度= 200;

WIDTH = 100, HEIGHT = 200;

"200-99-1 = 100"位于边界之内

'200 - 99 - 1 = 100' which is inside the bounds

这篇关于线程"main"中的异常java.lang.ArrayIndexOutOfBoundsException:坐标超出范围setRGB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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