Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/swing/JMenuBar/MisplacedBorder/MisplacedBorder.java
38853 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* @bug 807379543* @summary JMenuBar has incorrect border when the window is on retina display.44* @author Sergey Bylokhov45* @run main/othervm MisplacedBorder46* @run main/othervm -Dswing.metalTheme=steel MisplacedBorder47*/48public final class MisplacedBorder implements Runnable {4950public static final int W = 400;51public static final int H = 400;5253public static void main(final String[] args) throws Exception {54for (final UIManager.LookAndFeelInfo laf : getInstalledLookAndFeels()) {55SwingUtilities.invokeAndWait(() -> setLookAndFeel(laf));56SwingUtilities.invokeAndWait(new MisplacedBorder());57}58System.out.println("Test passed");59}6061@Override62public void run() {63final JMenuBar menubar = new JMenuBar();64menubar.add(new JMenu(""));65menubar.add(new JMenu(""));66final JFrame frame = new JFrame();67frame.setUndecorated(true);68frame.setJMenuBar(menubar);69frame.setSize(W / 3, H / 3);70frame.setLocationRelativeTo(null);71frame.setVisible(true);7273// draw menu bar using standard order.74final BufferedImage bi1 = step1(menubar);7576// draw menu border on top of the menu bar, nothing should be changed.77final BufferedImage bi2 = step2(menubar);78frame.dispose();7980for (int x = 0; x < W; ++x) {81for (int y = 0; y < H; ++y) {82if (bi1.getRGB(x, y) != bi2.getRGB(x, y)) {83try {84ImageIO.write(bi1, "png", new File("image1.png"));85ImageIO.write(bi2, "png", new File("image2.png"));86} catch (IOException e) {87e.printStackTrace();88}89throw new RuntimeException("Failed: wrong color");90}91}92}93}9495/**96* Draws standard JMenuBar.97*/98private BufferedImage step1(final JMenuBar menubar) {99final BufferedImage bi1 = new BufferedImage(W, H, TYPE_INT_ARGB_PRE);100final Graphics2D g2d = bi1.createGraphics();101g2d.scale(2, 2);102g2d.setColor(Color.RED);103g2d.fillRect(0, 0, W, H);104menubar.paintAll(g2d);105g2d.dispose();106return bi1;107}108109/**110* Draws standard JMenuBar and border on top of it.111*/112private BufferedImage step2(final JMenuBar menubar) {113final BufferedImage bi2 = new BufferedImage(W, H, TYPE_INT_ARGB_PRE);114final Graphics2D g2d2 = bi2.createGraphics();115g2d2.scale(2, 2);116g2d2.setColor(Color.RED);117g2d2.fillRect(0, 0, W, H);118menubar.paintAll(g2d2);119menubar.getBorder().paintBorder(menubar, g2d2, menubar.getX(), menubar120.getX(), menubar.getWidth(), menubar.getHeight());121g2d2.dispose();122return bi2;123}124125private static void setLookAndFeel(final UIManager.LookAndFeelInfo laf) {126try {127UIManager.setLookAndFeel(laf.getClassName());128System.out.println("LookAndFeel: " + laf.getClassName());129} catch (ClassNotFoundException | InstantiationException |130UnsupportedLookAndFeelException | IllegalAccessException e) {131throw new RuntimeException(e);132}133}134}135136