Path: blob/master/test/jdk/java/awt/Graphics2D/DrawString/AlphaSurfaceText.java
41153 views
/*1* Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 667930826* @summary test drawing to Alpha surfaces27*/2829import java.awt.*;30import java.awt.image.*;3132public class AlphaSurfaceText {3334int wid=400, hgt=200;3536public AlphaSurfaceText(int biType, Color c) {37BufferedImage opaquebi0 =38new BufferedImage(wid, hgt, BufferedImage.TYPE_INT_RGB);39drawText(opaquebi0, c);4041BufferedImage alphabi = new BufferedImage(wid, hgt, biType);42drawText(alphabi, c);43BufferedImage opaquebi1 =44new BufferedImage(wid, hgt, BufferedImage.TYPE_INT_RGB);45Graphics2D g2d = opaquebi1.createGraphics();46g2d.drawImage(alphabi, 0, 0, null);47compare(opaquebi0, opaquebi1, biType, c);48}4950private void drawText(BufferedImage bi, Color c) {51Graphics2D g = bi.createGraphics();52g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,53RenderingHints.VALUE_TEXT_ANTIALIAS_ON);54g.setColor(c);55g.setFont(new Font("sansserif", Font.PLAIN, 70));56g.drawString("Hello!", 20, 100);57g.setFont(new Font("sansserif", Font.PLAIN, 12));58g.drawString("Hello!", 20, 130);59g.setFont(new Font("sansserif", Font.PLAIN, 10));60g.drawString("Hello!", 20, 150);61}6263// Need to allow for minimal rounding error, so allow each component64// to differ by 1.65void compare(BufferedImage bi0, BufferedImage bi1, int biType, Color c) {66for (int x=0; x<wid; x++) {67for (int y=0; y<hgt; y++) {68int rgb0 = bi0.getRGB(x, y);69int rgb1 = bi1.getRGB(x, y);70if (rgb0 == rgb1) continue;71int r0 = (rgb0 & 0xff0000) >> 16;72int r1 = (rgb1 & 0xff0000) >> 16;73int rdiff = r0-r1; if (rdiff<0) rdiff = -rdiff;74int g0 = (rgb0 & 0x00ff00) >> 8;75int g1 = (rgb1 & 0x00ff00) >> 8;76int gdiff = g0-g1; if (gdiff<0) gdiff = -gdiff;77int b0 = (rgb0 & 0x0000ff);78int b1 = (rgb1 & 0x0000ff);79int bdiff = b0-b1; if (bdiff<0) bdiff = -bdiff;80if (rdiff > 1 || gdiff > 1 || bdiff > 1) {81throw new RuntimeException(82"Images differ for type "+biType + " col="+c +83" at x=" + x + " y="+ y + " " +84Integer.toHexString(rgb0) + " vs " +85Integer.toHexString(rgb1));86}87}88}8990}91public static void main(String[] args) {92new AlphaSurfaceText(BufferedImage.TYPE_INT_ARGB, Color.white);93new AlphaSurfaceText(BufferedImage.TYPE_INT_ARGB, Color.red);94new AlphaSurfaceText(BufferedImage.TYPE_INT_ARGB, Color.blue);95new AlphaSurfaceText(BufferedImage.TYPE_INT_ARGB_PRE, Color.white);96new AlphaSurfaceText(BufferedImage.TYPE_INT_ARGB_PRE, Color.red);97new AlphaSurfaceText(BufferedImage.TYPE_INT_ARGB_PRE, Color.blue);98new AlphaSurfaceText(BufferedImage.TYPE_4BYTE_ABGR, Color.white);99new AlphaSurfaceText(BufferedImage.TYPE_4BYTE_ABGR, Color.red);100new AlphaSurfaceText(BufferedImage.TYPE_4BYTE_ABGR, Color.blue);101new AlphaSurfaceText(BufferedImage.TYPE_4BYTE_ABGR_PRE, Color.white);102new AlphaSurfaceText(BufferedImage.TYPE_4BYTE_ABGR_PRE, Color.red);103new AlphaSurfaceText(BufferedImage.TYPE_4BYTE_ABGR_PRE, Color.blue);104}105}106107108