Here you can find the source of getDimensionFromPercent(int percentWidth, int percentHeight)
Dimension
whose size is defined not in terms of pixels, but in terms of a given percent of the screen's width and height.
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>. |
public static final Dimension getDimensionFromPercent(int percentWidth, int percentHeight)
//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); } }