/* * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ import java.awt.*; import javax.swing.*; import java.awt.image.BufferedImage; import javax.imageio.ImageIO; import java.io.File; import java.io.IOException; /** * @test * @bug 8040689 * @summary Tests if antialiasing artifact are shown for font drawn on * translucent destination */ public class TranslucentAAFont { final private static int width = 40; final private static int height = 40; private static JFrame frame; private static BufferedImage actualBimg, expectedBI; public void start() { frame = new JFrame(); frame.setUndecorated(true); frame.setBackground(new Color(0, 0, 0, 0)); frame.setSize(new Dimension(width, height)); frame.setLocationRelativeTo(null); frame.setVisible(true); GraphicsEnvironment ge = GraphicsEnvironment .getLocalGraphicsEnvironment(); GraphicsConfiguration gc = ge.getDefaultScreenDevice() .getDefaultConfiguration(); int w = frame.getWidth(); int h = frame.getHeight(); // Actual image // Create translucent bimg BufferedImage actualBI = gc.createCompatibleImage(w, h, Transparency.TRANSLUCENT); // Render translucent red into translucent bimg Graphics2D actualg2d = (Graphics2D) actualBI.createGraphics(); actualg2d.setColor(new Color(255, 0, 0, 50)); actualg2d.fillRect(0, 0, w, h); // Render text on translucent bimg actualg2d.setColor(new Color(0,0,0,255)); actualg2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); Font goldfont = frame.getFont().deriveFont(40f); actualg2d.setFont(goldfont); actualg2d.drawString( " p " , 0, 30); // Render translucent bimg into all-white bimg actualBimg = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB); Graphics2D bg2d = actualBimg.createGraphics(); bg2d.setColor(Color.WHITE); bg2d.fillRect(0, 0, actualBimg.getWidth(), actualBimg.getHeight()); bg2d.drawImage(actualBI, 0, 0, null); // Comparison Expected image // Create opaue bimg expectedBI = gc.createCompatibleImage(w, h, Transparency.OPAQUE); Graphics2D tempg2d = (Graphics2D) expectedBI.createGraphics(); // Fill opaque bimg with white tempg2d.setColor(Color.WHITE); tempg2d.fillRect(0, 0, expectedBI.getWidth(), expectedBI.getHeight()); // Render translucent red into opaque bimg tempg2d.setColor(new Color(255, 0, 0, 50)); tempg2d.fillRect(0, 0, expectedBI.getWidth(), expectedBI.getHeight()); tempg2d.setColor(new Color(0,0,0,255)); tempg2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); Font font = frame.getFont().deriveFont(40f); tempg2d.setFont(font); tempg2d.drawString( " p " , 0, 30); } public static void main(String[] args) throws Exception { // Create the GUI on the event-dispatching thread SwingUtilities.invokeAndWait(new Runnable() { @Override public void run() { TranslucentAAFont gtw = new TranslucentAAFont(); gtw.start(); } }); test (expectedBI, actualBimg); frame.dispose(); } private static void test(final BufferedImage gold, final BufferedImage bi) throws Exception { for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { if (gold.getRGB(x, y) != bi.getRGB(x, y)) { ImageIO.write(gold, "png", new File("expected.png")); ImageIO.write(bi, "png", new File("actual.png")); System.out.println("gold.getRGB(" + x + ", " + y + ") = " + Integer.toHexString(gold.getRGB(x, y)) + " != " + "bi.getRGB("+ x + ", " + y + ") = " + Integer.toHexString(bi.getRGB(x, y))); throw new RuntimeException("antialiasing artifacts"); } } } } }