Tutorials point in java

A Simple 2D Point Class in Java

Creating a simple 2D point class in Java is fairly simple. A Cartesian coordinate system uses points in two dimensions along an x-axis and y-axis. Each point has a pair of x and y coordinates along these two axes. We define a SimplePoint class that has two private properties x and y :

public class SimplePoint  private double x,y; > 
 public double getX()  return x; >; public double getY()  return y; >; 

We could also provide setter methods if desired. A constructor method makes it convenient to create a SimplePoint object at a specific x,y location:

 public SimplePoint(double x1, double y1)  x = x1; y = y1; > 
 SimplePoint p1 = new SimplePoint(2,1); SimplePoint p2 = new SimplePoint(8,2); 
 public double distance(SimplePoint p)  return Math.sqrt( Math.pow(p.getX()-x,2) + Math.pow(p.getY()-y,2) ); > 
 System.out.println( p1.distance(p2) ); // 6.082762530298219 
public class SimplePoint  private double x,y; public SimplePoint(double x1, double y1)  x = x1; y = y1; > public double getX()  return x; >; public double getY()  return y; >; public double distance(SimplePoint p)  return Math.sqrt( Math.pow(p.getX()-x,2) + Math.pow(p.getY()-y,2) ); > public static void main(String[] args)  SimplePoint p1 = new SimplePoint(2,1); SimplePoint p2 = new SimplePoint(8,2); System.out.println( p1.distance(p2) ); // 6.082762530298219 > > 

Java makes it easy to represent 2D points. The SimplePoint class is one way to do that. Create SimplePoint objects at specified locations, then use the distance() function to calculate the distances between them. Thanks for reading. Follow me on Twitter @realEdwinTorres for more programming tips. 😀

Источник

Point class in java

The java.awt.Point class is a part of the Java Abstract Window Toolkit (AWT) package, which provides a set of APIs for creating and managing graphical user interfaces (GUIs) in Java. The Point class represents a location in a two-dimensional coordinate system using integer precision.

The Point class has two public fields, x and y, which represent the horizontal and vertical coordinates, respectively. The class provides several methods for manipulating these fields, such as getLocation() and setLocation(), which return or set the coordinates of the Point object, respectively.

In addition to these basic methods, the Point class also provides several convenience methods for performing common operations on points, such as distance() and move(). The distance() method calculates the distance between two points, while the move() method moves the point to a new location based on a specified offset.

The Point class is often used in conjunction with other AWT classes to create graphical user interfaces. For example, it is commonly used to specify the location of a component within a container, or to define the start and end points of a line or shape.

Here are all the methods of the java.awt.Point class:

  • Point() — Constructs a new Point object at (0,0).
  • Point(int x, int y) — Constructs a new Point object with the specified coordinates.
  • Point(Point p) — Constructs a new Point object that is a copy of the specified Point.
  • double distance(double px, double py) — Calculates the distance between this Point and the specified coordinates.
  • double distance(Point pt) — Calculates the distance between this Point and the specified Point.
  • double distanceSq(double px, double py) — Calculates the square of the distance between this Point and the specified coordinates.
  • double distanceSq(Point pt) — Calculates the square of the distance between this Point and the specified Point.
  • boolean equals(Object obj) — Returns true if the specified object is a Point with the same coordinates as this Point, and false otherwise.
  • int hashCode() — Returns a hash code value for this Point.
  • int x — The X coordinate of the Point.
  • int y — The Y coordinate of the Point.
  • void move(int x, int y) — Moves this Point to the specified coordinates.
  • void setLocation(int x, int y) — Sets the location of this Point to the specified coordinates.
  • void setLocation(double x, double y) — Sets the location of this Point to the specified coordinates.
  • void setLocation(Point p) — Sets the location of this Point to the same coordinates as the specified Point.
  • void setLocation(double x, double y) — Sets the location of this Point to the specified coordinates.
  • Point getLocation() — Returns a new Point object with the same coordinates as this Point.
  • void translate(int dx, int dy) — Translates this Point by the specified amount.
  • String toString() — Returns a string representation of this Point.

