Java Screen Center getDimensionFromPercent(int percentWidth, int percentHeight)

Here you can find the source of getDimensionFromPercent(int percentWidth, int percentHeight)

Description

Return a Dimension whose size is defined not in terms of pixels, but in terms of a given percent of the screen's width and height.

License

Apache License

Parameter

Parameter Description
percentWidth percentage width of the screen, in range <code>1..100</code>.
percentHeight percentage height of the screen, in range <code>1..100</code>.

Declaration

public static final Dimension getDimensionFromPercent(int percentWidth, int percentHeight) 

Method Source Code

//package com.java2s;
/*//  ww  w.  j av a 2  s  . c o  m
 * Copyright 2002-2004 the original author or authors.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

import java.awt.Dimension;

import java.awt.Toolkit;

public class Main {
    /**
     * Return a <code>Dimension</code> whose size is defined not in terms of
     * pixels, but in terms of a given percent of the screen's width and height.
     * 
     * <P>
     * Use to set the preferred size of a component to a certain percentage of
     * the screen.
     * 
     * @param percentWidth percentage width of the screen, in range
     * <code>1..100</code>.
     * @param percentHeight percentage height of the screen, in range
     * <code>1..100</code>.
     */
    public static final Dimension getDimensionFromPercent(int percentWidth, int percentHeight) {
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        return calcDimensionFromPercent(screenSize, percentWidth, percentHeight);
    }

    /**
     * Return the system screen size.
     * 
     * @return The dimension of the system screen size.
     */
    public static Dimension getScreenSize() {
        return Toolkit.getDefaultToolkit().getScreenSize();
    }

    private static Dimension calcDimensionFromPercent(Dimension dimension, int percentWidth, int percentHeight) {
        int width = dimension.width * percentWidth / 100;
        int height = dimension.height * percentHeight / 100;
        return new Dimension(width, height);
    }
}

Related

  1. getCenterPoint(Component parent, Component comp)
  2. getCenterPoint(final int w, final int h)
  3. getCenterPointRelativeToScreen(Dimension size)
  4. getCenterPosition(Dimension d)
  5. getCenterPositionForComponent(int width, int heigth)
  6. getFrameCenteredLocation(final java.awt.Component centerThis, final Component parent)
  7. getFrameOnCenterLocationPoint(Window window)
  8. getLocationToCenterOnScreen(Component comp)
  9. getPercentageOnScreen(Point location, Dimension size, Rectangle screen)