1 /* 2 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 25 /* 26 @test 27 @bug 8150176 8151773 28 @summary Check if correct resolution variant is used for tray icon. 29 @author a.stepanov 30 @run applet/manual=yesno MultiResolutionTrayIconTest.html 31 */ 32 33 34 import java.applet.Applet; 35 import java.awt.*; 36 import java.awt.event.*; 37 import java.awt.image.*; 38 39 40 public class MultiResolutionTrayIconTest extends Applet { 41 42 private SystemTray tray; 43 private TrayIcon icon, iconMRI; 44 45 public void init() { this.setLayout(new BorderLayout()); } 46 47 public void start() { 48 49 boolean trayIsSupported = SystemTray.isSupported(); 50 Button b = new Button("Start"); 51 if (trayIsSupported) { 52 53 prepareIcons(); 54 b.addActionListener(new ActionListener() { 55 @Override 56 public void actionPerformed(ActionEvent e) { doTest(); } 57 }); 58 } else { 59 b.setLabel("not supported"); 60 b.setEnabled(false); 61 System.out.println("system tray is not supported"); 62 } 63 add(b, BorderLayout.CENTER); 64 65 validate(); 66 setVisible(true); 67 } 68 69 private BufferedImage generateImage(int w, int h, Color c) { 70 71 BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); 72 Graphics g = img.getGraphics(); 73 g.setColor(c); 74 g.fillRect(0, 0, w, h); 75 g.setColor(Color.WHITE); 76 int r = (Math.min(w, h) >= 8) ? 3 : 1; 77 g.fillRect(r, r, w - 2 * r, h - 2 * r); 78 return img; 79 } 80 81 private void prepareIcons() { 82 83 tray = SystemTray.getSystemTray(); 84 Dimension d = tray.getTrayIconSize(); 85 int w = d.width, h = d.height; 86 87 BufferedImage img = generateImage(w, h, Color.BLUE); 88 // use wrong icon size for "nok" 89 BufferedImage nok = generateImage(w / 2 + 2, h / 2 + 2, Color.RED); 90 BaseMultiResolutionImage mri = 91 new BaseMultiResolutionImage(new BufferedImage[] {nok, img}); 92 icon = new TrayIcon(img); 93 icon.setImageAutoSize(true); // just in case 94 iconMRI = new TrayIcon(mri); 95 iconMRI.setImageAutoSize(true); 96 } 97 98 private void doTest() { 99 100 if (tray.getTrayIcons().length > 0) { return; } // icons were added already 101 try { 102 tray.add(icon); 103 tray.add(iconMRI); 104 } catch (Exception e) { 105 throw new RuntimeException(e); 106 } 107 } 108 109 public void stop() { 110 111 // check for null, just in case 112 if (tray != null) { 113 tray.remove(icon); 114 tray.remove(iconMRI); 115 } 116 } 117 }