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