Path: blob/master/test/jdk/java/awt/Graphics2D/DrawString/ScaledLCDTextMetrics.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 668531227* @summary Check advance of LCD text on a scaled graphics.28*/2930import javax.swing.*;31import java.awt.*;32import static java.awt.RenderingHints.*;3334public class ScaledLCDTextMetrics extends Component {3536public static void main(String[] args) {37JFrame f = new JFrame();38f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);39f.add("Center", new ScaledLCDTextMetrics());40f.pack();41f.setVisible(true);42}4344public Dimension getPreferredSize() {45return new Dimension(200,100);46}47public void paint(Graphics g) {48Graphics2D g2 = (Graphics2D)g;4950Font f = new Font("Tahoma", Font.PLAIN, 11);51g.setFont(f);52g.setColor(Color.white);53g.fillRect(0,0,400,300);54g.setColor(Color.black);55g2.setRenderingHint(KEY_TEXT_ANTIALIASING,VALUE_TEXT_ANTIALIAS_LCD_HRGB);56String text = "ABCDEFGHIJKLI";5758FontMetrics fm1 = g2.getFontMetrics();59int adv1 = fm1.stringWidth(text);60g.drawString(text, 5, 20);6162g2.scale(2,2);6364FontMetrics fm2 = g2.getFontMetrics();65int adv2 = fm2.stringWidth(text);66g.drawString(text, 5, 40);6768double frac = Math.abs(adv1/(double)adv2);6970System.out.println("scalex1: " + adv1);71System.out.println("scalex2: " + adv2);72System.out.println("Fraction : "+ frac);7374// adv1 will not be exactly the same as adv2, but should differ75// only by a fraction.7677if (frac < 0.8 || frac > 1.2) {78throw new RuntimeException("Metrics differ " +79"Adv1="+adv1+" Adv2="+adv2+" Fraction="+frac);80}81}82}838485