1 /* 2 * Copyright (c) 2020, 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 24 import java.awt.Button; 25 import java.awt.Canvas; 26 import java.awt.Checkbox; 27 import java.awt.Choice; 28 import java.awt.Component; 29 import java.awt.Frame; 30 import java.awt.GraphicsConfiguration; 31 import java.awt.GraphicsDevice; 32 import java.awt.GraphicsEnvironment; 33 import java.awt.Label; 34 import java.awt.List; 35 import java.awt.Rectangle; 36 import java.awt.Robot; 37 import java.awt.ScrollPane; 38 import java.awt.Scrollbar; 39 import java.awt.TextArea; 40 import java.awt.TextField; 41 import java.awt.Window; 42 43 /** 44 * @test 45 * @key headful 46 * @bug 8211999 47 * @run main/othervm SetComponentsBounds 48 * @run main/othervm -Dsun.java2d.uiScale=1 SetComponentsBounds 49 * @run main/othervm -Dsun.java2d.uiScale=2.25 SetComponentsBounds 50 */ 51 public final class SetComponentsBounds { 52 53 private static final int X = 111; 54 private static final int Y = 222; 55 private static final int WIDTH = 321; 56 private static final int HEIGHT = 123; 57 58 public static void main(String[] args) throws Exception { 59 var ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); 60 for (GraphicsDevice gd : ge.getScreenDevices()) { 61 test(gd.getDefaultConfiguration(), true); 62 test(gd.getDefaultConfiguration(), false); 63 } 64 } 65 66 private static void test(GraphicsConfiguration gc, boolean visible) throws Exception { 67 Rectangle screen = gc.getBounds(); 68 Window frame = new Frame(); 69 try { 70 frame.setLayout(null); // trigger use the minimum size of 71 // the peer 72 frame.setBounds(screen.x + 100, screen.y + 100, 500, 500); 73 frame.add(new Button()); 74 frame.add(new Canvas()); 75 frame.add(new Checkbox()); 76 frame.add(new Choice()); 77 frame.add(new Label()); 78 frame.add(new List()); 79 frame.add(new Scrollbar()); 80 frame.add(new ScrollPane()); 81 frame.add(new TextArea()); 82 frame.add(new TextField()); 83 for (Component comp : frame.getComponents()) { 84 comp.setBounds(X, Y, WIDTH, HEIGHT); 85 } 86 if (visible) { 87 frame.setVisible(true); 88 } else { 89 frame.pack(); 90 } 91 Robot robot = new Robot(); 92 robot.waitForIdle(); 93 checkGC(gc, frame); 94 for (Component comp : frame.getComponents()) { 95 Rectangle bounds = comp.getBounds(); 96 if (bounds.x != X || bounds.y != Y || bounds.width != WIDTH) { 97 System.err.println("Screen bounds:" + screen); 98 System.err.println("Component:" + comp); 99 throw new RuntimeException("Wrong bounds:" + bounds); 100 } 101 if (bounds.height > HEIGHT) { 102 // different check for HEIGHT, it depends from the font 103 throw new RuntimeException("Wrong height:" + bounds.height); 104 } 105 checkGC(gc, comp); 106 } 107 } finally { 108 frame.dispose(); 109 } 110 } 111 112 private static void checkGC(GraphicsConfiguration gc, Component comp) { 113 GraphicsConfiguration compGC = comp.getGraphicsConfiguration(); 114 if (compGC != gc) { 115 System.err.println("Expected GC:" + gc); 116 System.err.println("Actual GC:" + compGC); 117 throw new RuntimeException(); 118 } 119 } 120 }