Skip to main content
gamedev.net
🔒 Locked

[java] Drawing: Omitting certain pixels

Started by Infinite_Daremo Mar 26, 2009 at 9:20 AM 3 replies 1.1k views
Original Post
Infinite_Daremo
Infinite_Daremo
Ive looked for a while now but i can't find a simple tutorial which explains how to not draw certain coloured pixels. Ive tried looping through each pixel of a bmp and setting it to whatever the blackground clear colour is but that isn't going to help in the long run. Is there a way to make the graphics device not draw a specific colour? Specifically magenta? Or is there a way to apply this colour key when loading an image? Similiar to SDL's load image function.
VildNinja
VildNinja
Quote:
Original post by Infinite_Daremo
Ive tried looping through each pixel of a bmp and setting it to whatever the background clear colour is but that isn't going to help in the long run.

If you use BufferedImage with color type TYPE_INT_ARGB, and calls setRGB(x, y, 0). The current pixel should become transparent IIRC.

Cakey
Cakey
If your using the graphics class-
BufferedImage image = new BufferedImage(rw, rh, BufferedImage.TYPE_INT_ARGB);Graphics g2d = image.createGraphics();Color transparent = new Color(0, 0, 0, 0);g2d.setColor(transparent);


Something like that perhaps?

But seriously look into GIMP it's a free tool for image editing and it's pretty dang easy to make a transparent .gif or a transparent .png
leif_dragon
leif_dragon
You can simply render the image as you load it into your program. It only will work properly with gif and png, there are many little errors that can happen with jpeg formatting. But really all you need is a custom made function called

BufferedImage renderTransparent(BufferedImage image, int rgb){}

the code might look something like this,

....
....
ImageIcon img = new ImageIcon("/yourImage.png");
BufferedImage image = new BufferedImage(img.getIconWidth(), img.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g2D = image.getGraphics()
g2D.paintIcon(null, g2D, 0, 0); // Draws the image into the BufferedImage
g2D.dispose();

image = renderTransparent(image, 0xFFFF0000); // Will make all red pixels transparent.
....
....

public BufferedImage renderTransparent(BufferedImage image, int rgb){
for(int x = 0; x < image.getWidth(); x++){
for(int y = 0; y < image.getHeight(); y++){
if(image.getRGB(x, y) == rgb) // If the color matches the specified rgb
image.setRGB(x, y, 0); // Sets the pixel to transparent
}
}
return image;
}

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.