1 /*
   2  * Copyright (c) 2015, 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 import java.awt.Color;
  25 import java.awt.Graphics2D;
  26 import java.awt.image.BufferedImage;
  27 import java.io.File;
  28 import java.io.IOException;
  29 
  30 import javax.imageio.ImageIO;
  31 import javax.swing.JFrame;
  32 import javax.swing.JMenu;
  33 import javax.swing.JMenuBar;
  34 import javax.swing.SwingUtilities;
  35 import javax.swing.UIManager;
  36 import javax.swing.UnsupportedLookAndFeelException;
  37 
  38 import static java.awt.image.BufferedImage.TYPE_INT_ARGB_PRE;
  39 import static javax.swing.UIManager.getInstalledLookAndFeels;
  40 
  41 /**
  42  * @test
  43  * @bug 8073795
  44  * @summary JMenuBar has incorrect border when the window is on retina display.
  45  * @author Sergey Bylokhov
  46  * @run main/othervm MisplacedBorder
  47  * @run main/othervm -Dswing.metalTheme=steel MisplacedBorder
  48  */
  49 public final class MisplacedBorder implements Runnable {
  50 
  51     public static final int W = 400;
  52     public static final int H = 400;
  53 
  54     public static void main(final String[] args) throws Exception {
  55         for (final UIManager.LookAndFeelInfo laf : getInstalledLookAndFeels()) {
  56             SwingUtilities.invokeAndWait(() -> setLookAndFeel(laf));
  57             SwingUtilities.invokeAndWait(new MisplacedBorder());
  58         }
  59         System.out.println("Test passed");
  60     }
  61 
  62     @Override
  63     public void run() {
  64         final JMenuBar menubar = new JMenuBar();
  65         menubar.add(new JMenu(""));
  66         menubar.add(new JMenu(""));
  67         final JFrame frame = new JFrame();
  68         frame.setUndecorated(true);
  69         frame.setJMenuBar(menubar);
  70         frame.setSize(W / 3, H / 3);
  71         frame.setLocationRelativeTo(null);
  72         frame.setVisible(true);
  73 
  74         // draw menu bar using standard order.
  75         final BufferedImage bi1 = step1(menubar);
  76 
  77         // draw menu border on top of the menu bar, nothing should be changed.
  78         final BufferedImage bi2 = step2(menubar);
  79         frame.dispose();
  80 
  81         for (int x = 0; x < W; ++x) {
  82             for (int y = 0; y < H; ++y) {
  83                 if (bi1.getRGB(x, y) != bi2.getRGB(x, y)) {
  84                     try {
  85                         ImageIO.write(bi1, "png", new File("image1.png"));
  86                         ImageIO.write(bi2, "png", new File("image2.png"));
  87                     } catch (IOException e) {
  88                         e.printStackTrace();
  89                     }
  90                     throw new RuntimeException("Failed: wrong color");
  91                 }
  92             }
  93         }
  94     }
  95 
  96     /**
  97      * Draws standard JMenuBar.
  98      */
  99     private BufferedImage step1(final JMenuBar menubar) {
 100         final BufferedImage bi1 = new BufferedImage(W, H, TYPE_INT_ARGB_PRE);
 101         final Graphics2D g2d = bi1.createGraphics();
 102         g2d.scale(2, 2);
 103         g2d.setColor(Color.RED);
 104         g2d.fillRect(0, 0, W, H);
 105         menubar.paintAll(g2d);
 106         g2d.dispose();
 107         return bi1;
 108     }
 109 
 110     /**
 111      * Draws standard JMenuBar and border on top of it.
 112      */
 113     private BufferedImage step2(final JMenuBar menubar) {
 114         final BufferedImage bi2 = new BufferedImage(W, H, TYPE_INT_ARGB_PRE);
 115         final Graphics2D g2d2 = bi2.createGraphics();
 116         g2d2.scale(2, 2);
 117         g2d2.setColor(Color.RED);
 118         g2d2.fillRect(0, 0, W, H);
 119         menubar.paintAll(g2d2);
 120         menubar.getBorder().paintBorder(menubar, g2d2, menubar.getX(), menubar
 121                 .getX(), menubar.getWidth(), menubar.getHeight());
 122         g2d2.dispose();
 123         return bi2;
 124     }
 125 
 126     private static void setLookAndFeel(final UIManager.LookAndFeelInfo laf) {
 127         try {
 128             UIManager.setLookAndFeel(laf.getClassName());
 129             System.out.println("LookAndFeel: " + laf.getClassName());
 130         } catch (ClassNotFoundException | InstantiationException |
 131                 UnsupportedLookAndFeelException | IllegalAccessException e) {
 132             throw new RuntimeException(e);
 133         }
 134     }
 135 }