Results 1 to 8 of 8
  1. #1
    samcoles is offline Member
    Join Date
    Feb 2010
    Posts
    4
    Rep Power
    0

    Default 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

  2. #2
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    6

    Default

    "... 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 06:42 PM.
    My Hobby Project: LegacyClone

  3. #3
    samcoles is offline Member
    Join Date
    Feb 2010
    Posts
    4
    Rep Power
    0

    Default

    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.

  4. #4
    samcoles is offline Member
    Join Date
    Feb 2010
    Posts
    4
    Rep Power
    0

    Default

    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.

  5. #5
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    6

    Default

    I added some more to my original post; i hit the done too soon, hehe.
    My Hobby Project: LegacyClone

  6. #6
    samcoles is offline Member
    Join Date
    Feb 2010
    Posts
    4
    Rep Power
    0

    Default

    The only thing I don't get about that piece of code - how does it know which colour to make transparent?

  7. #7
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    6

    Default

    sorry, the piece of code doesn't remove the color, just creates an efficient image to draw to the screen :)
    My Hobby Project: LegacyClone

  8. #8
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    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

  1. creating a BufferedImage from a drawing
    By chappa in forum Java 2D
    Replies: 2
    Last Post: 01-10-2010, 07:04 PM
  2. Using BufferedImage
    By timkd127 in forum New To Java
    Replies: 5
    Last Post: 12-19-2009, 10:17 PM
  3. BufferedImage imageScaling
    By MINGxDOG in forum New To Java
    Replies: 2
    Last Post: 11-17-2009, 04:04 PM
  4. BufferedImage through FTP
    By dudejonne in forum New To Java
    Replies: 7
    Last Post: 11-05-2009, 06:36 PM
  5. BufferedImage to Byte
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-22-2008, 09:17 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •