使用BufferedImage协调超出界限异常

时间:2015-03-02 01:23:42

标签: java image swing indexoutofboundsexception

现在我正在尝试制作游戏。我在加载图片时遇到问题。为此,我必须在我的Main类中为removeImageBackground(BufferedImage img,Color tc)方法提供一个图像。基本上这个方法的作用是获取参数(img)中指定的图像。接下来会发生的是它通过一些for语句浏览图像,如果找到一个像素的颜色与参数(tc)中指定的颜色匹配,它会将该像素更改为透明。我对此没有任何问题但是接下来在这个方法中接下来发生的是图像然后被扩展(这必须完成,因为我的游戏有8位图形)。无论如何,它创建了一个名为outImg的新bufferedimage。之后我有4个嵌套for语句。前2个循环通过原始img图像,最后2个循环通过新的outImg图像。这样做是在将5x5像素正方形写入相应位置的新outImg图像时读取原始img图像。但是在这行代码中我遇到了问题。我一直在获得arrayindexoutofboundsexceptions。我尝试使用System.out.println()语句来尝试调试,但我似乎无法弄清楚错误是什么。这是我的代码中我遇到问题的部分,以及我的错误消息和调试结果:

我的Main Class中的removeImageBackground方法:

//removed the background of the given image with the given transparent color
public static BufferedImage removeImageBackground(BufferedImage img, Color tc) {
    System.out.println("Method Fired:Main.removeImageBackground(BufferedImage img, Color tc)"); //debug
    //remove the background
    for(int y = 0; y < img.getHeight(); y++) {
        for(int x = 0; x < img.getWidth(); x++) {
            Color c = new Color(img.getRGB(x, y));
            if(c == tc) {
                c = new Color(c.getRed(), c.getGreen(), c.getBlue(), 255);
            }
            img.setRGB(x, y, c.getRGB());
        }
    }

    //expand the image
    BufferedImage outImg = new BufferedImage(img.getWidth() * graphicsScale, img.getHeight() * graphicsScale, BufferedImage.TYPE_INT_ARGB);
    System.out.println("outImg height=" + outImg.getHeight());
    System.out.println("outImg width=" + outImg.getWidth());
    for(int y = 0; y < img.getHeight(); y++) {
        System.out.println("for y=" + y);
        System.out.println("img height=" + img.getHeight());
        for(int x = 0; x < img.getWidth(); x++) {
            System.out.println("for x=" + x);
            System.out.println("img width=" + img.getWidth());
            for(int y2 = 0; y2 < graphicsScale; y++) {
                for(int x2 = 0; x2 < graphicsScale; x++) {
                    outImg.setRGB((x * graphicsScale) + x2, (y * graphicsScale) + y2, img.getRGB(x, y));
                }
            }
        }
    }
    return outImg;
}

我的整个Gale类(在我的错误消息中指定):

package entity;

import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.awt.Color;
import java.io.File;
import java.io.IOException;

import main.Main;

