KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jorphan > gui > ComponentUtil


1 // $Header: /home/cvs/jakarta-jmeter/src/jorphan/org/apache/jorphan/gui/ComponentUtil.java,v 1.5 2004/02/11 23:48:32 sebb Exp $
2
/*
3  * Copyright 2001-2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17 */

18
19 package org.apache.jorphan.gui;
20
21 import java.awt.Component JavaDoc;
22 import java.awt.Dimension JavaDoc;
23
24 /**
25  * This class is a Util for awt Component and could be used to place them in
26  * center of an other.
27  *
28  * @author <a HREF="mailto:bo.regnlin@pc.nu">Bo Regnlin</a>
29  * @version $Revision: 1.5 $
30  */

31 public final class ComponentUtil
32 {
33     /**
34      * Use this static method if you want to center and set its position
35      * compared to the size of the current users screen size. Valid percent
36      * is between +-(0-100) minus is treated as plus, bigger than 100 is always
37      * set to 100.
38      *
39      * @param component the component you want to center and set size on
40      * @param percentOfScreen the percent of the current screensize you want
41      * the component to be
42      */

43     public static void centerComponentInWindow(
44         Component JavaDoc component,
45         int percentOfScreen)
46     {
47         if (percentOfScreen < 0)
48         {
49             centerComponentInWindow(component, -percentOfScreen);
50             return;
51         }
52         if (percentOfScreen > 100)
53         {
54             centerComponentInWindow(component, 100);
55             return;
56         }
57         double percent = percentOfScreen / 100.d;
58         Dimension JavaDoc dimension = component.getToolkit().getScreenSize();
59         component.setSize(
60             (int) (dimension.getWidth() * percent),
61             (int) (dimension.getHeight() * percent));
62         centerComponentInWindow(component);
63     }
64
65     /**
66      * Use this static method if you want to center a component in Window.
67      *
68      *@param component the component you want to center in window
69      */

70     public static void centerComponentInWindow(Component JavaDoc component)
71     {
72         Dimension JavaDoc dimension = component.getToolkit().getScreenSize();
73
74         component.setLocation(
75             (int) ((dimension.getWidth() - component.getWidth()) / 2),
76             (int) ((dimension.getHeight() - component.getHeight()) / 2));
77         component.validate();
78         component.repaint();
79     }
80
81     /**
82      * Use this static method if you want to center a component over another
83      * component.
84      *
85      * @param parent the component you want to use to place it on
86      * @param toBeCentered the component you want to center
87      */

88     public static void centerComponentInComponent(
89         Component JavaDoc parent,
90         Component JavaDoc toBeCentered)
91     {
92         toBeCentered.setLocation(
93             (int) parent.getX()
94                 + (int) ((parent.getWidth() - toBeCentered.getWidth()) / 2),
95             (int) parent.getY()
96                 + (int) ((parent.getHeight() - toBeCentered.getHeight()) / 2));
97
98         toBeCentered.validate();
99         toBeCentered.repaint();
100     }
101
102     /**
103      * Private constructor to prevent instantiation.
104      */

105     private ComponentUtil()
106     {
107     }
108 }
109
Popular Tags