Get Screen Center Point in Java

An example on getting screen center in Java in a single statement!

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);

}
}

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

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). Here is how to get screen size and resolution in Java

Also see AWT Tutorial in Java

No comments: