Path: blob/master/test/jdk/java/awt/Graphics2D/DrawString/DrawRotatedStringUsingRotatedFont.java
41153 views
/*1* Copyright (c) 2014, 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.Font;25import java.awt.Graphics2D;26import java.awt.RenderingHints;27import java.awt.geom.AffineTransform;28import java.awt.image.BufferedImage;29import java.io.File;30import java.io.IOException;3132import javax.imageio.ImageIO;3334import static java.awt.image.BufferedImage.TYPE_INT_RGB;35import static java.lang.Math.toRadians;3637/**38* @test39* @bug 806537340* @summary Verifies that we get correct direction, when draw rotated string.41* @author Sergey Bylokhov42* @run main DrawRotatedStringUsingRotatedFont43*/44public final class DrawRotatedStringUsingRotatedFont {4546private static final int SIZE = 500;47private static final String STR = "MMMMMMMMMMMMMMMM";4849private static AffineTransform[] txs = {50AffineTransform.getRotateInstance(toRadians(00)),51AffineTransform.getRotateInstance(toRadians(45)),52AffineTransform.getRotateInstance(toRadians(-45)),53AffineTransform.getRotateInstance(toRadians(90)),54AffineTransform.getRotateInstance(toRadians(-90)),55AffineTransform.getRotateInstance(toRadians(135)),56AffineTransform.getRotateInstance(toRadians(-135)),57AffineTransform.getRotateInstance(toRadians(180)),58AffineTransform.getRotateInstance(toRadians(-180)),59AffineTransform.getRotateInstance(toRadians(225)),60AffineTransform.getRotateInstance(toRadians(-225)),61AffineTransform.getRotateInstance(toRadians(270)),62AffineTransform.getRotateInstance(toRadians(-270)),63AffineTransform.getRotateInstance(toRadians(315)),64AffineTransform.getRotateInstance(toRadians(-315)),65AffineTransform.getRotateInstance(toRadians(360)),66AffineTransform.getRotateInstance(toRadians(-360))67};6869public static void main(final String[] args) throws IOException {70for (final AffineTransform tx2 : txs) {71for (final AffineTransform tx1 : txs) {72for (final boolean aa : new boolean[]{true, false}) {73final BufferedImage bi1 = createImage(aa, tx1, tx2);74final BufferedImage bi2 = createImage(aa, tx2, tx1);75compareImage(bi1, bi2);76fillTextArea(bi1, tx1, tx2);77fillTextArea(bi2, tx2, tx1);78checkColors(bi1, bi2);79}80}81}82System.out.println("Passed");83}8485/**86* Compares two images.87*/88private static void compareImage(final BufferedImage bi1,89final BufferedImage bi2)90throws IOException {91for (int i = 0; i < SIZE; ++i) {92for (int j = 0; j < SIZE; ++j) {93if (bi1.getRGB(i, j) != bi2.getRGB(i, j)) {94ImageIO.write(bi1, "png", new File("image1.png"));95ImageIO.write(bi2, "png", new File("image2.png"));96throw new RuntimeException("Failed: wrong text location");97}98}99}100}101102/**103* Checks an image color. RED and GREEN are allowed only.104*/105private static void checkColors(final BufferedImage bi1,106final BufferedImage bi2)107throws IOException {108for (int i = 0; i < SIZE; ++i) {109for (int j = 0; j < SIZE; ++j) {110final int rgb1 = bi1.getRGB(i, j);111final int rgb2 = bi2.getRGB(i, j);112if (rgb1 != rgb2 || rgb1 != 0xFFFF0000 && rgb1 != 0xFF00FF00) {113ImageIO.write(bi1, "png", new File("image1.png"));114ImageIO.write(bi2, "png", new File("image2.png"));115throw new RuntimeException("Failed: wrong text location");116}117}118}119}120121/**122* Creates an BufferedImage and draws a text, using two transformations,123* one for graphics and one for font.124*/125private static BufferedImage createImage(final boolean aa,126final AffineTransform gtx,127final AffineTransform ftx) {128final BufferedImage bi = new BufferedImage(SIZE, SIZE, TYPE_INT_RGB);129final Graphics2D bg = bi.createGraphics();130bg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,131aa ? RenderingHints.VALUE_ANTIALIAS_ON132: RenderingHints.VALUE_ANTIALIAS_OFF);133bg.setColor(Color.RED);134bg.fillRect(0, 0, SIZE, SIZE);135bg.translate(100, 100);136bg.transform(gtx);137bg.setColor(Color.BLACK);138bg.setFont(bg.getFont().deriveFont(20.0f).deriveFont(ftx));139bg.drawString(STR, 0, 0);140bg.dispose();141return bi;142}143144/**145* Fills the area of text using green solid color.146*/147private static void fillTextArea(final BufferedImage bi,148final AffineTransform tx1,149final AffineTransform tx2) {150final Graphics2D bg = bi.createGraphics();151bg.translate(100, 100);152bg.transform(tx1);153bg.transform(tx2);154bg.setColor(Color.GREEN);155final Font font = bg.getFont().deriveFont(20.0f);156bg.setFont(font);157bg.fill(font.getStringBounds(STR, bg.getFontRenderContext()));158bg.dispose();159}160}161162163164