2

recently I was thinking to make a game and I choose, for the making of the maps, to use an image. It's a normal image, like this:

Level Image Example

I wanted for each red pixel (255, 0, 0) to draw black rectangles 32x32 with an if statement. I made this but I can't get the RGB working:

loadMap() Method:

public void loadMap(BufferedImage image) {
    int w = image.getWidth();
    int h = image.getHeight();

    for(int xx = 0; xx < w; xx++) {
        for(int yy = 0; yy < h; yy++) {
            int pixel = image.getRGB(xx, yy);
            int alpha = (pixel >> 24) & 0xff;
            int red = (pixel >> 16) & 0xff;
            int green = (pixel >> 8) & 0xff;
            int blue = pixel & 0xff;                

            if(red == 255 && green == 0 && blue == 0)
                handler.addObject(new Wall(xx*32, yy*32, ID.Wall, this));

        }
    }
}

Wall Class:

public class Wall extends GameObject {

public Wall(int x, int y, ID id) {
    super(x, y, id);
}

public void update() {

}

public void render(Graphics g) {
    g.setColor(Color.black);
    g.fillRect(x, y, 32, 32);
}

public Rectangle getBounds() {
    return new Rectangle(x, y, 32, 32);
}
}

GameObject class:

public abstract class GameObject {

protected int x, y;
protected float speedX = 0, speedY = 0;
protected ID id;

public GameObject(int x, int y, ID id) {
    this.x = x;
    this.y = y;
    this.id = id;
}

public abstract void update();
public abstract void render(Graphics g);
public abstract Rectangle getBounds();

... //Getter's and Setter's
}

Can anyone explain what's happening, please? Am I doing something wrong?

Thanks in advance!

PS: I've checked the RGB values on the image and they are correct, but it still doesn't work :(

2
  • 1
    Might just be a typo in copying to SO, but you're missing an & after red: if(red == 255 & green == 0 && blue == 0)
    – Richard
    Commented Jul 11, 2018 at 17:07
  • @Richard Edited it, my bad xD. But it still doesn't work :/ Commented Jul 11, 2018 at 17:18

1 Answer 1

1

your image does not contain 0x00FF0000 it is rendered with different colors and anti-aliased. I found your image has these colors:

(217,0,0) // main red
(127,0,0) // main anti aliased red
( 63,0,0) // bleeded anti aliased red

So I would try this instead:

if ((red>120)&&(green==0)&&(blue==0)) ...;

I feel safer with the added () inside if conditions.

PS. it is refreshing to see Question from a low rep user that contains all the info we needed +1 for that

3
  • Is there a way to make the Red channel 255? I use Paint.net and the program says that I have R 255, G 0 and B 0... Can you suggest to me a good program to make this maps with the values I wanted? Commented Jul 12, 2018 at 11:50
  • 1
    @FrancescoMesiano I am using MS Paint for stuff like that. I do not code under .net so I could be wrong but I suspect you are rescaling the image somewhere along the way which can cause the bleeding behavior. Another option can be some theme or shader or transparency overlay enabled or transforming the image to some lossy compression or incompatible color width.
    – Spektre
    Commented Jul 12, 2018 at 13:10
  • Ok, thank you for giving your time to give me answers, I hope you have a good day! Commented Jul 12, 2018 at 13:26

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.