使用 BufferedImage 协调越界异常

Coordinate Out Of Bounds Exception With BufferedImage

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

我的 Main 中的 removeImageBackground 方法 Class:

//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 Class(在我的错误消息中指定):

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)

差不多就这些了。我的主要方法只是调用我的加载方法。我的加载方法只是调用我的 Gale() 构造函数。所以这两个都不应该有问题。

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

真快...

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++) {

原来...

缩放 (4x)