1 /*
   2  * Copyright (c) 2010, 2014, 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.*;
  25 import javax.swing.*;
  26 import java.awt.image.BufferedImage;
  27 
  28 /*
  29  * @test
  30  * @key headful
  31  * @summary Check if a per-pixel translucent window shows up with correct translucency
  32  * @author mrkam
  33  * @library ../../../../lib/testlibrary
  34  * @build Common ExtendedRobot
  35  * @run main PerPixelTranslucentCanvas
  36  * @run main/othervm -Dsun.java2d.uiScale=1.5 PerPixelTranslucentCanvas
  37  */
  38 
  39 public class PerPixelTranslucentCanvas extends Common {
  40 
  41     JPanel center;
  42     static Color OVAL_COLOR = Color.BLUE;
  43 
  44     public static void main(String[] ignored) throws Exception {
  45         FG_COLOR = new Color(200, 0, 0, 100);
  46         BG_COLOR = Color.GREEN;
  47         for (Class<Window> windowClass: WINDOWS_TO_TEST)
  48             new PerPixelTranslucentCanvas(windowClass).doTest();
  49     }
  50 
  51     public PerPixelTranslucentCanvas(Class windowClass) throws Exception {
  52         super(windowClass);
  53     }
  54 
  55     @Override
  56     public void createSwingComponents() {
  57         Container contentPane = RootPaneContainer.class.cast(window).getContentPane();
  58         BorderLayout bl = new BorderLayout(10, 10);
  59         contentPane.setLayout(bl);
  60 
  61         JLabel label = new JLabel("North", new ImageIcon(
  62                 new BufferedImage(30, 30, BufferedImage.TYPE_INT_RGB)), SwingConstants.CENTER);
  63         contentPane.add(label, BorderLayout.NORTH);
  64 
  65         JButton button = new JButton("West");
  66         contentPane.add(button, BorderLayout.WEST);
  67 
  68         center = new JPanel() {
  69             @Override
  70             public void paint(Graphics g) {
  71                 g.setColor(OVAL_COLOR);
  72                 g.fillOval(0, 0, getWidth(), getHeight());
  73             }
  74         };
  75         contentPane.add(center, BorderLayout.CENTER);
  76 
  77         JTextField jTextField = new JTextField("South");
  78         contentPane.add(jTextField, BorderLayout.SOUTH);
  79     }
  80 
  81     @Override
  82     public void doTest() throws Exception {
  83         robot.waitForIdle(delay);
  84 
  85         Rectangle bounds = center.getBounds();
  86         Point loc = center.getLocationOnScreen();
  87 
  88         final int x = loc.x + bounds.width / 2;
  89         final int y = loc.y + bounds.height / 2;
  90 
  91         Color color = robot.getPixelColor(x, y);
  92         if (OVAL_COLOR.getRGB() != color.getRGB())
  93             throw new RuntimeException("bounds = " + bounds + "\n" +
  94                     "loc = " + loc + "\n" +
  95                     "background loc = " + background.getX() + ", " + background.getY() + "\n" +
  96                     "so middle point over background is " + (x - background.getX()) + ", " + (y - background.getY()) + "\n" +
  97                     "Oval is not opaque in the middle point (" + x + ", " + y + ", " + color + ")");
  98 
  99         color = robot.getPixelColor(loc.x - 5, loc.y - 5);
 100         if (FG_COLOR.getRGB() == color.getRGB())
 101             throw new RuntimeException("Background is not translucent (" + color + ")");
 102 
 103         EventQueue.invokeAndWait(this::dispose);
 104         robot.waitForIdle();
 105     }
 106 }