1 /*
   2  * Copyright (c) 2018, 2019, 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 /**
  25  * @test
  26  * @key headful
  27  * @bug 8204931 8227392 8224825
  28  * @summary test alpha colors are blended with background.
  29  */
  30 
  31 import java.awt.Color;
  32 import java.awt.Component;
  33 import java.awt.Dimension;
  34 import java.awt.Frame;
  35 import java.awt.Graphics;
  36 import java.awt.Robot;
  37 import javax.swing.SwingUtilities;
  38 
  39 public class AlphaColorTest extends Component {
  40 
  41     private Color color;
  42 
  43     public AlphaColorTest(Color c) {
  44        this.color = c;
  45     }
  46 
  47     public void paint(Graphics g) {
  48         g.setColor(color);
  49         g.fillRect(0, 0, getSize().width, getSize().height);
  50     }
  51 
  52     public Dimension getPreferredSize() {
  53         return getSize();
  54     }
  55 
  56     public Dimension getSize() {
  57         return new Dimension(200, 200);
  58     }
  59 
  60     public static void main(String args[]) throws Exception {
  61         SwingUtilities.invokeAndWait(() -> createAndShowGUI());
  62         Robot robot = new Robot();
  63         robot.delay(5000);
  64         robot.waitForIdle();
  65         Color c = robot.getPixelColor(frame.getX() + 100, frame.getY() + 100);
  66         int red = c.getRed();
  67         frame.dispose();
  68         // Should be 126-128, but be tolerant of gamma correction.
  69         if (red < 122 || red > 132) {
  70             throw new RuntimeException("Color is not as expected. Got " + c);
  71         }
  72      }
  73 
  74     static Frame frame;
  75     private static void createAndShowGUI() {
  76         frame = new Frame("Alpha Color Test") {
  77             @Override
  78             public void paint(Graphics g) {
  79                 g.setColor(Color.black);
  80                 g.fillRect(0, 0, getWidth(), getHeight());
  81                 super.paint(g);
  82             }
  83         };
  84         Color color = new Color(255, 255, 255, 127);
  85         frame.add("Center", new AlphaColorTest(color));
  86         frame.pack();
  87         frame.setVisible(true);
  88     }
  89 }