Path: blob/master/test/jdk/java/awt/Graphics2D/DrawString/TextRenderingTest.java
41153 views
/*1* Copyright (c) 2013, 2016, 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*/2223import java.awt.Color;24import java.awt.Graphics2D;25import java.awt.GraphicsConfiguration;26import java.awt.GraphicsEnvironment;27import java.awt.GradientPaint;28import java.awt.geom.Point2D;2930import java.awt.Font;3132import java.awt.image.BufferedImage;33import java.awt.image.VolatileImage;343536/*37* @test38* @key headful39* @bug 7189452 802476740* @summary Check if source offset for text rendering is handled correctly41* (shouldn't see the text on a similarly colored background).42* @author a.stepanov43* @run main TextRenderingTest44*/4546public class TextRenderingTest {4748private static final int width = 450;49private static final int height = 150;5051public static void main(final String[] args) {5253GraphicsEnvironment ge =54GraphicsEnvironment.getLocalGraphicsEnvironment();55GraphicsConfiguration gc =56ge.getDefaultScreenDevice().getDefaultConfiguration();57VolatileImage vi = gc.createCompatibleVolatileImage(width, height);5859while (true) {60vi.validate(gc);61Graphics2D g2d = vi.createGraphics();62g2d.setColor(Color.white);63g2d.fillRect(0, 0, width, height);6465g2d.setPaint(new GradientPaint(66new Point2D.Float(0, height / 2), Color.white,67new Point2D.Float(width, height / 2), Color.black));68g2d.fillRect(0, 0, width, height);6970String fnt = g2d.getFont().getFamily();71g2d.setFont(new Font(fnt, Font.PLAIN, 100));72g2d.drawString("IIIIIIIIII", 100, 100); // draw text with offset7374g2d.dispose();7576if (vi.validate(gc) != VolatileImage.IMAGE_OK) {77try {78Thread.sleep(100);79} catch (InterruptedException e) {}80continue;81}8283if (vi.contentsLost()) {84try {85Thread.sleep(100);86} catch (InterruptedException e) {}87continue;88}8990break;91}9293BufferedImage bi = vi.getSnapshot();9495// the text shifted shouldn't be visible onto a painted rectangle!96// so the check: color component (blue) must decrease monotonously9798int prev = Integer.MAX_VALUE;99for (int x = 0; x < width; ++x) {100int color = bi.getRGB(x, height / 2);101int b = color & 0xFF;102103if (b > prev) {104throw new RuntimeException("test failed: can see the text rendered!");105}106107prev = b;108}109}110}111112113