有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

从BuffereImage渲染精灵时出现java问题

我有一个问题,在改编了游戏创建教程中的代码后,我没有得到错误。它告诉我我在家里有一种超乎寻常的感觉 pixels[x + y * SIZE] = sheet.pixels[(x + this.x1) + (y + this.y1) * sheet.SIZE]; 但我不明白为什么。这是我的代码,谢谢你的帮助。屏幕的渲染是在main方法中完成的,这不会是错误,因为使用教程代码可以很好地工作

public class Sprite {

public static final Sprite GRASS = new Sprite(1, 1, 15, 15, SpriteSheet.ENVIRONMENT_SHEET);

public final int SIZE;
private int x1, y1, x2, y2;
private SpriteSheet sheet;
public int[] pixels;

public Sprite(int x1, int y1, int x2, int y2, SpriteSheet sheet){
    this.sheet = sheet;
    this.SIZE = x2 - x1;
    this.x1 = x1;
    this.y1 = y1;
    this.x2 = x2;
    this.y2 = y2;
    this.pixels = new int[SIZE * SIZE];
    load();
}

private void load(){
    for (int y = this.y1; y < this.y2; y++) {
        for (int x = this.x1; y < this.x2; x++) {
            pixels[x + y * SIZE] = sheet.pixels[(x + this.x1) + (y + this.y1) * sheet.SIZE];
        }
    }
}

}

public class SpriteSheet {

public static final SpriteSheet ARCHER_SHEET = new SpriteSheet("/textures/Archer_Sheet.png", 1024);
public static final SpriteSheet ENVIRONMENT_SHEET = new SpriteSheet("/textures/Environment_Sheet.png", 128);


private String path;
public final int SIZE;
public int[] pixels;

public SpriteSheet(String path, int size){
    this.path = path;
    this.SIZE = size;
    this.pixels = new int[SIZE * SIZE];
    load();
}

private void load(){
    try {
        BufferedImage image = ImageIO.read(SpriteSheet.class.getResource(path));
        int width = image.getWidth();
        int height = image.getHeight();
        image.getRGB(0, 0, width, height, pixels, 0, width);
        System.out.println("Loading image from path: " + SpriteSheet.class.getResource(path));
    } catch (IOException e) {
        e.printStackTrace();
    }
}

}

public class Screen {

private int width, height;
public int[] pixels;

public int[] tiles =  new int[GameData.mapSize * GameData.mapSize];

private Random random = new Random();

public Screen(int width, int height){
    this.width = width;
    this.height = height;
    this.pixels = new int[width * height];

    for (int i = 0; i < GameData.mapSize * GameData.mapSize; i++) {
        tiles[i] = random.nextInt(0xFFFFFF);
    }
}

public void clearScreen(){
    for (int i = 0; i < pixels.length; i++) {
        pixels[i] = 0;
    }
}

public void render(int xOffset, int yOffset){

    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            pixels[x + y * width] = Sprite.GRASS.pixels[(x & (Sprite.GRASS.SIZE - 1)) + (y & (Sprite.GRASS.SIZE - 1)) * Sprite.GRASS.SIZE];
        }
    }
}

}


共 (1) 个答案

  1. # 1 楼答案

    问题似乎来自此代码

    for (int y = this.y1; y < this.y2; y++) {
        for (int x = this.x1; y < this.x2; x++) {
            pixels[x + y * SIZE] = sheet.pixels[(x + this.x1) + (y + this.y1) * sheet.SIZE];
        }
    }
    

    让我们计算一下:

    x1=1
    y1=1
    x2=15
    y2=15
    尺寸=14(x2-x1)

    现在让我们看看你的代码

    this.pixels = new int[SIZE * SIZE];
    //creates an array of size 196
    //so pixels.length == 196
    

    现在让我们为最坏的情况插入for循环(所有值都处于最高值)

    for (int y = (1); y < (15); y++) {
        for (int x = (1); y < (15); x++) {
            pixels[14 + 14 * 14] = sheet.pixels[(14 + 1) + (14 + 1) * sheet.SIZE];
        }
    }
    

    我们简化了表达式

    pixels[210] = sheet.pixels[(15) + (15) * sheet.SIZE];
    

    正如你所看到的,该指数仅高达196,我们正在尝试访问指数210。(根据阵列的大小,右侧的阵列也可能超出边界。)

    注意:还要注意,在执行(x + y * SIZE)操作时,y*大小将首先出现,然后根据操作顺序添加x