Java Reference
In-Depth Information
associated with them that define the areas within the window that they occupy. Point objects are used in the
definition of other geometric entities such as lines and circles, and to specify their position in a window.
Note that neither Point nor Rectangle objects have any built-in representation on the screen. They aren't
components; they are abstract geometric entities. If you want to display a rectangle you have to draw it. You
see how to do this in Chapter 19 when you read about other classes that define geometric shapes that can be
displayed.
Point Objects
As I said, the Point class defines a point by two public data members of type int , x and y , so you can
access them directly. Let's look at the methods that the class provides.
TRY IT OUT: Playing with Point Objects
Try the following code:
import java.awt.Point;
public class PlayingWithPoints {
public static void main(String[] args) {
Point aPoint = new Point();
// Initialize to 0,0
Point bPoint = new Point(50,25);
Point cPoint = new Point(bPoint);
System.out.println("aPoint is located at: " + aPoint);
aPoint.move(100,50);
// Change to position
100,50
bPoint.x = 110;
bPoint.y = 70;
aPoint.translate(10,20);
// Move by 10 in x and 20
in y
System.out.println("aPoint is now at: " + aPoint);
if(aPoint.equals(bPoint))
System.out.println("aPoint and bPoint are at the same
location.");
}
}
PlayingWithPoints.java
If you run the program, you should see the following output produced:
aPoint is located at: java.awt.Point[x=0,y=0]
aPoint is now at: java.awt.Point[x=110,y=70]
Search WWH ::




Custom Search