1 /* 2 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 import java.awt.Font; 24 import java.awt.Graphics; 25 import java.util.concurrent.CountDownLatch; 26 import java.util.concurrent.TimeUnit; 27 import javax.swing.JFrame; 28 import javax.swing.JLabel; 29 import javax.swing.SwingUtilities; 30 31 /** 32 * @test 33 * @bug 8142966 34 * @summary Wrong cursor position in text components on HiDPI display 35 * @run main/othervm -Dsun.java2d.uiScale=2 SwingFontMetricsTest 36 */ 37 public class SwingFontMetricsTest { 38 39 private static final String LOWER_CASE_TEXT = "the quick brown fox jumps over the lazy dog"; 40 private static final String UPPER_CASE_TEXT = LOWER_CASE_TEXT.toUpperCase(); 41 private static final String TEXT = LOWER_CASE_TEXT + UPPER_CASE_TEXT; 42 private static boolean passed = false; 43 private static CountDownLatch latch = new CountDownLatch(1); 44 45 public static void main(String[] args) throws Exception { 46 SwingUtilities.invokeAndWait(SwingFontMetricsTest::createAndShowGUI); 47 latch.await(5, TimeUnit.SECONDS); 48 49 if (!passed) { 50 throw new RuntimeException("Test Failed!"); 51 } 52 } 53 54 private static void createAndShowGUI() { 55 final JFrame frame = new JFrame(); 56 frame.setSize(300, 300); 57 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 58 JLabel label = new JLabel(TEXT) { 59 @Override 60 public void paint(Graphics g) { 61 super.paint(g); 62 Font font = getFont(); 63 int width1 = getFontMetrics(font).stringWidth(TEXT); 64 int width2 = g.getFontMetrics(font).stringWidth(TEXT); 65 passed = (width1 == width2); 66 latch.countDown(); 67 frame.dispose(); 68 } 69 }; 70 frame.add(label); 71 frame.setVisible(true); 72 } 73 }