RenderToCustomBufferTest.java revision 6126
3544N/A/*
3544N/A * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
3544N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3544N/A *
3544N/A * This code is free software; you can redistribute it and/or modify it
3544N/A * under the terms of the GNU General Public License version 2 only, as
3544N/A * published by the Free Software Foundation.
3544N/A *
3544N/A * This code is distributed in the hope that it will be useful, but WITHOUT
3544N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3544N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
3544N/A * version 2 for more details (a copy is included in the LICENSE file that
3544N/A * accompanied this code).
3544N/A *
3544N/A * You should have received a copy of the GNU General Public License version
3544N/A * 2 along with this work; if not, write to the Free Software Foundation,
3544N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3544N/A *
3544N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3544N/A * or visit www.oracle.com if you need additional information or have any
3544N/A * questions.
3544N/A */
4495N/A/**
5611N/A * @test
4586N/A * @bug 8015606
4586N/A * @summary Test verifies whether a text is rendered correctly to
4586N/A * a custom buffered image.
3544N/A *
3544N/A * @run main RenderToCustomBufferTest
3544N/A */
3544N/A
3544N/Aimport java.awt.Color;
3544N/Aimport java.awt.Font;
4586N/Aimport java.awt.Graphics2D;
3544N/Aimport java.awt.RenderingHints;
3544N/Aimport java.awt.Transparency;
3544N/Aimport java.awt.color.ColorSpace;
3544N/Aimport java.awt.image.BufferedImage;
3544N/Aimport java.awt.image.ColorModel;
3544N/Aimport java.awt.image.ComponentColorModel;
3544N/Aimport java.awt.image.DataBuffer;
3544N/Aimport java.awt.image.WritableRaster;
3544N/A
3544N/Apublic class RenderToCustomBufferTest {
3544N/A public static void main(String[] args) {
3544N/A final BufferedImage dst_custom = createCustomBuffer();
3544N/A final BufferedImage dst_dcm = new BufferedImage(width, height,
3544N/A BufferedImage.TYPE_INT_RGB);
3544N/A
3544N/A renderTo(dst_custom);
3544N/A renderTo(dst_dcm);
3544N/A
3544N/A check(dst_custom, dst_dcm);
4305N/A }
3544N/A
4305N/A private static void check(BufferedImage a, BufferedImage b) {
3544N/A for (int y = 0; y < height; y++) {
3544N/A for (int x = 0; x < width; x++) {
3544N/A int pa = a.getRGB(x, y);
3544N/A int pb = b.getRGB(x, y);
3544N/A
3544N/A if (pa != pb) {
3544N/A String msg = String.format(
3544N/A "Point [%d, %d] has different colors: %08X and %08X",
3544N/A x, y, pa, pb);
3544N/A throw new RuntimeException("Test failed: " + msg);
3544N/A }
3544N/A }
3544N/A }
3544N/A }
3544N/A
4586N/A private static BufferedImage createCustomBuffer() {
4586N/A ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
4586N/A ColorModel cm = new ComponentColorModel(cs, false, false,
3544N/A Transparency.OPAQUE, DataBuffer.TYPE_FLOAT);
4586N/A WritableRaster wr = cm.createCompatibleWritableRaster(width, height);
4586N/A
3544N/A return new BufferedImage(cm, wr, false, null);
4586N/A }
4586N/A
3544N/A private static void renderTo(BufferedImage dst) {
3544N/A System.out.println("The buffer: " + dst);
3544N/A Graphics2D g = dst.createGraphics();
3544N/A
3544N/A final int w = dst.getWidth();
3544N/A final int h = dst.getHeight();
3544N/A
4586N/A g.setColor(Color.blue);
3544N/A g.fillRect(0, 0, w, h);
3544N/A
3544N/A g.setColor(Color.red);
3544N/A Font f = g.getFont();
3544N/A g.setFont(f.deriveFont(48f));
4586N/A
3544N/A g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
4586N/A RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
3544N/A
4586N/A // NB: this clip ctriggers the problem
3544N/A g.setClip(50, 50, 200, 100);
3544N/A
3544N/A g.drawString("AA Text", 52, 90);
4586N/A
3544N/A g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
4495N/A RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
4586N/A
4305N/A // NB: this clip ctriggers the problem
3544N/A g.setClip(50, 100, 100, 100);
3544N/A g.drawString("Text", 52, 148);
3544N/A
3544N/A g.dispose();
3544N/A }
3544N/A
3544N/A private static final int width = 230;
3544N/A private static final int height = 150;
3544N/A}
3544N/A