[英]Coordinate Out Of Bounds Exception With BufferedImage
現在,我正在嘗試制作游戲。 我在加載圖像時遇到問題。 為此,我必須在Main類中將圖像提供給removeImageBackground(BufferedImage img,Color tc)方法。 基本上,此方法的作用是獲取參數(img)中指定的圖像。 接下來發生的是,它通過一些for語句遍歷圖像,並且如果找到顏色與參數(tc)中指定的顏色匹配的像素,它將將該像素更改為透明。 我對此沒有任何問題,但是此方法接下來會發生的事情是將圖像展開(這是必須做的,因為我的游戲具有8位圖形)。 無論如何,它會創建一個名為outImg的新緩沖圖像。 在那之后,我有4個嵌套的語句。 最初的2個循環遍歷原始img圖像,最后2個循環遍歷新的outImg圖像。 這是在將5x5像素正方形寫入相應位置的新outImg圖像的同時讀取原始img圖像。 但是在這行代碼中,我遇到了問題。 我不斷收到arrayindexoutofboundsexceptions。 我嘗試放入System.out.println()語句嘗試進行調試,但似乎無法弄清問題所在。 這是我遇到問題的代碼部分,以及錯誤消息和調試結果:
我的主類中的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方法只是調用我的Gale()構造函數。 因此,這兩個都不應該有問題。
有人請幫我解決這個問題。 我很高興能完成這款游戲,這對我來說是一個很大的問題,因為它阻止了我加載所有內容。
真的很快...
for(int y2 = 0; y2 < graphicsScale; y++) {
for(int x2 = 0; x2 < graphicsScale; x++) {
您正在內部循環中遞增x
和y
,我相信您想將x2
和y2
遞增
for(int y2 = 0; y2 < graphicsScale; y2++) {
for(int x2 = 0; x2 < graphicsScale; x2++) {
原版的...
縮放比例(4x)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.