Path: blob/master/test/jdk/java/awt/Graphics2D/DrawString/DrawStrSuper.java
41153 views
/*1* Copyright (c) 2008, 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*/2223/**24* @test25* @key headful26* @bug 668405627* @summary Super-scripted text needs to be positioned the same with28* drawString and TextLayout.29*/30import java.awt.*;31import java.awt.event.*;32import java.awt.font.*;33import static java.awt.font.TextAttribute.*;34import java.awt.geom.AffineTransform;35import java.awt.image.BufferedImage;36import java.util.HashMap;373839public class DrawStrSuper extends Component {4041int angle = 0;42static boolean interactive = false;4344int wid=400, hgt=400;45BufferedImage bi = null;4647void paintImage() {4849if (bi == null) {50bi = new BufferedImage(wid, hgt, BufferedImage.TYPE_INT_RGB);51}52Graphics2D g2d = bi.createGraphics();53g2d.setColor(Color.white);54g2d.fillRect(0, 0, wid, hgt);55g2d.translate(200, 200);5657Font fnt = new Font("Arial", Font.PLAIN, 20);58fnt = fnt.deriveFont(60.0f);59HashMap attrMap = new HashMap();60AffineTransform aff =61AffineTransform.getRotateInstance(angle * Math.PI/180.0);62attrMap.put(SUPERSCRIPT, SUPERSCRIPT_SUPER);63attrMap.put(TRANSFORM, aff);64fnt = fnt.deriveFont(attrMap);6566g2d.setFont(fnt);67g2d.setColor(Color.yellow);68TextLayout tl = new TextLayout("Text", fnt,g2d.getFontRenderContext());69g2d.fill(tl.getBounds());7071g2d.setColor(Color.black);72g2d.drawLine(-3, 0, 3, 0);73g2d.drawLine(0, -3, 0, 3);7475g2d.setColor(Color.blue);76g2d.drawString("Text", 0, 0);7778g2d.setColor(Color.red);79tl.draw(g2d,0f,0f);8081// Test BI: should be no blue82int blue = Color.blue.getRGB();83for (int px=0;px<wid;px++) {84for (int py=0;py<hgt;py++) {85int rgb = bi.getRGB(px, py);86if (rgb == blue) {87throw new RuntimeException88("Unexpected color : " + Integer.toHexString(rgb) +89" at x=" + px + " y="+ py);90}91}92}93}9495@Override96public void paint(Graphics g) {97paintImage();98g.drawImage(bi, 0,0, null);99}100101102static class Runner extends Thread {103104DrawStrSuper dss;105106Runner(DrawStrSuper dss) {107this.dss = dss;108}109110public void run() {111while (true) {112if (!interactive && dss.angle > 360) {113return;114}115try {116Thread.sleep(100);117} catch (InterruptedException e) {118return;119}120121dss.angle += 10;122dss.repaint();123}124}125}126127@Override128public Dimension getPreferredSize() {129return new Dimension(400, 400);130}131132public static void main(String argv[]) throws InterruptedException {133if (argv.length > 0) interactive = true;134135Frame f = new Frame("Text bounds test");136f.addWindowListener(new WindowAdapter() {137@Override138public void windowClosing(WindowEvent e) {139System.exit(0);140}141});142DrawStrSuper dss = new DrawStrSuper();143f.add(dss, BorderLayout.CENTER);144f.pack();145f.setLocationRelativeTo(null);146f.setVisible(true);147Runner runner = new Runner(dss);148runner.start();149runner.join();150}151}152153154