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);
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
|
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 @author Andrei Dmitriev : area=awt.layout
31 @run main LayoutExtraGaps
32 */
33
34
35 import java.awt.Frame;
36 import java.awt.Panel;
37 import java.awt.GridLayout;
38 import java.awt.Color;
39 import java.awt.Label;
40 import java.awt.Component;
41 import java.awt.ComponentOrientation;
42 import java.awt.Rectangle;
43
44 public class LayoutExtraGaps extends Frame {
45 final static int compCount = 30;
46
47 public LayoutExtraGaps() {
48 super("GridLayoutTest");
49 Panel yellowPanel = new Panel(new GridLayout(compCount, 1, 3, 3));
50 yellowPanel.setBackground(Color.yellow);
51
52 for(int i = 0; i < compCount ; i++) {
53 Label redLabel = new Label(""+i);
54 redLabel.setBackground(Color.red);
55 yellowPanel.add(redLabel);
56 }
57
58 Panel bluePanel = new Panel(new GridLayout(1, compCount, 3, 3));
59 bluePanel.setBackground(Color.blue);
60
61 for(int i = 0; i < compCount; i++) {
62 Label greenLabel = new Label(""+i);
78 Panel redPanel = new Panel(new GridLayout(1, compCount, 3, 3));
79 redPanel.applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
80 redPanel.setBackground(Color.red);
81
82 for(int i = 0; i < compCount; i++) {
83 Label greenLabel = new Label(""+i);
84 greenLabel.setBackground(Color.green);
85 redPanel.add(greenLabel);
86 }
87
88 setLayout(new GridLayout(2, 2, 20, 20));
89
90 add(yellowPanel);
91 add(bluePanel);
92 add(redPanel);
93 add(blackPanel);
94 pack();
95 setSize((int)getPreferredSize().getWidth() + 50, (int)getPreferredSize().getHeight() + 50);
96 setVisible(true);
97
98 blockTillDisplayed();
99
100 if (isComponentCentredLTR(yellowPanel) && isComponentCentredLTR(bluePanel)
101 && isComponentCentredLTR(blackPanel) && isComponentCentredRTL(redPanel))
102 {
103 System.out.println("Test passed.");
104 } else {
105 throw new RuntimeException("Test failed. GridLayout doesn't center component.");
106 }
107
108 setVisible(false);
109 dispose();
110 }
111
112 void blockTillDisplayed() {
113 while (isVisible() == false) {
114 try {
115 Thread.sleep(100);
116 } catch (InterruptedException e) {
117 e.printStackTrace();
118 setVisible(false);
119 dispose();
120 throw new RuntimeException("Test failed.");
121 }
122 }
123 }
124 /**
125 * Checks if the components under Panel p are properly centred (i.e.
126 * opposite borders between the Panel and component are equal). Panel p
127 * must not be affect by RTL orientation (RTL only affects horizontal
128 * positioning and components lay out from top right corner).
129 *
130 * @param p the panel where the components exist and is not affected
131 * by right to left orientation
132 * @return true if components of panel p are properly centre, false
133 * otherwise
134 */
135 public static boolean isComponentCentredLTR(Panel p) {
136 double borderLeft;
137 double borderRight;
138 double borderTop;
139 double borderBottom;
140
141 //The first component(rectangle) in panel p.
142 Rectangle firstRec = p.getComponent(0).getBounds();
143
|