Path: blob/master/test/jdk/java/awt/GridLayout/LayoutExtraGaps/LayoutExtraGaps.java
41153 views
/*1* Copyright (c) 2006, 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 437031627@summary GridLayout does not centre its component properly28(summary was GridLayout does not fill its Container)29@library ../../regtesthelpers30@build Util31@author Andrei Dmitriev : area=awt.layout32@run main LayoutExtraGaps33*/3435import java.awt.*;36import java.awt.event.*;37import test.java.awt.regtesthelpers.Util;3839public class LayoutExtraGaps extends Frame {40final static int compCount = 30;4142public LayoutExtraGaps() {43super("GridLayoutTest");44Panel yellowPanel = new Panel(new GridLayout(compCount, 1, 3, 3));45yellowPanel.setBackground(Color.yellow);4647for(int i = 0; i < compCount ; i++) {48Label redLabel = new Label(""+i);49redLabel.setBackground(Color.red);50yellowPanel.add(redLabel);51}5253Panel bluePanel = new Panel(new GridLayout(1, compCount, 3, 3));54bluePanel.setBackground(Color.blue);5556for(int i = 0; i < compCount; i++) {57Label greenLabel = new Label(""+i);58greenLabel.setBackground(Color.green);59bluePanel.add(greenLabel);60}6162//RTL orientation63Panel blackPanel = new Panel(new GridLayout(compCount, 1, 3, 3));64blackPanel.setBackground(Color.black);65blackPanel.applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);6667for(int i = 0; i < compCount ; i++) {68Label redLabel = new Label(""+i);69redLabel.setBackground(Color.red);70blackPanel.add(redLabel);71}7273Panel redPanel = new Panel(new GridLayout(1, compCount, 3, 3));74redPanel.applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);75redPanel.setBackground(Color.red);7677for(int i = 0; i < compCount; i++) {78Label greenLabel = new Label(""+i);79greenLabel.setBackground(Color.green);80redPanel.add(greenLabel);81}8283setLayout(new GridLayout(2, 2, 20, 20));8485add(yellowPanel);86add(bluePanel);87add(redPanel);88add(blackPanel);89pack();90setSize((int)getPreferredSize().getWidth() + 50, (int)getPreferredSize().getHeight() + 50);91setVisible(true);9293Util.waitForIdle(Util.createRobot());9495if (isComponentCentredLTR(yellowPanel) && isComponentCentredLTR(bluePanel)96&& isComponentCentredLTR(blackPanel) && isComponentCentredRTL(redPanel))97{98System.out.println("Test passed.");99} else {100throw new RuntimeException("Test failed. GridLayout doesn't center component.");101}102}103104/**105* Checks if the components under Panel p are properly centred (i.e.106* opposite borders between the Panel and component are equal). Panel p107* must not be affect by RTL orientation (RTL only affects horizontal108* positioning and components lay out from top right corner).109*110* @param p the panel where the components exist and is not affected111* by right to left orientation112* @return true if components of panel p are properly centre, false113* otherwise114*/115public static boolean isComponentCentredLTR(Panel p) {116double borderLeft;117double borderRight;118double borderTop;119double borderBottom;120121//The first component(rectangle) in panel p.122Rectangle firstRec = p.getComponent(0).getBounds();123124//The last component(rectangle) in panel p.125Rectangle lastRec = p.getComponent(compCount - 1).getBounds();126127System.out.println("bounds of the first rectangle in "+ p.getName() + " = " + firstRec);128System.out.println("bounds of the last rectangle in "+ p.getName() + " = " + lastRec);129130borderLeft = firstRec.getX();131borderRight = p.getWidth() - lastRec.getWidth() - lastRec.getX();132borderTop = firstRec.getY();133borderBottom = p.getHeight() - lastRec.getHeight() - lastRec.getY();134135return areBordersEqual(borderLeft, borderRight) &&136areBordersEqual(borderTop, borderBottom);137}138139/**140* Checks if the components under Panel p are properly centred (i.e.141* opposite borders between the Panel and component are equal). Panel p142* must be affect by RTL orientation (RTL only affects horizontal positioning143* and components lay out from top right corner).144*145* @param p the panel where the components exist and is affected by146* right to left orientation147* @return true if components of panel p are properly centre, false148* otherwise149*/150public static boolean isComponentCentredRTL(Panel p) {151double borderLeft;152double borderRight;153double borderTop;154double borderBottom;155156//The first component(rectangle) in panel p.157Rectangle firstRec = p.getComponent(0).getBounds();158159//The last component(rectangle) in panel p.160Rectangle lastRec = p.getComponent(compCount - 1).getBounds();161162System.out.println("bounds of the first rectangle in "+ p.getName() + " = " + firstRec);163System.out.println("bounds of the last rectangle in "+ p.getName() + " = " + lastRec);164165borderLeft = lastRec.getX();166borderRight = p.getWidth() - firstRec.getWidth() - firstRec.getX();167borderTop = lastRec.getY();168borderBottom = p.getHeight() - firstRec.getHeight() - firstRec.getY();169170return areBordersEqual(borderLeft, borderRight) &&171areBordersEqual(borderTop, borderBottom);172}173174/**175* Given two borders border1 and border2 check if they are equal.176*177* @param border1 one of the borders being compared178* @param border2 the other border being compared179* @return true if border1 and border2 are equal to each other (i.e.180* their width/height difference is at most 1, assuming the181* smallest pixel is of size 1), false otherwise182*/183public static boolean areBordersEqual(double border1, double border2) {184return Math.abs(border1 - border2) <= 1;185}186187public static void main(String[] args) {188new LayoutExtraGaps();189}190}191192193