Java examples for 2D Graphics:Screen
creates and returns Screen bounds.
//package com.java2s; import static java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment; import java.awt.Rectangle; public class Main { public static void main(String[] argv) throws Exception { int width = 2; int height = 2; System.out.println(createCenteredScreenBounds(width, height)); }//from w w w . jav a 2s. com /** * This method creates and returns bounds. * * @param width The width of the bounds. * @param height The height of the bounds. * * @return Bounds. */ public static Rectangle createCenteredScreenBounds(int width, int height) { return createCenteredBounds(width, height, getScreenBounds()); } /** * This method creates and returns bounds. * * @param width The width of the bounds. * @param height The height of the bounds. * @param area The area to center the bounds to. * * @return Bounds. */ public static Rectangle createCenteredBounds(int width, int height, Rectangle area) { Rectangle rectangle = new Rectangle(0, 0, width, height); int x = area.x + (area.width / 2); int y = area.y + (area.height / 2); rectangle.setLocation(x - (width / 2), y - (height / 2)); return rectangle; } /** * This method returns the bounds for the current screen resolution. * * @return Bounds. */ public static Rectangle getScreenBounds() { return getLocalGraphicsEnvironment().getMaximumWindowBounds(); } }