Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/GridLayout/LayoutExtraGaps/LayoutExtraGaps.java
38828 views
/*1* Copyright (c) 2006, 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@bug 437031626@summary GridLayout does not centre its component properly27(summary was GridLayout does not fill its Container)28@library ../../regtesthelpers29@build Util30@author Andrei Dmitriev : area=awt.layout31@run main LayoutExtraGaps32*/3334import java.awt.*;35import java.awt.event.*;36import test.java.awt.regtesthelpers.Util;3738public class LayoutExtraGaps extends Frame {39final static int compCount = 30;4041public LayoutExtraGaps() {42super("GridLayoutTest");43Panel yellowPanel = new Panel(new GridLayout(compCount, 1, 3, 3));44yellowPanel.setBackground(Color.yellow);4546for(int i = 0; i < compCount ; i++) {47Label redLabel = new Label(""+i);48redLabel.setBackground(Color.red);49yellowPanel.add(redLabel);50}5152Panel bluePanel = new Panel(new GridLayout(1, compCount, 3, 3));53bluePanel.setBackground(Color.blue);5455for(int i = 0; i < compCount; i++) {56Label greenLabel = new Label(""+i);57greenLabel.setBackground(Color.green);58bluePanel.add(greenLabel);59}6061//RTL orientation62Panel blackPanel = new Panel(new GridLayout(compCount, 1, 3, 3));63blackPanel.setBackground(Color.black);64blackPanel.applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);6566for(int i = 0; i < compCount ; i++) {67Label redLabel = new Label(""+i);68redLabel.setBackground(Color.red);69blackPanel.add(redLabel);70}7172Panel redPanel = new Panel(new GridLayout(1, compCount, 3, 3));73redPanel.applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);74redPanel.setBackground(Color.red);7576for(int i = 0; i < compCount; i++) {77Label greenLabel = new Label(""+i);78greenLabel.setBackground(Color.green);79redPanel.add(greenLabel);80}8182setLayout(new GridLayout(2, 2, 20, 20));8384add(yellowPanel);85add(bluePanel);86add(redPanel);87add(blackPanel);88pack();89setSize((int)getPreferredSize().getWidth() + 50, (int)getPreferredSize().getHeight() + 50);90setVisible(true);9192Util.waitForIdle(Util.createRobot());9394if (isComponentCentredLTR(yellowPanel) && isComponentCentredLTR(bluePanel)95&& isComponentCentredLTR(blackPanel) && isComponentCentredRTL(redPanel))96{97System.out.println("Test passed.");98} else {99throw new RuntimeException("Test failed. GridLayout doesn't center component.");100}101}102103/**104* Checks if the components under Panel p are properly centred (i.e.105* opposite borders between the Panel and component are equal). Panel p106* must not be affect by RTL orientation (RTL only affects horizontal107* positioning and components lay out from top right corner).108*109* @param p the panel where the components exist and is not affected110* by right to left orientation111* @return true if components of panel p are properly centre, false112* otherwise113*/114public static boolean isComponentCentredLTR(Panel p) {115double borderLeft;116double borderRight;117double borderTop;118double borderBottom;119120//The first component(rectangle) in panel p.121Rectangle firstRec = p.getComponent(0).getBounds();122123//The last component(rectangle) in panel p.124Rectangle lastRec = p.getComponent(compCount - 1).getBounds();125126System.out.println("bounds of the first rectangle in "+ p.getName() + " = " + firstRec);127System.out.println("bounds of the last rectangle in "+ p.getName() + " = " + lastRec);128129borderLeft = firstRec.getX();130borderRight = p.getWidth() - lastRec.getWidth() - lastRec.getX();131borderTop = firstRec.getY();132borderBottom = p.getHeight() - lastRec.getHeight() - lastRec.getY();133134return areBordersEqual(borderLeft, borderRight) &&135areBordersEqual(borderTop, borderBottom);136}137138/**139* Checks if the components under Panel p are properly centred (i.e.140* opposite borders between the Panel and component are equal). Panel p141* must be affect by RTL orientation (RTL only affects horizontal positioning142* and components lay out from top right corner).143*144* @param p the panel where the components exist and is affected by145* right to left orientation146* @return true if components of panel p are properly centre, false147* otherwise148*/149public static boolean isComponentCentredRTL(Panel p) {150double borderLeft;151double borderRight;152double borderTop;153double borderBottom;154155//The first component(rectangle) in panel p.156Rectangle firstRec = p.getComponent(0).getBounds();157158//The last component(rectangle) in panel p.159Rectangle lastRec = p.getComponent(compCount - 1).getBounds();160161System.out.println("bounds of the first rectangle in "+ p.getName() + " = " + firstRec);162System.out.println("bounds of the last rectangle in "+ p.getName() + " = " + lastRec);163164borderLeft = lastRec.getX();165borderRight = p.getWidth() - firstRec.getWidth() - firstRec.getX();166borderTop = lastRec.getY();167borderBottom = p.getHeight() - firstRec.getHeight() - firstRec.getY();168169return areBordersEqual(borderLeft, borderRight) &&170areBordersEqual(borderTop, borderBottom);171}172173/**174* Given two borders border1 and border2 check if they are equal.175*176* @param border1 one of the borders being compared177* @param border2 the other border being compared178* @return true if border1 and border2 are equal to each other (i.e.179* their width/height difference is at most 1, assuming the180* smallest pixel is of size 1), false otherwise181*/182public static boolean areBordersEqual(double border1, double border2) {183return Math.abs(border1 - border2) <= 1;184}185186public static void main(String[] args) {187new LayoutExtraGaps();188}189}190191192