Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/Graphics2D/DrawString/DrawStrSuper.java
38828 views
/*1* Copyright (c) 2008, 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* @bug 668405626* @summary Super-scripted text needs to be positioned the same with27* drawString and TextLayout.28*/29import java.awt.*;30import java.awt.event.*;31import java.awt.font.*;32import static java.awt.font.TextAttribute.*;33import java.awt.geom.AffineTransform;34import java.awt.image.BufferedImage;35import java.util.HashMap;363738public class DrawStrSuper extends Component {3940int angle = 0;41static boolean interactive = false;4243int wid=400, hgt=400;44BufferedImage bi = null;4546void paintImage() {4748if (bi == null) {49bi = new BufferedImage(wid, hgt, BufferedImage.TYPE_INT_RGB);50}51Graphics2D g2d = bi.createGraphics();52g2d.setColor(Color.white);53g2d.fillRect(0, 0, wid, hgt);54g2d.translate(200, 200);5556Font fnt = new Font("Arial", Font.PLAIN, 20);57fnt = fnt.deriveFont(60.0f);58HashMap attrMap = new HashMap();59AffineTransform aff =60AffineTransform.getRotateInstance(angle * Math.PI/180.0);61attrMap.put(SUPERSCRIPT, SUPERSCRIPT_SUPER);62attrMap.put(TRANSFORM, aff);63fnt = fnt.deriveFont(attrMap);6465g2d.setFont(fnt);66g2d.setColor(Color.yellow);67TextLayout tl = new TextLayout("Text", fnt,g2d.getFontRenderContext());68g2d.fill(tl.getBounds());6970g2d.setColor(Color.black);71g2d.drawLine(-3, 0, 3, 0);72g2d.drawLine(0, -3, 0, 3);7374g2d.setColor(Color.blue);75g2d.drawString("Text", 0, 0);7677g2d.setColor(Color.red);78tl.draw(g2d,0f,0f);7980// Test BI: should be no blue81int blue = Color.blue.getRGB();82for (int px=0;px<wid;px++) {83for (int py=0;py<hgt;py++) {84int rgb = bi.getRGB(px, py);85if (rgb == blue) {86throw new RuntimeException87("Unexpected color : " + Integer.toHexString(rgb) +88" at x=" + px + " y="+ py);89}90}91}92}9394@Override95public void paint(Graphics g) {96paintImage();97g.drawImage(bi, 0,0, null);98}99100101static class Runner extends Thread {102103DrawStrSuper dss;104105Runner(DrawStrSuper dss) {106this.dss = dss;107}108109public void run() {110while (true) {111if (!interactive && dss.angle > 360) {112return;113}114try {115Thread.sleep(100);116} catch (InterruptedException e) {117return;118}119120dss.angle += 10;121dss.repaint();122}123}124}125126@Override127public Dimension getPreferredSize() {128return new Dimension(400, 400);129}130131public static void main(String argv[]) throws InterruptedException {132if (argv.length > 0) interactive = true;133134Frame f = new Frame("Text bounds test");135f.addWindowListener(new WindowAdapter() {136@Override137public void windowClosing(WindowEvent e) {138System.exit(0);139}140});141DrawStrSuper dss = new DrawStrSuper();142f.add(dss, BorderLayout.CENTER);143f.pack();144f.setLocationRelativeTo(null);145f.setVisible(true);146Runner runner = new Runner(dss);147runner.start();148runner.join();149}150}151152153