public class Gale extends Player {
//variables
private static BufferedImage downStand;
private static BufferedImage leftStand;
private static BufferedImage rightStand;
private static BufferedImage upStand;
private static BufferedImage downWalk;
private static BufferedImage leftWalk;
private static BufferedImage rightWalk;
private static BufferedImage upWalk;

private static String objectName = "Gale";

//constructors
//used ONLY when loading
public Gale() throws IOException {
    System.out.println("Constructor Fired:Gale()"); //debug
    downStand = Main.removeImageBackground(ImageIO.read(new File(Main.filePath + Main.directory + Main.resourceFile + Main.directory + objectName + Main.directory + "DownStand.jpg")), Main.transparentColor);
    leftStand = Main.removeImageBackground(ImageIO.read(new File(Main.filePath + Main.directory + Main.resourceFile + Main.directory + objectName + Main.directory + "LeftStand.jpg")), Main.transparentColor);
    rightStand = Main.removeImageBackground(ImageIO.read(new File(Main.filePath + Main.directory + Main.resourceFile + Main.directory + objectName + Main.directory + "RightStand.jpg")), Main.transparentColor);
    upStand = Main.removeImageBackground(ImageIO.read(new File(Main.filePath + Main.directory + Main.resourceFile + Main.directory + objectName + Main.directory + "UpStand.jpg")), Main.transparentColor);
    downWalk = Main.removeImageBackground(ImageIO.read(new File(Main.filePath + Main.directory + Main.resourceFile + Main.directory + objectName + Main.directory + "DownWalk.jpg")), Main.transparentColor);
    leftWalk = Main.removeImageBackground(ImageIO.read(new File(Main.filePath + Main.directory + Main.resourceFile + Main.directory + objectName + Main.directory + "LeftWalk.jpg")), Main.transparentColor);
    rightWalk = Main.removeImageBackground(ImageIO.read(new File(Main.filePath + Main.directory + Main.resourceFile + Main.directory + objectName + Main.directory + "RightWalk.jpg")), Main.transparentColor);
    upWalk = Main.removeImageBackground(ImageIO.read(new File(Main.filePath + Main.directory + Main.resourceFile + Main.directory + objectName + Main.directory + "UpWalk.jpg")), Main.transparentColor);
}

//to be used for anything else
public Gale(int x, int y) {
    System.out.println("Constructor Fired:Gale(int x, int y)"); //debug
}

//methods
//returns the images
public BufferedImage getDownStand() {
    System.out.println("Method Fired:Gale.getDownStand()"); //debug
    return downStand;
}
public BufferedImage getLeftStand() {
    System.out.println("Method Fired:Gale.getLeftStand()"); //debug
    return leftStand;
}
public BufferedImage getRightStand() {
    System.out.println("Method Fired:Gale.getRightStand()"); //debug
    return rightStand;
}
public BufferedImage getUpStand() {
    System.out.println("Method Fired:Gale.getUpStand()"); //debug
    return upStand;
}
public BufferedImage getDownWalk() {
    System.out.println("Method Fired:Gale.getDownWalk()"); //debug
    return downWalk;
}
public BufferedImage getLeftWalk() {
    System.out.println("Method Fired:Gale.getLeftWalk()"); //debug
    return leftWalk;
}
public BufferedImage getRightWalk() {
    System.out.println("Method Fired:Gale.getRightWalk()"); //debug
    return rightWalk;
}
public BufferedImage getUpWalk() {
    System.out.println("Method Fired:Gale.getUpWalk()"); //debug
    return upWalk;
}

}

我的调试结果:

Method Fired:Main.removeImageBackground(BufferedImage img, Color tc)
outImg height=250
outImg width=125
for y=0
img height=50
for x=0
img width=25

我的错误讯息:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Coordinate out of bounds!
at sun.awt.image.ByteInterleavedRaster.getDataElements(ByteInterleavedRaster.java:318)
at java.awt.image.BufferedImage.getRGB(BufferedImage.java:888)
at main.Main.removeImageBackground(Main.java:101)
at entity.Gale.<init>(Gale.java:28)
at main.Main.load(Main.java:186)
at main.Main.main(Main.java:69)

几乎就是这样。我的主要方法只是调用我的load方法。而我的load方法只是调用我的Gale()构造函数。因此,不应该出现任何问题。

有人请帮我解决这个问题。我很高兴能够完成这个游戏,这对我来说是一个非常大的问题,因为它阻止我加载所有东西。

1 个答案:

答案 0 :(得分:1)

真的很快......

for(int y2 = 0; y2 < graphicsScale; y++) {
    for(int x2 = 0; x2 < graphicsScale; x++) {

您正在内循环中递增xy,我相信您希望增加x2y2

for(int y2 = 0; y2 < graphicsScale; y2++) {
    for(int x2 = 0; x2 < graphicsScale; x2++) {

...原始

enter image description here

缩放(4x)

enter image description here