Results 1 to 5 of 5
- 03-18-2009, 09:32 PM #1
Member
- Join Date
- Aug 2008
- Posts
- 58
- Rep Power
- 0
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 09:37 PM.
- 03-19-2009, 08:08 AM #2Java Code:
BufferedImage tmp = new BufferedImage(width, height, BufferedImage.TRANSLUCENT);
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.
- 03-21-2009, 12:52 AM #3
Member
- Join Date
- Aug 2008
- Posts
- 58
- Rep Power
- 0
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?
- 03-24-2009, 10:50 PM #4
Senior Member
- Join Date
- Aug 2008
- Posts
- 384
- Rep Power
- 13
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 10:55 PM. Reason: Added example
I die a little on the inside...
Every time I get shot.
- 03-26-2009, 10:42 PM #5
Member
- Join Date
- Aug 2008
- Posts
- 58
- Rep Power
- 0
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
-
transparent jframe?
By majkel in forum AWT / SwingReplies: 12Last Post: 10-09-2010, 06:06 PM -
SWT Transparent button
By ynnorj in forum SWT / JFaceReplies: 2Last Post: 03-04-2009, 07:33 AM -
setting background color of JFrame form with NetBeans 6.1
By onefootswill in forum New To JavaReplies: 4Last Post: 08-12-2008, 08:02 AM -
Retaining DB values as well as Dynamically generated Values.. Help Needed !
By rajivjha in forum Advanced JavaReplies: 0Last Post: 05-22-2008, 11:53 AM -
Transparent JTextPane
By Ada in forum AWT / SwingReplies: 1Last Post: 05-31-2007, 10:50 PM
Bookmarks