Java Tutorial/2D Graphics/Point

Материал из Java эксперт
Перейти к: навигация, поиск

java.awt.Point

  1. A Point object represents a point in a coordinate system.
  2. It has two int fields, x and y.
  3. You can construct a Point object by using one of its constructors.



public Point ()
public Point (int x, int y)
public Point (Point anotherPoint)



The Point class"s getX and getY methods return the value of the x and y fields in double, respectively.


Playing with Point Objects

import java.awt.Point;
public class MainClass {
  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.");
  }
}



aPoint is located at: java.awt.Point[x=0,y=0]
aPoint is now at: java.awt.Point[x=110,y=70]
aPoint and bPoint are at the same location.


Point class

import java.awt.Point;
class PointSetter {
  public static void main(String[] arguments) {
    Point location = new Point(4, 13);
    System.out.println("Starting location:");
    System.out.println("X equals " + location.x);
    System.out.println("Y equals " + location.y);
    System.out.println("\nMoving to (7, 6)");
    location.x = 7;
    location.y = 6;
    System.out.println("\nEnding location:");
    System.out.println("X equals " + location.x);
    System.out.println("Y equals " + location.y);
  }
}