Right now i am trying to make a game. I am having problems with loading my images. To do so I have to give an image to the removeImageBackground(BufferedImage img, Color tc) method in my Main class. Basically what this method does is take the image specified in the parameters (img). What happens next is it goes through the image with some for statements and if it finds a pixel who's color matches the color specified in the parameters (tc) it changes that pixel to be transparent. I have no problem with this but then what happens next in this method is the image is then expanded (this has to be done because my game has 8-bit graphics). anyway it makes a new bufferedimage named outImg. I have 4 nested for statements after that. The first 2 cycle through the original img image and the last 2 cycle through the new outImg image. What this does is reads the original img image while writing 5x5 pixel squares into the new outImg image in the corresponding locations. But it's at this line of code that I have a problem. I keep getting arrayindexoutofboundsexceptions. I tried putting in System.out.println() statements to try to debug, but I can't seem to figure out what's wrong. Here's the part of my code where I'm having problems, along with my error messages and my debug results:
My removeImageBackground method from my Main 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;
}
My entire Gale Class (Which is specified in My error messages):
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;
}
}
My Debug Results:
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
My Error Messages:
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)
That's pretty much it. My main method just calls on my load method. And my load method just calls on my Gale() constructor. So there shouldn't be a problem in either of those.
Someone please help me fix this. I'm really excited to get this game finished and this is becoming a very big problem for me since it is preventing me from loading everything.
Aucun commentaire:
Enregistrer un commentaire