Question

I am trying to make a RPG game in Java and i have run into a problem when it comes to building the level. It is not getting the hexidecimal code correct. Here is the loop and switch statement that is in question:

public void loadLevel(BufferedImage levelImage){
    tiles = new int[levelImage.getWidth()][levelImage.getHeight()];
    for (int y = 0; y < h; y++) {
        for (int x = 0; x < w; x++) {
            Color c = new Color(levelImage.getRGB(x, y));
            String h = String.format("&02x%02x%02x", c.getRed(),c.getGreen(),c.getBlue());

            switch(h){
            case "00ff00"://GRASS Tile - 1
                tiles[x][y] = 1;
                System.out.println("Found Grass");
                break;
            case "808080"://Stone -2
                tiles[x][y] = 2;
                System.out.println("Found STONE");
                break;
            case "894627"://Dirt -3
                tiles[x][y] = 3;
                break;
            case "404040"://Rock -4
                tiles[x][y] = 4;
                break;
            case "00b700"://Tree -5
                tiles[x][y] = 5;
                break;
            case"000000"://Wall -6
                tiles[x][y] = 6;
                break;
            default:
                tiles[x][y] = 1;
                System.out.println("Did not find anything");
                System.out.println(h);
                break;

            }

right now, the string is being set as some variation of &02x0000 Any ideas/revisions would be very helpful!

Was it helpful?

Solution

Changed &02x%02x%02x to %02x%02x%02x. Fixed thanks to Jesper

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top