Path: blob/master/test/jdk/sun/java2d/loops/RenderToCustomBufferTest.java
41149 views
/*1* Copyright (c) 2013, 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*/22/**23* @test24* @bug 8015606 822015025* @summary Test verifies whether a text is rendered correctly to26* a custom buffered image.27*28* @run main RenderToCustomBufferTest29*/3031import java.awt.Color;32import java.awt.Font;33import java.awt.Graphics2D;34import java.awt.RenderingHints;35import java.awt.Transparency;36import java.awt.color.ColorSpace;37import java.awt.image.BufferedImage;38import java.awt.image.ColorModel;39import java.awt.image.ComponentColorModel;40import java.awt.image.DataBuffer;41import java.awt.image.WritableRaster;4243public class RenderToCustomBufferTest {44public static void main(String[] args) {45final BufferedImage dst_custom = createCustomBuffer();46final BufferedImage dst_dcm = new BufferedImage(width, height,47BufferedImage.TYPE_INT_RGB);4849renderTo(dst_custom);50renderTo(dst_dcm);5152check(dst_custom, dst_dcm);53}5455private static void check(BufferedImage a, BufferedImage b) {56for (int y = 0; y < height; y++) {57for (int x = 0; x < width; x++) {58int pa = a.getRGB(x, y);59int pb = b.getRGB(x, y);6061if (pa != pb) {62String msg = String.format(63"Point [%d, %d] has different colors: %08X and %08X",64x, y, pa, pb);65throw new RuntimeException("Test failed: " + msg);66}67}68}69}7071private static BufferedImage createCustomBuffer() {72ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);73ColorModel cm = new ComponentColorModel(cs, false, false,74Transparency.OPAQUE, DataBuffer.TYPE_FLOAT);75WritableRaster wr = cm.createCompatibleWritableRaster(width, height);7677return new BufferedImage(cm, wr, false, null);78}7980private static void renderTo(BufferedImage dst) {81System.out.println("The buffer: " + dst);82Graphics2D g = dst.createGraphics();8384final int w = dst.getWidth();85final int h = dst.getHeight();8687g.setColor(Color.blue);88g.fillRect(0, 0, w, h);8990g.setColor(Color.red);91Font f = g.getFont();92g.setFont(f.deriveFont(48f));9394g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,95RenderingHints.VALUE_TEXT_ANTIALIAS_ON);9697// NB: this clip ctriggers the problem98g.setClip(50, 50, 200, 100);99100g.drawString("AA Text", 52, 90);101102g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,103RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);104105// NB: this clip ctriggers the problem106g.setClip(50, 100, 100, 100);107g.drawString("Text", 52, 148);108109g.dispose();110}111112private static final int width = 230;113private static final int height = 150;114}115116117