Path: blob/master/test/jdk/javax/swing/JMenuBar/MisplacedBorder/MisplacedBorder.java
41152 views
/*1* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.awt.Color;24import java.awt.Graphics2D;25import java.awt.image.BufferedImage;26import java.io.File;27import java.io.IOException;2829import javax.imageio.ImageIO;30import javax.swing.JFrame;31import javax.swing.JMenu;32import javax.swing.JMenuBar;33import javax.swing.SwingUtilities;34import javax.swing.UIManager;35import javax.swing.UnsupportedLookAndFeelException;3637import static java.awt.image.BufferedImage.TYPE_INT_ARGB_PRE;38import static javax.swing.UIManager.getInstalledLookAndFeels;3940/**41* @test42* @key headful43* @bug 807379544* @summary JMenuBar has incorrect border when the window is on retina display.45* @author Sergey Bylokhov46* @run main/othervm MisplacedBorder47* @run main/othervm -Dswing.metalTheme=steel MisplacedBorder48*/49public final class MisplacedBorder implements Runnable {5051public static final int W = 400;52public static final int H = 400;5354public static void main(final String[] args) throws Exception {55for (final UIManager.LookAndFeelInfo laf : getInstalledLookAndFeels()) {56SwingUtilities.invokeAndWait(() -> setLookAndFeel(laf));57SwingUtilities.invokeAndWait(new MisplacedBorder());58}59System.out.println("Test passed");60}6162@Override63public void run() {64final JMenuBar menubar = new JMenuBar();65menubar.add(new JMenu(""));66menubar.add(new JMenu(""));67final JFrame frame = new JFrame();68frame.setUndecorated(true);69frame.setJMenuBar(menubar);70frame.setSize(W / 3, H / 3);71frame.setLocationRelativeTo(null);72frame.setVisible(true);7374// draw menu bar using standard order.75final BufferedImage bi1 = step1(menubar);7677// draw menu border on top of the menu bar, nothing should be changed.78final BufferedImage bi2 = step2(menubar);79frame.dispose();8081for (int x = 0; x < W; ++x) {82for (int y = 0; y < H; ++y) {83if (bi1.getRGB(x, y) != bi2.getRGB(x, y)) {84try {85ImageIO.write(bi1, "png", new File("image1.png"));86ImageIO.write(bi2, "png", new File("image2.png"));87} catch (IOException e) {88e.printStackTrace();89}90throw new RuntimeException("Failed: wrong color");91}92}93}94}9596/**97* Draws standard JMenuBar.98*/99private BufferedImage step1(final JMenuBar menubar) {100final BufferedImage bi1 = new BufferedImage(W, H, TYPE_INT_ARGB_PRE);101final Graphics2D g2d = bi1.createGraphics();102g2d.scale(2, 2);103g2d.setColor(Color.RED);104g2d.fillRect(0, 0, W, H);105menubar.paintAll(g2d);106g2d.dispose();107return bi1;108}109110/**111* Draws standard JMenuBar and border on top of it.112*/113private BufferedImage step2(final JMenuBar menubar) {114final BufferedImage bi2 = new BufferedImage(W, H, TYPE_INT_ARGB_PRE);115final Graphics2D g2d2 = bi2.createGraphics();116g2d2.scale(2, 2);117g2d2.setColor(Color.RED);118g2d2.fillRect(0, 0, W, H);119menubar.paintAll(g2d2);120menubar.getBorder().paintBorder(menubar, g2d2, menubar.getX(), menubar121.getX(), menubar.getWidth(), menubar.getHeight());122g2d2.dispose();123return bi2;124}125126private static void setLookAndFeel(final UIManager.LookAndFeelInfo laf) {127try {128UIManager.setLookAndFeel(laf.getClassName());129System.out.println("LookAndFeel: " + laf.getClassName());130} catch (ClassNotFoundException | InstantiationException |131UnsupportedLookAndFeelException | IllegalAccessException e) {132throw new RuntimeException(e);133}134}135}136137138