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

69 views Asked by At

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);

}
2

There are 2 answers

0
VGR On BEST ANSWER

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);
0
Teto On

How do you know that your x and y values are 99? Is it because render method puts everything in the same place? Can you open the block[][] array in an IDE? If not, consider printing out the contents of each block as you put it in the array.

Consider rewriting your loop like this.

public static void generateWorld() {
    Random random = new Random();
    Block result;

    for (int x = 0; x < block.length; x++) {
        for (int y = 0; y < block[0].length; y++) {
            if((y > 10) && ((random.nextInt(5) == 1)){
                result = Block.dirt;
            }
            else {
                result = Block.air;  
            }
            result.setBounds(x * Block.size, y * Block.size, Block.size, Block.size);
            block[x][y] = result;
            System.out.println ("block at position (" + x + "," + " + y + ") = " + result.x, result.y, result.width, result.height);
        }
    }
}

So I changed a few things. I don't re-instantiate Random every time, one instance is enough. And I use a "holder" variable to hold the value that goes into the array so that I can modify it without using the [x][y] notation. There is nothing wrong with that notation, but it would have been tedious in the println() statement. Finally I make sure to print out the contents of each block in turn. That should show you that your values are really being initialized correctly.

When you run this, you might consider reducing the size of the block[][] array to maybe 10x10. Just until you figure out what is going on.

If the blocks all print out properly when they initialize, then it tells you that something is wrong, then it tells you there is something wrong with the part of the code that is displaying the contents of the array. (which you didn't include).

I hope this helps.