Results 1 to 8 of 8
Thread: Setting alpha in BufferedImage
- 02-10-2010, 04:16 PM #1
Member
- Join Date
- Feb 2010
- Posts
- 4
- Rep Power
- 0
Setting alpha in BufferedImage
Hi,
I remember when I programmed a few SDL demos a few years back there was a simple function to set a colour on a source image to alpha so it wasn't blitted to the screen. I've searched up and the function was SDL_SetColorKey().
I've begun programming PacMan in Java at the moment, I've made a sprite class, it all loads from files and animates and such... I'm pretty pleased thus far, except for the lack of transparency in the magenta areas that are #FF00FF. I've Googled and found some pretty long winded ways of doing it that all looked a bit confusing, I've also trawled through the API and I still can't really find what I'm looking for... I'd find it hard to believe the Java API doesn't have a similarly easy to use function like SDL_SetColorKey(). The sprite image is stored as a 'BufferedImage' once loaded.
Can anyone point me in the right direction for what I should be looking at? Hoping that something as simple exists but if it doesn't and the simple solution would just be to save the images as PNGs with transparency already built into the image then I'll do that.
Cheers
Sam
- 02-10-2010, 05:36 PM #2
"... Hoping that something as simple exists but if it doesn't and the simple solution would just be to save the images as PNGs with transparency already built into the image then I'll do that."
That's what i ended up having to do. Or when you load the image you can redraw it to bufferedimage and strip out the color yourself. If you want a effiency boost then create the image that is compatible to the panels graphics object:
Java Code:BufferedImage image= ((Graphics2D)this.getGraphics()).getDeviceConfiguration().createCompatibleImage( width,height,java.awt.Transparency.BITWISE);Last edited by mrmatt1111; 02-10-2010 at 05:42 PM.
My Hobby Project: LegacyClone
- 02-10-2010, 05:37 PM #3
Member
- Join Date
- Feb 2010
- Posts
- 4
- Rep Power
- 0
I've achieved this by simply using PNGs with transparency by deleting the magenta around my sprites in GIMP and exporting. Probably makes just as much sense doing it this way anyway but if anyone knows a solution to what I was trying to achieve in the first place it would still be very much appreciated for expanding my sprite class in the future.
Sam.
- 02-10-2010, 05:39 PM #4
Member
- Join Date
- Feb 2010
- Posts
- 4
- Rep Power
- 0
We both posted the same time, your post wasn't there when I posted the above. Lol. I'm glad I'm not the only one who just gave up and went for PNGs. I thought it was such an obvious method the API would have something just as simple.
The API does appear to have methods for achieving this kind of thing but I'll be damned if I could make any sense of it myself.
- 02-10-2010, 05:44 PM #5
- 02-10-2010, 05:46 PM #6
Member
- Join Date
- Feb 2010
- Posts
- 4
- Rep Power
- 0
The only thing I don't get about that piece of code - how does it know which colour to make transparent?
- 02-10-2010, 06:05 PM #7
sorry, the piece of code doesn't remove the color, just creates an efficient image to draw to the screen :)
My Hobby Project: LegacyClone
- 02-12-2010, 05:07 AM #8
Java Code:import java.awt.*; import java.awt.image.BufferedImage; import javax.swing.*; public class Test { /** * Adapted from code found in j2se 1.5 jdk: * /src/com/sun/java/swing/plaf/windows/XPStyle */ private BufferedImage convert(BufferedImage src, Color color) { int w = src.getWidth(); int h = src.getHeight(); int type = Transparency.BITMASK; BufferedImage dst = getCompatibleImage(w,h,type); int rgb; if(color != null) rgb = color.getRGB() | 0xff000000; else rgb = 0xffff00ff; // Copy pixels a scan line at a time. int buf[] = new int[w]; for(int y = 0; y < h; y++) { src.getRGB(0, y, w, 1, buf, 0, w); for(int x = 0; x < w; x++) { if(buf[x] == rgb) buf[x] = 0; } dst.setRGB(0, y, w, 1, buf, 0, w); } return dst; } private JScrollPane getContent() { BufferedImage image = getImage(); Color color = Color.magenta; BufferedImage modified = convert(image, color); JPanel panel = new JPanel(new GridLayout(1,0,5,0)); panel.setBackground(Color.black); panel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5)); panel.add(new JLabel(new ImageIcon(image))); panel.add(new JLabel(new ImageIcon(modified))); return new JScrollPane(panel); } private BufferedImage getImage() { int w = 300, h = 300, type = Transparency.OPAQUE; BufferedImage image = getCompatibleImage(w,h,type); Graphics2D g2 = image.createGraphics(); g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.setPaint(Color.magenta); g2.fillRect(0,0,w,h); g2.setPaint(Color.blue); g2.drawRect(0,0,w-1,h-1); g2.setPaint(Color.green.darker()); g2.fillRect(w/4,h/4,w/2,h/2); g2.dispose(); return image; } private BufferedImage getCompatibleImage(int w, int h, int type) { return GraphicsEnvironment.getLocalGraphicsEnvironment() .getDefaultScreenDevice() .getDefaultConfiguration() .createCompatibleImage(w,h,type); } public static void main(String[] args) { JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(new Test().getContent()); f.pack(); f.setLocation(200,200); f.setVisible(true); } }
Similar Threads
-
creating a BufferedImage from a drawing
By chappa in forum Java 2DReplies: 2Last Post: 01-10-2010, 06:04 PM -
Using BufferedImage
By timkd127 in forum New To JavaReplies: 5Last Post: 12-19-2009, 09:17 PM -
BufferedImage imageScaling
By MINGxDOG in forum New To JavaReplies: 2Last Post: 11-17-2009, 03:04 PM -
BufferedImage through FTP
By dudejonne in forum New To JavaReplies: 7Last Post: 11-05-2009, 05:36 PM -
BufferedImage to Byte
By Java Tip in forum Java TipReplies: 0Last Post: 01-22-2008, 08:17 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks