1 /* 2 * Copyright (c) 2012, 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 * @test 26 * @key headful 27 * @bug 6209975 28 * @summary regression: JMenuItem icons overimposed on JMenuItem labels under Metal LAF 29 * @library ../../regtesthelpers 30 * @build Util 31 * @author Alexander Zuev 32 * @run main bug6209975 33 */ 34 import javax.swing.*; 35 import java.awt.*; 36 import java.awt.event.InputEvent; 37 import sun.awt.SunToolkit; 38 39 public class bug6209975 { 40 41 private static final ReturnObject RO1 = new ReturnObject(); 42 private static final ReturnObject RO2 = new ReturnObject(); 43 44 private static JMenu menu; 45 private static JButton button; 46 47 public static void main(String[] args) throws Exception { 48 49 SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit(); 50 Robot robot = new Robot(); 51 robot.setAutoDelay(500); 52 53 54 SwingUtilities.invokeAndWait(new Runnable() { 55 56 @Override 57 public void run() { 58 createAndShowGUI(); 59 } 60 }); 61 62 toolkit.realSync(); 63 64 Point clickPoint = getButtonClickPoint(); 65 robot.mouseMove(clickPoint.x, clickPoint.y); 66 robot.mousePress(InputEvent.BUTTON1_MASK); 67 robot.mouseRelease(InputEvent.BUTTON1_MASK); 68 toolkit.realSync(); 69 70 clickPoint = getMenuClickPoint(); 71 robot.mouseMove(clickPoint.x, clickPoint.y); 72 robot.mousePress(InputEvent.BUTTON1_MASK); 73 robot.mouseRelease(InputEvent.BUTTON1_MASK); 74 toolkit.realSync(); 75 76 if (RO1.itsValue <= RO2.itsValue) { 77 throw new RuntimeException("Offset if the second icon is invalid."); 78 } 79 } 80 81 private static Point getButtonClickPoint() throws Exception { 82 final Point[] result = new Point[1]; 83 84 SwingUtilities.invokeAndWait(new Runnable() { 85 86 @Override 87 public void run() { 88 Point p = button.getLocationOnScreen(); 89 Dimension size = button.getSize(); 90 result[0] = new Point(p.x + size.width / 2, p.y + size.height / 2); 91 } 92 }); 93 return result[0]; 94 } 95 96 private static Point getMenuClickPoint() throws Exception { 97 final Point[] result = new Point[1]; 98 99 SwingUtilities.invokeAndWait(new Runnable() { 100 101 @Override 102 public void run() { 103 Point p = menu.getLocationOnScreen(); 104 Dimension size = menu.getSize(); 105 result[0] = new Point(p.x + size.width / 2, p.y + size.height / 2); 106 } 107 }); 108 return result[0]; 109 } 110 111 private static void createAndShowGUI() { 112 JFrame frame = new JFrame("Test6209975"); 113 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 114 frame.applyComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT); 115 frame.setLayout(new BorderLayout()); 116 button = new JButton("Focus holder"); 117 frame.add(button); 118 119 JMenuBar mb = new JMenuBar(); 120 menu = new JMenu("File"); 121 122 JMenuItem item; 123 124 item = new JMenuItem("Just a menu item"); 125 item.setIcon(new MyIcon(RO1)); 126 item.setHorizontalTextPosition(SwingConstants.LEADING); 127 menu.add(item); 128 129 item = new JMenuItem("Menu Item with another icon"); 130 item.setIcon(new MyIcon(RO2)); 131 item.setHorizontalTextPosition(SwingConstants.TRAILING); 132 menu.add(item); 133 134 mb.add(menu); 135 136 frame.setJMenuBar(mb); 137 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 138 frame.pack(); 139 frame.setLocation(400, 300); 140 frame.setVisible(true); 141 } 142 143 public static class ReturnObject { 144 145 public volatile int itsValue; 146 } 147 148 public static class MyIcon implements Icon { 149 150 ReturnObject thisObject = null; 151 152 public MyIcon(ReturnObject ro) { 153 super(); 154 thisObject = ro; 155 } 156 157 public void paintIcon(Component c, Graphics g, int x, int y) { 158 Color color = g.getColor(); 159 g.setColor(Color.BLACK); 160 g.fillRect(x, y, 10, 10); 161 g.setColor(color); 162 thisObject.itsValue = x; 163 } 164 165 public int getIconWidth() { 166 return 10; 167 } 168 169 public int getIconHeight() { 170 return 10; 171 } 172 } 173 }