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