Declaration of this class as follows.

public class Point extends java.awt.geom.Point2D implements java.io.Serializable

This reference is belong to javadoc

Public Constructors

There is two types of public constructor is defined inside this class there syntax as follows.

public java.awt.Point(java.awt.Point) public java.awt.Point(int,int) public java.awt.Point() 

Example of those constructor.

import java.awt.Point; public class Example < public static void main(String[] arg) < // Create instance Point p1 = new Point(); Point p2 = new Point(-3, 6); //Display point instance value System.out.println(p1); System.out.println(p2); >>

Instance of point class in java

java.awt.Point[x=0,y=0] java.awt.Point[x=-3,y=6]

java.awt.Point equals(Object) method example

// equals() method in java.awt.Point class import java.awt.Point; public class Example < public static void main(String[] arg) < // Create instance Point p1 = new Point(-3, 6); Point p2 = new Point(-3, 6); Point p3 = new Point(1, 6); // Test equals method System.out.println(p1.equals(p2)); System.out.println(p2.equals(p3)); System.out.println(p3.equals(p3)); >>

java.awt.Point getLocation() method example

// getLocation() method in java.awt.Point class import java.awt.Point; public class Example < public static void main(String[] arg) < // Create instance Point p1 = new Point(1, 2); // getLocation Point p2 = p1.getLocation(); // Display value System.out.println(p1); System.out.println(p2); >>

getLocation() method in Point class in java

java.awt.Point[x=1,y=2] java.awt.Point[x=1,y=2]

java.awt.Point getX() method example

// getX() method in java.awt.Point class import java.awt.Point; public class Example < public static void main(String[] arg) < // Create instance Point p1 = new Point(4, 2); Point p2 = new Point(10, 20); // Display x instance value System.out.println(p1.getX()); System.out.println(p2.getX()); >>

java.awt.Point getY() method example

// getY() method in java.awt.Point class import java.awt.Point; public class Example < public static void main(String[] arg) < // Create instance Point p1 = new Point(4, 2); Point p2 = new Point(10, 20); // Display y instance value System.out.println(p1.getY()); System.out.println(p2.getY()); >>

java.awt.Point move(int, int) method example

// move(int,int) method in java.awt.Point class import java.awt.Point; public class Example < public static void main(String[] arg) < // Create instance Point p1 = new Point(4, 2); Point p2 = new Point(10, 20); System.out.println("Before Move"); System.out.println(p1); System.out.println(p2); p1.move(3, 45); p2.move(6, 53); System.out.println("After Move"); System.out.println(p1); System.out.println(p2); >>
Before Move java.awt.Point[x=4,y=2] java.awt.Point[x=10,y=20] After Move java.awt.Point[x=3,y=45] java.awt.Point[x=6,y=53]

java.awt.Point setLocation(int, int) method example

// setLocation(int,int) method in java.awt.Point class import java.awt.Point; public class Example < public static void main(String[] arg) < // Create instance Point p1 = new Point(4, 2); // getLocation method Point p2 = p1.getLocation(); // setLocation method p1.setLocation(10, 20); System.out.println(p1); System.out.println(p2); >>
java.awt.Point[x=10,y=20] java.awt.Point[x=4,y=2]

java.awt.Point setLocation(double, double) method example

// setLocation(double,double) method in java.awt.Point class import java.awt.Point; public class Example < public static void main(String[] arg) < // Create instance Point p1 = new Point(4, 2); Point p2 = new Point(6, 8); // setLocation method p1.setLocation(10.8, 20.4); p2.setLocation(5.3, 6.7); System.out.println(p1); System.out.println(p2); >>
java.awt.Point[x=11,y=20] java.awt.Point[x=5,y=7]

java.awt.Point setLocation(Point) method example

// setLocation(Point) method in java.awt.Point class import java.awt.Point; public class Example < public static void main(String[] arg) < // Create instance Point p1 = new Point(4, 2); Point p2 = new Point(10, 20); System.out.println("Before setLocation"); System.out.println(p1); System.out.println(p2); // setLocation method p2.setLocation(p1); System.out.println("After setLocation"); System.out.println(p1); System.out.println(p2); >>
Before setLocation java.awt.Point[x=4,y=2] java.awt.Point[x=10,y=20] After setLocation java.awt.Point[x=4,y=2] java.awt.Point[x=4,y=2]

java.awt.Point toString() method example

// toString() method in java.awt.Point class import java.awt.Point; public class Example < public static void main(String[] arg) < // Create instance Point p1 = new Point(4, 2); Point p2 = new Point(6, 8); System.out.println(p1.toString()); System.out.println(p2.toString()); >>
java.awt.Point[x=4,y=2] java.awt.Point[x=6,y=8]

java.awt.Point translate(int, int) method example

// translate(int, int) method in java.awt.Point class import java.awt.Point; public class Example < public static void main(String[] arg) < // Create instance Point p1 = new Point(4, 2); Point p2 = new Point(6, 8); System.out.println("Before translate"); System.out.println(p1); System.out.println(p2); p1.translate(13, 66); // 4+13, 2+66 p2.translate(3, 5); // 6+3, 8+5 System.out.println("After translate"); System.out.println(p1); System.out.println(p2); >>
Before translate java.awt.Point[x=4,y=2] java.awt.Point[x=6,y=8] After translate java.awt.Point[x=17,y=68] java.awt.Point[x=9,y=13]

Источник

Class Point

Nested classes/interfaces declared in class java.awt.geom.Point2D

Field Summary

Constructor Summary

Method Summary

Translates this point, at location (x,y) , by dx along the x axis and dy along the y axis so that it now represents the point (x+dx,y+dy) .

Methods declared in class java.awt.geom.Point2D

Methods declared in class java.lang.Object

Field Details

x

y

Constructor Details

Point

Point

Point

Method Details

getX

getY

getLocation

Returns the location of this point. This method is included for completeness, to parallel the getLocation method of Component .

setLocation

Sets the location of the point to the specified location. This method is included for completeness, to parallel the setLocation method of Component .

setLocation

Changes the point to have the specified location. This method is included for completeness, to parallel the setLocation method of Component . Its behavior is identical with move(int, int) .

setLocation

Sets the location of this point to the specified double coordinates. The double values will be rounded to integer values. Any number smaller than Integer.MIN_VALUE will be reset to MIN_VALUE , and any number larger than Integer.MAX_VALUE will be reset to MAX_VALUE .

move

Moves this point to the specified location in the (x,y) coordinate plane. This method is identical with setLocation(int, int) .

translate

Translates this point, at location (x,y) , by dx along the x axis and dy along the y axis so that it now represents the point (x+dx,y+dy) .

equals

Determines whether or not two points are equal. Two instances of Point2D are equal if the values of their x and y member fields, representing their position in the coordinate space, are the same.

toString

Returns a string representation of this point and its location in the (x,y) coordinate space. This method is intended to be used only for debugging purposes, and the content and format of the returned string may vary between implementations. The returned string may be empty but may not be null .

Report a bug or suggest an enhancement
For further API reference and developer documentation see the Java SE Documentation, which contains more detailed, developer-targeted descriptions with conceptual overviews, definitions of terms, workarounds, and working code examples. Other versions.
Java is a trademark or registered trademark of Oracle and/or its affiliates in the US and other countries.
Copyright © 1993, 2023, Oracle and/or its affiliates, 500 Oracle Parkway, Redwood Shores, CA 94065 USA.
All rights reserved. Use is subject to license terms and the documentation redistribution policy.

Источник

Читайте также:  Sign in - Google Accounts
Оцените статью