001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.jorphan.gui;
020:
021: import java.awt.Component;
022: import java.awt.Dimension;
023:
024: /**
025: * This class is a Util for awt Component and could be used to place them in
026: * center of an other.
027: *
028: * @author <a href="mailto:bo.regnlin@pc.nu">Bo Regnlin</a>
029: * @version $Revision: 493784 $
030: */
031: public final class ComponentUtil {
032: /**
033: * Use this static method if you want to center and set its position
034: * compared to the size of the current users screen size. Valid percent is
035: * between +-(0-100) minus is treated as plus, bigger than 100 is always set
036: * to 100.
037: *
038: * @param component
039: * the component you want to center and set size on
040: * @param percentOfScreen
041: * the percent of the current screensize you want the component
042: * to be
043: */
044: public static void centerComponentInWindow(Component component,
045: int percentOfScreen) {
046: if (percentOfScreen < 0) {
047: centerComponentInWindow(component, -percentOfScreen);
048: return;
049: }
050: if (percentOfScreen > 100) {
051: centerComponentInWindow(component, 100);
052: return;
053: }
054: double percent = percentOfScreen / 100.d;
055: Dimension dimension = component.getToolkit().getScreenSize();
056: component.setSize((int) (dimension.getWidth() * percent),
057: (int) (dimension.getHeight() * percent));
058: centerComponentInWindow(component);
059: }
060:
061: /**
062: * Use this static method if you want to center a component in Window.
063: *
064: * @param component
065: * the component you want to center in window
066: */
067: public static void centerComponentInWindow(Component component) {
068: Dimension dimension = component.getToolkit().getScreenSize();
069:
070: component
071: .setLocation((int) ((dimension.getWidth() - component
072: .getWidth()) / 2), (int) ((dimension
073: .getHeight() - component.getHeight()) / 2));
074: component.validate();
075: component.repaint();
076: }
077:
078: /**
079: * Use this static method if you want to center a component over another
080: * component.
081: *
082: * @param parent
083: * the component you want to use to place it on
084: * @param toBeCentered
085: * the component you want to center
086: */
087: public static void centerComponentInComponent(Component parent,
088: Component toBeCentered) {
089: toBeCentered.setLocation(parent.getX()
090: + (parent.getWidth() - toBeCentered.getWidth()) / 2,
091: parent.getY()
092: + (parent.getHeight() - toBeCentered
093: .getHeight()) / 2);
094:
095: toBeCentered.validate();
096: toBeCentered.repaint();
097: }
098:
099: /**
100: * Private constructor to prevent instantiation.
101: */
102: private ComponentUtil() {
103: }
104: }
|