Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Setting a transparency level of a picture issue

  1. #1
    Member
    Join Date
    Jun 2010
    Posts
    48
    Thanks
    12
    Thanked 2 Times in 2 Posts

    Default Setting a transparency level of a picture issue

    Hello,
    I have such method:
    private ImageIcon createSelectionPattern(int x, int y) {
    		BufferedImage bi = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration().createCompatibleImage(x, y, Transparency.BITMASK);
    		Graphics2D g2 = bi.createGraphics();
    		g2.setPaint(getFill());
    		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    		g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
    		int INS = x / 6;
    		g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.20f));
    		g2.fillRoundRect(0, 0, x, y, INS, INS);
    		g2.dispose();
    		return new ImageIcon(bi);
    	}
    The problem is with calling this method:
    g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.20f));
    I want to make this picture with 20% transparency, but with 0.2f I get 100% transparency. If I set it >=0.50f, I get 0% transparency, if <0.50f - 100% transparency. Why is it so?
    Last edited by Asido; August 28th, 2010 at 10:22 AM.


  2. #2
    Junior Member
    Join Date
    Jul 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Setting a transparency level of a picture issue

    Quote Originally Posted by Asido View Post
    Hello,
    I have such method:
    private ImageIcon createSelectionPattern(int x, int y) {
    		BufferedImage bi = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration().createCompatibleImage(x, y, Transparency.BITMASK);
    		Graphics2D g2 = bi.createGraphics();
    		g2.setPaint(getFill());
    		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    		g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
    		int INS = x / 6;
    		g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.20f));
    		g2.fillRoundRect(0, 0, x, y, INS, INS);
    		g2.dispose();
    		return new ImageIcon(bi);
    	}
    The problem is with calling this method:
    g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.20f));
    I want to make this picture with 20% transparency, but with 0.2f I get 100% transparency. If I set it >=0.50f, I get 0% transparency, if <0.50f - 100% transparency. Why is it so?
    Hi,

    setComposite() is used to blend the source color with the destination color.i.e., Source is the one on which u draw and destination is the one that u draw. i.e., Source is similar to drawing board and destination is similar to the picture that u draw over the board.

    AlphaComposite.getInstance(AlphaComposite.SRC_OVER ,0.20f) will perform the following action: Multiply the alpha value of the source with 0.20f and this is the alpha value of ur final image.
    So,check the alpha value of ur source!!

    AlphaComposite.SRC_OVER will work on the following logic:
    If ur source is opaque,then it replaces the destination whereas if ur source is transparent,then ur destination remains unchanged.

    If u have doubts, u can visit this site:
    Blending Colors with AlphaComposite (Java Foundation Classes)

    Hope this helps

Similar Threads

  1. How to place icon on a picture base on (x,y) axis
    By FaintSmile in forum Java Theory & Questions
    Replies: 1
    Last Post: July 13th, 2010, 07:27 AM
  2. Showing a picture in a GUI
    By joachim89 in forum AWT / Java Swing
    Replies: 1
    Last Post: February 15th, 2010, 02:42 PM
  3. Help with a program to make a landscape picture
    By noseeds in forum Java Theory & Questions
    Replies: 1
    Last Post: December 15th, 2009, 10:25 PM
  4. Modify Colors in a Picture
    By theuniverse in forum Java Theory & Questions
    Replies: 0
    Last Post: October 17th, 2009, 04:49 PM
  5. Need help with a Picture!
    By Scout in forum Java Theory & Questions
    Replies: 1
    Last Post: October 12th, 2009, 05:33 PM