Spring Framework Get Screen Center Point in Java

An example on getting the middle point of a screen in Java. It is just a single statement far away.


Example




import java.awt.*;

class GetCenter

{

public static void main(String args[])

{



// Get the center point using getCenterPoint() in GraphicsEnvironment class

Point p=GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint();



// Print the x co-ordinate

System.out.println("The center x co-ordinate is "+p.x);



// Print the y co-ordinate

System.out.println("The center y co-ordinate is "+p.y);



}

}

Output

The center x co-ordinate is 512
The center y co-ordinate is 364

Explanation


As java.awt.GraphicsEnvironment is an abstract class, it's object here is created using the method getLocalGraphicsEnvironment() which is a public static method of the class itself and now the method which is just a normal method, the getCenterPoint() method returns the java.awt.Point object which contains the x and y co ordinates (the center of the screen).