code_junkie

java TrayIcon using image with transparent background

Nov 12th, 2011
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3.  
  4. public class TrayFun {
  5.  
  6.  
  7. static class ShowMessageListener implements ActionListener {
  8. TrayIcon trayIcon;
  9. String title;
  10. String message;
  11. TrayIcon.MessageType messageType;
  12. ShowMessageListener(
  13. TrayIcon trayIcon,
  14. String title,
  15. String message,
  16. TrayIcon.MessageType messageType) {
  17. this.trayIcon = trayIcon;
  18. this.title = title;
  19. this.message = message;
  20. this.messageType = messageType;
  21. }
  22. public void actionPerformed(ActionEvent e) {
  23. trayIcon.displayMessage(title, message, messageType);
  24. }
  25. }
  26.  
  27. public static void main(String args[]) {
  28. Runnable runner = new Runnable() {
  29. public void run() {
  30. if (SystemTray.isSupported()) {
  31. final SystemTray tray = SystemTray.getSystemTray();
  32. Image image = Toolkit.getDefaultToolkit().getImage("green_info.png");
  33. PopupMenu popup = new PopupMenu();
  34. final TrayIcon trayIcon = new TrayIcon(image, "The Tip Text", popup);
  35. trayIcon.setImageAutoSize(true);
  36.  
  37. MenuItem item = new MenuItem("Close");
  38. item.addActionListener(new ActionListener() {
  39. public void actionPerformed(ActionEvent e) {
  40. tray.remove(trayIcon);
  41. }
  42. });
  43. popup.add(item);
  44. try {
  45. tray.add(trayIcon);
  46. } catch (AWTException e) {
  47. System.err.println("Can't add to tray");
  48. }
  49. } else {
  50. System.err.println("Tray unavailable");
  51. }
  52. }
  53. };
  54. EventQueue.invokeLater(runner);
  55. }
  56. }
  57.  
  58. public void paint(Graphics g) {
  59. if (g != null && curW > 0 && curH > 0) {
  60. BufferedImage bufImage = new BufferedImage(curW, curH, BufferedImage.TYPE_INT_ARGB);
  61. Graphics2D gr = bufImage.createGraphics();
  62. if (gr != null) {
  63. try {
  64. gr.setColor(getBackground());
  65. gr.fillRect(0, 0, curW, curH);
  66. gr.drawImage(image, 0, 0, curW, curH, observer);
  67. gr.dispose();
  68.  
  69. g.drawImage(bufImage, 0, 0, curW, curH, null);
  70. } finally {
  71. gr.dispose();
  72. }
  73. }
  74. }
  75. }
Add Comment
Please, Sign In to add comment