Here you can find the source of getCenteringPoint(final Dimension frameDimension)
Parameter | Description |
---|---|
frameDimension | frame size |
Parameter | Description |
---|---|
NullPointerException | on some platform (virtual box) |
public static Point getCenteringPoint(final Dimension frameDimension) throws NullPointerException
//package com.java2s; import java.awt.Dimension; import java.awt.DisplayMode; import java.awt.GraphicsDevice; import java.awt.GraphicsEnvironment; import java.awt.Point; public class Main { /** Screen width */ private static int _screenWidth = 0; /** Screen height */ private static int _screenHeight = 0; /**/*from w ww. jav a2 s . c o m*/ * Returns the centered point in order to center a frame on the screen * @param frameDimension frame size * @return centered point * @throws NullPointerException on some platform (virtual box) */ public static Point getCenteringPoint(final Dimension frameDimension) throws NullPointerException { getScreenProperties(); int x = (_screenWidth - frameDimension.width) / 2; x = Math.max(x, 0); int y = (_screenHeight - frameDimension.height) / 2; y = Math.max(y, 0); return new Point(x, y); } /** * Get screen properties * @throws NullPointerException on some platform (virtual box) */ public static void getScreenProperties() throws NullPointerException { // Get main screen size GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsDevice gs = ge.getDefaultScreenDevice(); DisplayMode dm = gs.getDisplayMode(); _screenWidth = dm.getWidth(); _screenHeight = dm.getHeight(); } }