trayicon java,java TrayIcon使用图像与透明背景

I am using the following code to set a tray icon in Windows and Linux. It works wonderful in Windows and works okay in Linux. In Linux (Ubuntu) I have my panel set to be (somewhat) transparent and when I add a GIF (with a transparent background) the background of the icon shows up all grey and ugly (see image, green diamond "!")....Any ideas on how to make the GIF image I am adding "keep" its transparent background?

and the image I am using, if you'd like to test:

import java.awt.*;

import java.awt.event.*;

public class TrayFun {

static class ShowMessageListener implements ActionListener {

TrayIcon trayIcon;

String title;

String message;

TrayIcon.MessageType messageType;

ShowMessageListener(

TrayIcon trayIcon,

String title,

String message,

TrayIcon.MessageType messageType) {

this.trayIcon = trayIcon;

this.title = title;

this.message = message;

this.messageType = messageType;

}

public void actionPerformed(ActionEvent e) {

trayIcon.displayMessage(title, message, messageType);

}

}

public static void main(String args[]) {

Runnable runner = new Runnable() {

public void run() {

if (SystemTray.isSupported()) {

final SystemTray tray = SystemTray.getSystemTray();

Image image = Toolkit.getDefaultToolkit().getImage("green_info.png");

PopupMenu popup = new PopupMenu();

final TrayIcon trayIcon = new TrayIcon(image, "The Tip Text", popup);

trayIcon.setImageAutoSize(true);

MenuItem item = new MenuItem("Close");

item.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

tray.remove(trayIcon);

}

});

popup.add(item);

try {

tray.add(trayIcon);

} catch (AWTException e) {

System.err.println("Can't add to tray");

}

} else {

System.err.println("Tray unavailable");

}

}

};

EventQueue.invokeLater(runner);

}

}

解决方案

Chances are this problem cannot be resolved. It depends on wether Java is doing a good job in creating the tray subwindow in Linux or not. If Jave does it wrong, transparency is already lost when the image is drawn.

What is the real background value of the icon you are using? Is it the gray tone shown above? Set it to purple to see if the transparency of the image is used (Java defaults to gray background) or not.

Make sure you tried both transparency options of PNG: transparent color index as well as alpha channel. Alpha channel is very common in Linux, not so in the Windows world.

The resolution of your icon is too small. Do it in 64x64 or better 128x128. AFAIK there is no standard resolution for tray icons, and even if so, it is certainly not 16x16.

Another format you could try is SVG. Only try that after making sure that the transparency of the image is the problem (see 1).

See here for background information on this issue:

http://www.rasterman.com/index.php?page=News (scroll down to 2 February 2006)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值