aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Drawing a Transparent PNG Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Drawing a Transparent PNG" Watch "Drawing a Transparent PNG" New topic
Author

Drawing a Transparent PNG

Mikpo Siru
Ranch Hand

Joined: Apr 09, 2009
Posts: 54
Hi,

I'm trying to draw a translucent PNG onto a JPanel:



However the transparent areas are drawn as solid white. How do I draw the image with it's transparency?

Thanks.
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

i used to make transparent gif's a long time age. i would download the free 30 day trial of Paint Shop Pro to do it. there is/was a free program that did the same(just the transparent background). try a google search. perhaps i misunderstood, you want to see the desktop as the background?


SCJP
Visit my download page
Mikpo Siru
Ranch Hand

Joined: Apr 09, 2009
Posts: 54
Hi

The image itself is already transparent, but it's showing up on a JPanel as solid white.

I'll double check that the image is actually transparent.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4642
    
    5

It's needlessly wasteful to construct an ImageIcon solely for the purpose of extracting its Image. Load the image using ImageIO#read(...) instead.

To get better help sooner, post a SSCCE (Short, Self Contained, Compilable and Executable) example that demonstrates the problem. Use a .png image with transparency that is available on the net so that members here can run and test your code.

luck, db
There are no new questions, but there may be new answers.
Nitin Surana
Ranch Hand

Joined: Jan 21, 2011
Posts: 129

I tried it with a transparent image(png) and its working absolutely fine.

here's my code, may be this'll help you.

Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

i will copy that just to have some reading material.

well if the image is transparent, then the background color of white is showing through yes?

D.B. i like ImageIO class myself. i like how it can read an image file regardless of file extension
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4642
    
    5

Randall Twede wrote:D.B. i like ImageIO class myself. i like how it can read an image file regardless of file extension

Just be aware that ImageIO reads to a BufferedImage, so it loads only the first frame of an animated GIF.

To load an animated image, you need to use the Toolkit / MediaTracker approach, along with ImageObserver. Or stick an ImageIcon on a JLabel and let the core classes take care of the animation.
Mikpo Siru
Ranch Hand

Joined: Apr 09, 2009
Posts: 54
@Darryl: Thanks for the tip.
@Nitin: Thanks for trying it out.

The problem turned out to be my image, as I saved it in MSPaint and the transparency was lost. The only program that seemed to recognise the transparency in the image is the Windows XP preview bar, so I was sort of misled into thinking transparency was there.

I downloaded Paint.NET (free) and used that to make the image transparent. It's working now. Thanks everyone for your input.

Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

Paint.Net next on my download list. back in the old days there was Paint Shop Pro (free trial) Photoshop(way too expensive) and some obscure, hard to use free program.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Drawing a Transparent PNG