Java Screen Center setCenteredInScreen(Component frame)

Here you can find the source of setCenteredInScreen(Component frame)

Description

set Centered In Screen

License

Open Source License

Declaration

public static void setCenteredInScreen(Component frame) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.awt.Component;

import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;

import java.awt.Rectangle;

public class Main {
    public static void setCenteredInScreen(Component frame) {
        setCenteredInScreen(frame, -1);//from  w w  w  .  j a v a 2 s.  c om
    }

    /**
     * 
     * @param frame
     * @param scale
     *            0.0 (exclusive) to 1.0 (inclusive, full-screen); if negative,
     *            use existing geometry of component
     */
    public static void setCenteredInScreen(Component frame, double scale) {
        GraphicsEnvironment ge = GraphicsEnvironment
                .getLocalGraphicsEnvironment();
        GraphicsDevice gd = ge.getDefaultScreenDevice();
        GraphicsConfiguration gc = gd.getDefaultConfiguration();
        Rectangle bounds = gc.getBounds();

        int width = new Double(scale * bounds.getWidth()).intValue();
        int height = new Double(scale * bounds.getHeight()).intValue();

        if (scale < 0) {
            width = frame.getWidth();
            height = frame.getHeight();
        }

        int x = new Double((bounds.getWidth() - width) / 2).intValue();
        int y = new Double((bounds.getHeight() - height) / 2).intValue();

        frame.setSize(width, height);
        frame.setLocation(x, y);
    }
}

Related

  1. setCenter(Component comp)
  2. setCenter(Component comp, Component parent)
  3. setCenter(Component component)
  4. setCenter(Component component, Component component1)
  5. setCenteredCoordSystem(Graphics2D g, Component comp, double unitx, double unity)
  6. setCenteredRectangle(int rectangleWidth, int rectangleHeight)
  7. setCenterLocation(final Window window)
  8. setCenterPosition(final Frame frame)
  9. setComponentLocationOnCenter(Component component)