Ошибка RasterFormatException после замены файла png - PullRequest
0 голосов
/ 20 мая 2019
//PlayState class
package com.csgo.game.states;

import com.csgo.game.graphics.Font;
import com.csgo.game.graphics.Sprite;
import com.csgo.game.util.KeyHandler;
import com.csgo.game.util.MouseHandler;
import com.csgo.game.util.Vector2f;

import java.awt.*;

public class PlayState extends GameState {

private Font font;

public PlayState(GameStateManager gsm) {
    super(gsm);
    font = new Font("font/ZeldaFont.png", 16, 16);
}

public void update() {

}

public void input(MouseHandler mouse, KeyHandler key) {

}

public void render(Graphics2D g) {
    Sprite.drawArray(g, font, "A", new Vector2f(100, 100), 16, 16, 16, 
0);
}
}


//Font class

package com.csgo.game.graphics;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;

public class Font {

private final int TILE_SIZE = 32;
public int w;
public int h;
private BufferedImage FONTSHEET = null;
private BufferedImage[][] spriteArray;
private int wLetter;
private int hLetter;

public Font(String file) {
    w = TILE_SIZE;
    h = TILE_SIZE;

    System.out.println("Loading: " + file + "...");
    FONTSHEET = loadFont(file);

    wLetter = FONTSHEET.getWidth() / w;
    hLetter = FONTSHEET.getHeight() / h;
    loadSpriteArray();
}

public Font(String file, int w, int h) {
    this.w = w;
    this.h = h;

    System.out.println("Loading: " + file + "...");
    FONTSHEET = loadFont(file);

    wLetter = FONTSHEET.getWidth() / w;
    hLetter = FONTSHEET.getHeight() / h;
    loadSpriteArray();
}

public void setSize(int width, int height) {
    setWidth(width);
    setHeight(height);
}

public int getWidth() {
    return w;
}

public void setWidth(int i) {
    w = i;
    wLetter = FONTSHEET.getWidth() / w;
}

public int getHeight() {
    return h;
}

public void setHeight(int i) {
    h = i;
    hLetter = FONTSHEET.getHeight() / h;
}

private BufferedImage loadFont(String file) {
    BufferedImage sprite = null;
    try {
        sprite =         
ImageIO.read(getClass().getClassLoader().getResourceAsStream(file));
    } catch (Exception e) {
        System.out.println("ERROR: could not load file: " + file);
    }
    return sprite;
}

public void loadSpriteArray() {
    spriteArray = new BufferedImage[wLetter][hLetter];
    for (int x = 0; x < wLetter; x++) {
        for (int y = 0; y < wLetter; y++) {
            spriteArray[x][y] = getLetter(x, y);
        }
    }
}

public BufferedImage getFontSheet() {
    return FONTSHEET;
}

public BufferedImage getLetter(int x, int y) {
    return FONTSHEET.getSubimage(x*w , y*h , w, h);
}

public BufferedImage getFont(char letter) {
    int value = letter - 1;
    int x = value % wLetter;
    int y = value / wLetter;
    return getLetter(x, y);
}
}

//Sprite class

package com.csgo.game.graphics;

import com.csgo.game.util.Vector2f;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.ArrayList;

public class Sprite {

private BufferedImage SPRITESHEET = null;
private final int TILE_SIZE = 32;
public int w;
public int h;
private BufferedImage[][] spriteArray;
private int wSprite;
private int hSprite;

public Sprite(String file) {
    w = TILE_SIZE;
    h = TILE_SIZE;

    System.out.println("Loading: " + file + "...");
    SPRITESHEET = loadSprite(file);

    wSprite = SPRITESHEET.getWidth() / w;
    hSprite = SPRITESHEET.getHeight() / h;
    loadSpriteArray();
}

public Sprite(String file, int w, int h) {
    this.w = w;
    this.h = h;

    System.out.println("Loading: " + file + "...");
    SPRITESHEET = loadSprite(file);

    wSprite = SPRITESHEET.getWidth() / w;
    hSprite = SPRITESHEET.getHeight() / h;
    loadSpriteArray();
}

public static void drawArray(Graphics2D g, ArrayList<BufferedImage> img,     
Vector2f pos, int width, int height, int xOffset, int yOffset) {
    float x = pos.x;
    float y = pos.y;

    for (int i = 0; i < img.size(); i++) {
        if (img.get(i) != null) {
            g.drawImage(img.get(i), (int) x, (int) y, width, height, 
null);
        }

        x += xOffset;
        y += yOffset;
    }
}

public static void drawArray(Graphics2D g, Font f, String word, Vector2f 
pos, int width, int height, int xOffset, int yOffset) {
    float x = pos.x;
    float y = pos.y;

    for (int i = 0; i < word.length(); i++) {
        if (word.charAt(i) != 32)
            g.drawImage(f.getFont(word.charAt(i)), (int) x, (int) y, 
width, height, null);
        x += xOffset;
        y += yOffset;
    }
}

public void setSize(int width, int height) {
    setWidth(width);
    setHeight(height);
}

public int getWidth() {
    return w;
}

public void setWidth(int i) {
    w = i;
    wSprite = SPRITESHEET.getWidth() / w;
}

public int getHeight() {
    return h;
}

public void setHeight(int i) {
    h = i;
    hSprite = SPRITESHEET.getHeight() / h;
}

private BufferedImage loadSprite(String file) {
    BufferedImage sprite = null;
    try {
        sprite = 
ImageIO.read(getClass().getClassLoader().getResourceAsStream(file));
    } catch (Exception e) {
        System.out.println("ERROR: could not load file: " + file);
    }
    return sprite;
}

public void loadSpriteArray() {
    spriteArray = new BufferedImage[wSprite][hSprite];
    for (int x = 0; x < wSprite; x++) {
        for (int y = 0; y < wSprite; y++) {
            spriteArray[x][y] = getSprite(x, y);
        }
    }
}

public BufferedImage getSpriteSheet() {
    return SPRITESHEET;
}

public BufferedImage getSprite(int x, int y) {
    return SPRITESHEET.getSubimage(x * w, y * h, w, h);
}

public BufferedImage[] getSpriteArray(int i) {
    return spriteArray[i];
}

public BufferedImage[][] getSpriteArray2(int i) {
    return spriteArray;
}
}

Вот стек ошибок:

Exception in thread "GameThread" java.awt.image.RasterFormatException: (y + height) is outside of Raster
    at java.desktop/sun.awt.image.ByteInterleavedRaster.createWritableChild(ByteInterleavedRaster.java:1243)
    at java.desktop/java.awt.image.BufferedImage.getSubimage(BufferedImage.java:1202)
    at com.csgo.game.graphics.Font.getLetter(Font.java:87)
    at com.csgo.game.graphics.Font.loadSpriteArray(Font.java:77)
    at com.csgo.game.graphics.Font.<init>(Font.java:37)
    at com.csgo.game.states.PlayState.<init>(PlayState.java:17)
    at com.csgo.game.states.GameStateManager.<init>(GameStateManager.java:24)
    at com.csgo.game.GamePanel.init(GamePanel.java:51)
    at com.csgo.game.GamePanel.run(GamePanel.java:55)
    at java.base/java.lang.Thread.run(Thread.java:834)
...