Point class represents a location in a two-dimensional space.
A location in a two-dimensional space is represented by two values: an x coordinate and a y coordinate.
The Point class is in the java.awt package.
The following code demonstrates its use:
// Create a Point Point p = new Point(20, 40); // Get the x and y coordinate of p int x = p.getX(); int y = p.getY(); // Set the x and y coordinate of p to (10, 60) p.setLocation(10, 60);
The following code shows how to use Point class to set and get the location (x and y coordinates) of a component.
The following two statements do the same thing.
JButton closeButton = new JButton("Close"); closeButton.setLocation(10, 15); closeButton.setLocation(new Point(10, 15));
To get the location of the button
Point p = closeButton.getLocation();
Dimension class wraps the width and height of a component. Dimension class from java.awt package is used to represent the size of a component.
The following code creates Dimension class with a width and height.
Dimension d = new Dimension(200, 20);
Then we set the size of closeButton to 200 by 20.
We can alo use the following code to set the size.
To get the size of closeButton
Dimension d2 = closeButton.getSize(); int width = d2.width; int height = d2.height;
Insets class from the java.awt package represents spaces around a container. It wraps four properties named top, left, bottom, and right, whose values represent the spaces left on the four side of a container.
The following code creates an object of the Insets class using its constructor Insets(top, left, bottom, right).
Insets ins = new Insets(20, 5, 5, 5);
Then we can get the insets of a JFrame
Insets ins = frame.getInsets(); int top = ins.top; int left = ins.left; int bottom = ins.bottom; int right = ins.right;
Rectangle class from the java.awt package represents a rectangle shape. A Rectangle is defined by three properties:
A Rectangle object combines a Point object and a Dimension object. The Point object holds the (x, y) coordinates of the upper left corner of the rectangle and the Dimension object holds the width and height.
We can create an object of the Rectangle class by specifying different combinations of its properties.
To create a Rectangle object whose upper-left corner is at (0, 0) with width and height as zero.
Rectangle r1 = new Rectangle();
To create a Rectangle object from a Point object with its width and height as zero.
Rectangle r2 = new Rectangle(new Point(10, 10));
To create a Rectangle object from a Point object and a Dimension object
Rectangle r3 = new Rectangle(new Point(10, 10), new Dimension(200, 100));
To create a Rectangle object by specifying its upper-left corner's coordinate at (10, 10) and width as 200 and height as 100
Rectangle r4 = new Rectangle(10, 10, 200, 100);