• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Drawing a Transparent PNG

 
Mikpo Siru
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4469
3
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Mikpo Siru
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Nitin Surana
Ranch Hand
Posts: 129
Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4469
3
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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
Posts: 4469
3
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic