The Point Class - Java 2D Graphics

Java examples for 2D Graphics:Point

Introduction

Point class represents a location in a two-dimensional space.

Point class is represented by two values: an x coordinate and a y coordinate.

The Point class is in the java.awt package.

The following snippet of code demonstrates its use:

Demo Code

import java.awt.Point;

public class Main {
  public static void main(String[] argv) throws Exception {
    // Create an object of the Point class with (x, y) coordinate of (20, 40)
    Point p = new Point(20, 40);

    // Get the x and y coordinate of p
    double x = p.getX();
    double y = p.getY();

    // Set the x and y coordinate of p to (10, 60)
    p.setLocation(10, 60);//from ww  w. j  ava  2 s.  c  o  m
  }
}

For example, you can set the location of a JButton.

Demo Code

import java.awt.Point;

import javax.swing.JButton;

public class Main {
  public static void main(String[] argv) throws Exception {
    Point p = new Point(20, 40);
    p.setLocation(10, 60);/*from w ww .  ja v  a2s. c  om*/

    JButton closeButton = new JButton("Close");

    // The following two statements do the same thing.
    closeButton.setLocation(10, 15);
    closeButton.setLocation(new Point(10, 15));

    // Get the location of the closeButton
    p = closeButton.getLocation();

  }
}

Related Tutorials