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