现在,我正在尝试制作游戏。我在加载图像时遇到问题。为此,我必须在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)