1 /* 2 * Copyright (c) 2006, 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 24 /* 25 @test 26 @key headful 27 @bug 4370316 28 @summary GridLayout does not centre its component properly 29 (summary was GridLayout does not fill its Container) 30 @library ../../regtesthelpers 31 @build Util 32 @author Andrei Dmitriev : area=awt.layout 33 @run main LayoutExtraGaps 34 */ 35 36 import java.awt.*; 37 import java.awt.event.*; 38 import test.java.awt.regtesthelpers.Util; 39 40 public class LayoutExtraGaps extends Frame { 41 final static int compCount = 30; 42 43 public LayoutExtraGaps() { 44 super("GridLayoutTest"); 45 Panel yellowPanel = new Panel(new GridLayout(compCount, 1, 3, 3)); 46 yellowPanel.setBackground(Color.yellow); 47 48 for(int i = 0; i < compCount ; i++) { 49 Label redLabel = new Label(""+i); 50 redLabel.setBackground(Color.red); 51 yellowPanel.add(redLabel); 52 } 53 54 Panel bluePanel = new Panel(new GridLayout(1, compCount, 3, 3)); 55 bluePanel.setBackground(Color.blue); 56 57 for(int i = 0; i < compCount; i++) { 58 Label greenLabel = new Label(""+i); 59 greenLabel.setBackground(Color.green); 60 bluePanel.add(greenLabel); 61 } 62 63 //RTL orientation 64 Panel blackPanel = new Panel(new GridLayout(compCount, 1, 3, 3)); 65 blackPanel.setBackground(Color.black); 66 blackPanel.applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); 67 68 for(int i = 0; i < compCount ; i++) { 69 Label redLabel = new Label(""+i); 70 redLabel.setBackground(Color.red); 71 blackPanel.add(redLabel); 72 } 73 74 Panel redPanel = new Panel(new GridLayout(1, compCount, 3, 3)); 75 redPanel.applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); 76 redPanel.setBackground(Color.red); 77 78 for(int i = 0; i < compCount; i++) { 79 Label greenLabel = new Label(""+i); 80 greenLabel.setBackground(Color.green); 81 redPanel.add(greenLabel); 82 } 83 84 setLayout(new GridLayout(2, 2, 20, 20)); 85 86 add(yellowPanel); 87 add(bluePanel); 88 add(redPanel); 89 add(blackPanel); 90 pack(); 91 setSize((int)getPreferredSize().getWidth() + 50, (int)getPreferredSize().getHeight() + 50); 92 setVisible(true); 93 94 Util.waitForIdle(Util.createRobot()); 95 96 if (isComponentCentredLTR(yellowPanel) && isComponentCentredLTR(bluePanel) 97 && isComponentCentredLTR(blackPanel) && isComponentCentredRTL(redPanel)) 98 { 99 System.out.println("Test passed."); 100 } else { 101 throw new RuntimeException("Test failed. GridLayout doesn't center component."); 102 } 103 } 104 105 /** 106 * Checks if the components under Panel p are properly centred (i.e. 107 * opposite borders between the Panel and component are equal). Panel p 108 * must not be affect by RTL orientation (RTL only affects horizontal 109 * positioning and components lay out from top right corner). 110 * 111 * @param p the panel where the components exist and is not affected 112 * by right to left orientation 113 * @return true if components of panel p are properly centre, false 114 * otherwise 115 */ 116 public static boolean isComponentCentredLTR(Panel p) { 117 double borderLeft; 118 double borderRight; 119 double borderTop; 120 double borderBottom; 121 122 //The first component(rectangle) in panel p. 123 Rectangle firstRec = p.getComponent(0).getBounds(); 124 125 //The last component(rectangle) in panel p. 126 Rectangle lastRec = p.getComponent(compCount - 1).getBounds(); 127 128 System.out.println("bounds of the first rectangle in "+ p.getName() + " = " + firstRec); 129 System.out.println("bounds of the last rectangle in "+ p.getName() + " = " + lastRec); 130 131 borderLeft = firstRec.getX(); 132 borderRight = p.getWidth() - lastRec.getWidth() - lastRec.getX(); 133 borderTop = firstRec.getY(); 134 borderBottom = p.getHeight() - lastRec.getHeight() - lastRec.getY(); 135 136 return areBordersEqual(borderLeft, borderRight) && 137 areBordersEqual(borderTop, borderBottom); 138 } 139 140 /** 141 * Checks if the components under Panel p are properly centred (i.e. 142 * opposite borders between the Panel and component are equal). Panel p 143 * must be affect by RTL orientation (RTL only affects horizontal positioning 144 * and components lay out from top right corner). 145 * 146 * @param p the panel where the components exist and is affected by 147 * right to left orientation 148 * @return true if components of panel p are properly centre, false 149 * otherwise 150 */ 151 public static boolean isComponentCentredRTL(Panel p) { 152 double borderLeft; 153 double borderRight; 154 double borderTop; 155 double borderBottom; 156 157 //The first component(rectangle) in panel p. 158 Rectangle firstRec = p.getComponent(0).getBounds(); 159 160 //The last component(rectangle) in panel p. 161 Rectangle lastRec = p.getComponent(compCount - 1).getBounds(); 162 163 System.out.println("bounds of the first rectangle in "+ p.getName() + " = " + firstRec); 164 System.out.println("bounds of the last rectangle in "+ p.getName() + " = " + lastRec); 165 166 borderLeft = lastRec.getX(); 167 borderRight = p.getWidth() - firstRec.getWidth() - firstRec.getX(); 168 borderTop = lastRec.getY(); 169 borderBottom = p.getHeight() - firstRec.getHeight() - firstRec.getY(); 170 171 return areBordersEqual(borderLeft, borderRight) && 172 areBordersEqual(borderTop, borderBottom); 173 } 174 175 /** 176 * Given two borders border1 and border2 check if they are equal. 177 * 178 * @param border1 one of the borders being compared 179 * @param border2 the other border being compared 180 * @return true if border1 and border2 are equal to each other (i.e. 181 * their width/height difference is at most 1, assuming the 182 * smallest pixel is of size 1), false otherwise 183 */ 184 public static boolean areBordersEqual(double border1, double border2) { 185 return Math.abs(border1 - border2) <= 1; 186 } 187 188 public static void main(String[] args) { 189 new LayoutExtraGaps(); 190 } 191 }