Java Screen Center getCenteringPoint(final Dimension frameDimension)

Here you can find the source of getCenteringPoint(final Dimension frameDimension)

Description

Returns the centered point in order to center a frame on the screen

License

Open Source License

Parameter

Parameter Description
frameDimension frame size

Exception

Parameter Description
NullPointerException on some platform (virtual box)

Return

centered point

Declaration

public static Point getCenteringPoint(final Dimension frameDimension) throws NullPointerException 

Method Source Code


//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();
    }
}

Related

  1. getCenterCoordinates(Dimension d)
  2. getCenteredLocation(Component comp)
  3. getCenteredLocation(Window w)
  4. getCenteredLocation(Window window)
  5. getCenteringPoint(Dimension size)
  6. getCenteringPointOnScreen(Dimension dimension)
  7. getCenterLocation(final Window frame)
  8. getCenterLocation(Window window)
  9. getCenterLocationAWT( final java.awt.Component comp, final boolean onTitleBarIfWindow)