Search code examples
javasetbounds

Why are bounds not being set for rectangles and returning to the same int?


Im trying to set the x and y of these rectangles but any time i try they keep setting at 99. No matter where i set it it keeps going to that. As far as i know the only thing i can think of is that the Block classes x and y made inside the classare set to 99 and when i sat a block in the array to that it changes to 99. but im setting bounds after it sets the block so thats why im confused.

Setting blocks:

public class World {

public static Block[][] block = new Block[100][100];
public static Character character = new Character(40, 20, "Player001");
public static Point mse = new Point(0, 0);

public static void generateWorld() {
    for (int x = 0; x < block.length; x++) {
        for (int y = 0; y < block[0].length; y++) {
            block[x][y] = Block.air;
            if(y > 10) {
                if(new Random().nextInt(5) == 1){
                    block[x][y] = Block.dirt;
                }
            }
            block[x][y].setBounds(new Rectangle(x * Block.size, y * Block.size, Block.size, Block.size));
        }
    }
}

public static void tick() {
    System.out.println(block[5][5].x);
    character.tick();
    for (int x = 0; x < block.length; x++) {
        for (int y = 0; y < block[0].length; y++) {
            block[x][y].tick();
        }
    }
}

public static void render(Graphics g) {
    for (int x = 0; x < block.length; x++) {
        for (int y = 0; y < block[0].length; y++) {
            g.drawImage(block[x][y].image, x * Block.size
                    - Character.camX,
                    y * Block.size + Character.camY,
                    Block.size, Block.size, null);
        }
    }
    character.render(g);
}

}

Block class:

public class Block extends Rectangle {

private static final long serialVersionUID = 6859844338993954281L;

public static int size = 16;
public String name;
public int id;
public Image image;
public static int blockCount = 0;
public boolean isSolid;

public Block(String name, int id, boolean isSolid) {
    this.name = name;
    this.id = id;
    this.isSolid = isSolid;
    try {
        image = ImageIO.read(getClass().getResourceAsStream(
                "/Blocks/" + name + ".png"));
    } catch (Exception e) {
        e.printStackTrace();
    }
    blockCount++;
}

public void tick() {

}

public static Block air = new Block("air", 0, false);
public static Block dirt = new Block("dirt", 1, true);

}

Solution

  • Block.air is just one object. You are setting most of your block array to contain references to that single object, which causes all of those elements to share that Block object:

    Array elements pointing to Block.air

    As you might expect, then, calling setBounds immediately alters the original Block.air, and most of the elements see that change (since most of the elements point to the same shared Block.air object).

    What you need to do is make each element of your block array its own object. The clone() method inherited from Rectangle is probably the easiest way:

    Random random = new Random();
    block[x][y] = (Block) Block.air.clone();
    if (y > 10) {
        if (random.nextInt(5) == 1) {
            block[x][y] = (Block) Block.dirt.clone();
        }
    }
    block[x][y].setBounds(x * Block.size, y * Block.size, Block.size, Block.size);