Reputation: 25
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:
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 :(
Upvotes: 2
Views: 484
Reputation: 51835
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
Upvotes: 1