Results 1 to 5 of 5
  1. #1
    playwin2 is offline Member
    Join Date
    Aug 2008
    Posts
    58
    Rep Power
    0

    Default Trouble retaining transparent back color while setting opacity

    Hello,
    I'm trying to set opacity of an image and also making it's back transparent....

    The program suppose to mark the back color (the 1st pixel's color) of an image to transparent and also define the whole opacity (translucent) of the image...

    The problem is if I, first make the image transparent and then apply the opacity the image is loosing it's backcolor :confused:

    However if I do opposite that is , first apply opacity and then make it transparent then everythings is OK.

    Why is this happening?

    Bellow is the code that make the opacity...
    Java Code:
    private BufferedImage getTranslucentImage(float opacity)
        {
                   
            int width = img.getWidth();
            int height = img.getHeight();
    
    
            BufferedImage tmp = new BufferedImage(width, height,
                    BufferedImage.TRANSLUCENT);
    
            Graphics2D g2 = tmp.createGraphics();
            g2.setComposite(AlphaComposite.getInstance(
                    AlphaComposite.SRC_OVER, opacity));
            g2.drawImage(img, null, 0, 0);
            g2.dispose();
    
            return tmp;
        }
    Last edited by playwin2; 03-18-2009 at 08:37 PM.

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

    Default

    Java Code:
    BufferedImage tmp = new BufferedImage(width, height,
            BufferedImage.TRANSLUCENT);
    Translucent is a field of the Transparency interface which BufferedImage implements. Its value is 3.
    The BufferedImage constructor you are using takes an int imageType. The types are listed in the Field Summary section of the BufferedImage api. The type whose value is 3 is TYPE_INT_ARGB_PRE. This may be what you want. Try experimenting with the types.

  3. #3
    playwin2 is offline Member
    Join Date
    Aug 2008
    Posts
    58
    Rep Power
    0

    Default

    Hello,

    now I can preserve the back-color with transparency,

    I just find and set the back color and then paint a rect (same size as the image) and then draw the image on it and then apply transparency, this preservs the back color with transparency.

    However now I got problem to read them back in java ( I saved the image as png) , Does anyone know how to read TYPE_CUSTOM images in java?

  4. #4
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    6

    Default

    You mean you have the problem that when you save an image Java does not read it's correct transparency?

    In that case read the color per pixel and use this (which works for me):
    Color c;

    RED: (c>>>16)&0xff
    GREEN: (c>>>8)&0xff
    BLUE: c&0xff
    ALPHA (tranparency): (c>>>24)&0xff

    Hope this helps...
    Last edited by Supamagier; 03-24-2009 at 09:55 PM. Reason: Added example
    I die a little on the inside...
    Every time I get shot.

  5. #5
    playwin2 is offline Member
    Join Date
    Aug 2008
    Posts
    58
    Rep Power
    0

    Default

    No, I meant i can not preserve transparent backcolor when setting opacity , drawImage makes it black :mad:, if alpha is 0.

    However presently I'm doing this and it seems working.... what do you think?
    Java Code:
    private BufferedImage getTranslucentPic(BufferedImage img, float opacity)
        {
            if( ! isBackTrans(img))
            {
                return setOpacity(img, opacity);
            }
    
            int width = img.getWidth();
            int height = img.getHeight();
            int oldBackColor = getBackColor(img);
    
            BufferedImage tmp = new BufferedImage(width, height,
                    BufferedImage.TYPE_INT_ARGB);
    
            Graphics2D g2 = tmp.createGraphics();
            g2.setComposite(AlphaComposite.getInstance(
                    AlphaComposite.SRC_OVER, opacity));
            g2.drawImage(img, null , 0, 0);
            g2.dispose();
    
            int newBackColor = tmp.getRGB(0, 0);
    
            for(int i = 0; i < height; i++)
            {
                for(int  j = 0; j < width; j++)
                {
                    if( newBackColor == tmp.getRGB(j, i))
                    {
                        tmp.setRGB(j, i, oldBackColor);
                    }
                }
            }
    
            return tmp;
        }
    
        private BufferedImage setOpacity(
                BufferedImage img,
                float opacity)
        {
            int width = img.getWidth();
            int height = img.getHeight();
    
            BufferedImage tmp = new BufferedImage(width, height,
                    BufferedImage.TYPE_INT_ARGB);
            Graphics2D g2 = tmp.createGraphics();
            g2.setComposite(AlphaComposite.getInstance(
                    AlphaComposite.SRC_OVER, opacity));
            g2.drawImage(img, null, 0, 0);
            g2.dispose();
    
            return tmp;
        }

Similar Threads

  1. transparent jframe?
    By majkel in forum AWT / Swing
    Replies: 12
    Last Post: 10-09-2010, 05:06 PM
  2. SWT Transparent button
    By ynnorj in forum SWT / JFace
    Replies: 2
    Last Post: 03-04-2009, 06:33 AM
  3. setting background color of JFrame form with NetBeans 6.1
    By onefootswill in forum New To Java
    Replies: 4
    Last Post: 08-12-2008, 07:02 AM
  4. Replies: 0
    Last Post: 05-22-2008, 10:53 AM
  5. Transparent JTextPane
    By Ada in forum AWT / Swing
    Replies: 1
    Last Post: 05-31-2007, 09:50 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
  •