I'm trying to use a transparent PNG as the icon for my Java application. The image on the JFrame and task bar work great. However, when I use the image with a TrayIcon, I get a black matte background around the edge of the PNG.
Has anybody else come across this issue? I would really like to use a PNG instead of GIF or JPEG.
With Java6, a PNG picture should be used for TrayIcon, but as mentioned in this SO question, check:
the background color chosen to represent the transparent pixels
the transparency options
the resolution of the icon
alternate format like SVG (provided you are using external library like Batik, and conversion mechnism to java.awt.Image)
Related
In Java documentation about TrayIcon's setImage method (http://docs.oracle.com/javase/7/docs/api/java/awt/TrayIcon.html#setImage(java.awt.Image) says:
If the image represents an animated image, it will be animated automatically.
But I can't get an animated image in the trayicon.
I've tested with PNG and GIF animated, 32x32, 64x64 and 128x128 combinations. But no one works.
Is there any specific format to thses animated tray icon images ?
Check out the Swing tutorial on How to Use the System Tray.
I just changed the gif and it worked fine for me.
The gif I was using was 16x16. I also tried with a 137x116 gif and it worked, although I first had to right click on the "empty area" in the tray and then select the "Auto size" option.
Old question, but in case anyone needs this like me - you have to load the GIF image with new ImageIcon and not ImageIO.read.
trayImage = ImageIO.read(getClass().getClassLoader().getResource("logo.gif")); // DOESN'T WORK
trayImage = new ImageIcon(getClass().getClassLoader().getResource("logo.gif")).getImage(); // WORKS
So I have no clue why this is happening. I am using Universal Image Loader to load these images. It seems like the last line of pixels is being streched for some weird reason. I want the image to just stretch out evenly. (I don't care that it will look weird. The images below are for demo purposes.)
Also, don't mind the first and last image. I purposely blurred that out because it had someone's face on it.
This is how I set up my Universal Image Loader:
//setup Image Loader for loading cruise line logos
displayImageOptions = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.ic_launcher)//show this image when image is loading
.showImageForEmptyUri(R.drawable.ic_launcher)//show this image incase image doesn't exist
.showImageOnFail(R.drawable.ic_launcher)//show this image if fetching image from URL didn't work
.cacheInMemory(true)//cache image in RAM
.cacheOnDisc(true)//cache image in device for later use
.considerExifParams(true)
.displayer(new RoundedBitmapDisplayer(5))//super subtle rounded corners on images
.build();
This is caused by the way RoundedBitmapDisplayer draws the bitmap.
If you look at the source, you'll see that it uses a RoundedDrawable, which uses canvas.drawRoundRect() to draw a rounded rectangle of the desired size of the Drawable, using the downloaded image as the texture via a BitmapShader. BitmapShader does not support scaling (only clamping and tile modes). Try using a SimpleBitmapDisplayer instead which uses the normal ImageView.setImageBitmap() way of displaying the image.
If you need rounded corners, you'll have to find a different way to implement that, for example by scaling the Bitmap to the desired size first. Another option is to call Canvas.saveLayer() before delegating to BitmapDrawable for the scaling, and then applying the rounded corner masking effect using PorterDuff.Mode.DST_IN. Either way you'll end up writing a bit more low-level code, but you should be able to encapsulate everything nicely in a custom BitmapDisplayer.
I am working with a java swing application project. I want to make an image as icon in my JPanel but it seems to be low quality image when i am printing the image using the external printer. Here is the code
ImageIcon ii=new ImageIcon(scaleImage(90, 107, ImageIO.read(new File(f.getAbsolutePath()))));
image.setIcon(ii);
How can I make high quality image icon other than this method?
If you use a downscaled version of an image for an Icon, the resolution loss cannot be compensated when printing the icon.
Instead of using ImageIcon, you can create your own icon class (by implementing the javax.swing.Icon interface, it's actually quite simple), which keeps your Image at a higher resolution and paints it at a smaller size (but without quality loss, so you still can see more detail when printing it).
This can be done by applying an AffineTransform/Scale prior to drawing the Image, or by using graphics.drawImage(x,y,width,height,imageObserver).
I've used GIMP to create a sprite using a transparent background. Once I put it in the Image Packer the transparent background disappears and the whole image is messed up. After a bit of googling without finding a single useful thing I decided to use a pink background, but I can't find support for setting a pixel to alpha (I can read the pixel's color though!)
How do I fix this and have a transparent background in my sprite (org.newdawn.slick.Image to be precise)?
Useful links: None as far as I can remember. Took me 45 minutes to jump to this site for help, so there's either pretty much nothing or literally no support.
Are you sure the image is saved in a format that supports the transparency? For example JPG does NOT support transparency. PNG does.
Also are you sure that when saving the picture the background is indeed transparent and not white? To double check the background is transparent the majority of image editors will display a grey and white checked grids to show transparency.
Preferably, I'd like a somewhat fancy bevel . I'd like the picture to have some depth on it..so it doesn't look so plain and old-fashion :)
You can perhaps do something with the 2D Canvas API, but I don't know enough about that to describe a process. Here's how you can try to do that with ordinary widgets...well, mostly ordinary...:
Step #1: Create a nine-patch PNG that has your "somewhat fancy bevel" and is designed to stretch around images.
Step #2: Set that nine-patch PNG as the background of the ImageView that has your image.
Step #3: Adjust the padding of the ImageView so that your bevel shows -- that'll depend on how wide you made the bevel effect.
It's possible you'll need to put the nine-patch PNG on a LinearLayout or something and have the ImageView in there (then use margins to allow the bevel to peek through), but I think you can do it without the extra widget.
I would say that the easiest thing to do is to set nine-patch PNG as the background of your ImageView. This way the src would load the image, while background attribute would load the background 9.png image.
When creating 9.png for the background take care that you do not mark border lines as streatchable. Look at the image.