Saturday, September 17, 2011

Center Frame


import java.awt.*;

public class CenterFrame // a utility class
{
public static Point getPosition(int frameWidth, int frameHeight)
{
// frame size is width * height
// returns a Point holding the coordinates of
// the upper left-hand corner of the (centered) frame

Toolkit toolkit = Toolkit.getDefaultToolkit(); //a static method of the Toolkit class
Dimension dimensions =toolkit.getScreenSize(); //Get Dimension from the Screen Size
int x = (dimensions.width - frameWidth)/2; // x coordinate of upper left corner
int y = (dimensions.height - frameHeight)/2; // y coordinate of upper left corner
return (new Point(x,y)); // return coordinates as a Point object
}
}




No comments:

Post a Comment