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.*;
  25 import javax.swing.*;
  26 import java.awt.image.BufferedImage;
  27 import javax.imageio.ImageIO;
  28 import java.io.File;
  29 import java.io.IOException;
  30 
  31 /**
  32  * @test
  33  * @bug 8040689
  34  * @summary Tests if antialiasing artifact are shown for font drawn on 
  35  *          translucent destination
  36  */
  37 
  38 public class TranslucentAAFont {
  39     final private static int width = 40;
  40     final private static int height = 40;
  41     private static JFrame frame;
  42     private static BufferedImage actualBimg, expectedBI;
  43  
  44     public void start() {
  45         frame = new JFrame();
  46         frame.setUndecorated(true);
  47         frame.setBackground(new Color(0, 0, 0, 0));
  48         frame.setSize(new Dimension(width, height));
  49         frame.setLocationRelativeTo(null);
  50         frame.setVisible(true);
  51            
  52         GraphicsEnvironment ge = GraphicsEnvironment
  53                 .getLocalGraphicsEnvironment();
  54         GraphicsConfiguration gc = ge.getDefaultScreenDevice()
  55                                      .getDefaultConfiguration();        
  56 
  57         int w = frame.getWidth();
  58         int h = frame.getHeight();
  59 
  60         // Actual image
  61         // Create translucent bimg
  62         BufferedImage actualBI = gc.createCompatibleImage(w, h, Transparency.TRANSLUCENT);
  63 
  64         // Render translucent red into translucent bimg
  65         Graphics2D actualg2d = (Graphics2D) actualBI.createGraphics();
  66         actualg2d.setColor(new Color(255, 0, 0, 50));
  67         actualg2d.fillRect(0, 0, w, h);
  68 
  69         // Render text on translucent bimg
  70         actualg2d.setColor(new Color(0,0,0,255));
  71         actualg2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  72         Font goldfont = frame.getFont().deriveFont(40f);
  73         actualg2d.setFont(goldfont);
  74         actualg2d.drawString( " p " , 0, 30);
  75 
  76         // Render translucent bimg into all-white bimg
  77         actualBimg = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
  78         Graphics2D bg2d = actualBimg.createGraphics();
  79         bg2d.setColor(Color.WHITE);
  80         bg2d.fillRect(0, 0, actualBimg.getWidth(), actualBimg.getHeight());
  81         bg2d.drawImage(actualBI, 0, 0, null);
  82                     
  83 
  84         // Comparison Expected image
  85         // Create opaue bimg
  86         expectedBI = gc.createCompatibleImage(w, h, Transparency.OPAQUE);
  87 
  88         Graphics2D tempg2d = (Graphics2D) expectedBI.createGraphics();
  89 
  90         // Fill opaque bimg with white
  91         tempg2d.setColor(Color.WHITE);
  92         tempg2d.fillRect(0, 0, expectedBI.getWidth(), expectedBI.getHeight());
  93                    
  94         // Render translucent red into opaque bimg 
  95         tempg2d.setColor(new Color(255, 0, 0, 50));
  96         tempg2d.fillRect(0, 0, expectedBI.getWidth(), expectedBI.getHeight());
  97                     
  98         tempg2d.setColor(new Color(0,0,0,255));
  99         tempg2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
 100         Font font = frame.getFont().deriveFont(40f);
 101         tempg2d.setFont(font);
 102         tempg2d.drawString( " p " , 0, 30);
 103 
 104  
 105      
 106     }
 107 
 108     public static void main(String[] args) throws Exception {
 109         // Create the GUI on the event-dispatching thread
 110         SwingUtilities.invokeAndWait(new Runnable() {
 111 
 112             @Override
 113             public void run() {
 114                 TranslucentAAFont gtw = new TranslucentAAFont();
 115                 gtw.start();
 116                 
 117             }
 118         });
 119         test (expectedBI, actualBimg);
 120         frame.dispose();
 121     }
 122 
 123     private static void test(final BufferedImage gold, final BufferedImage bi)
 124             throws Exception {
 125         for (int x = 0; x < width; x++) {
 126             for (int y = 0; y < height; y++) {
 127                 if (gold.getRGB(x, y) != bi.getRGB(x, y)) {
 128                     ImageIO.write(gold, "png", new File("expected.png"));
 129                     ImageIO.write(bi, "png", new File("actual.png"));
 130                     System.out.println("gold.getRGB(" + x + ", " + y + ") = "
 131                             + Integer.toHexString(gold.getRGB(x, y)) + " != " +
 132                             "bi.getRGB("+ x + ", " + y + ") = " 
 133                             + Integer.toHexString(bi.getRGB(x, y)));
 134                     throw new RuntimeException("antialiasing artifacts");
 135                 }
 136             }
 137         }
 138     }
 139 }