/*
* Copyright (c) 2007, 2017 Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package org.jemmy;
import java.io.Serializable;
/**
* Replacement for java.awt.Point
* @author mrkam
*/
public class Point implements Serializable{
/**
* The X coordinate of this Point
.
* If no X coordinate is set it will default to 0.
*
* @serial
* @see #getLocation()
* @see #move(int, int)
*/
public int x;
/**
* The Y coordinate of this Point
.
* If no Y coordinate is set it will default to 0.
*
* @serial
* @see #getLocation()
* @see #move(int, int)
*/
public int y;
/*
* JDK 1.1 serialVersionUID
*/
private static final long serialVersionUID = -5276940640259749850L;
/**
* Constructs and initializes a point at the origin
* (0, 0) of the coordinate space.
*/
public Point() {
this(0, 0);
}
/**
* Constructs and initializes a point with the same location as
* the specified Point
object.
* @param p a point
*/
public Point(Point p) {
this(p.x, p.y);
}
/**
* Constructs and initializes a point at the specified
* {@code (x,y)} location in the coordinate space.
* @param x the X coordinate of the newly constructed Point
* @param y the Y coordinate of the newly constructed Point
*/
public Point(int x, int y) {
this.x = x;
this.y = y;
}
/**
* Constructs and initializes a point at the specified
* {@code (x,y)} location in the coordinate space. All {@code double}
* values are rounded and stored as {@code int} values.
* @param x the X coordinate of the newly constructed Point
* @param y the Y coordinate of the newly constructed Point
*/
public Point(double x, double y) {
this.x = (int) Math.round(x);
this.y = (int) Math.round(y);
}
/**
* {@inheritDoc}
*/
public int getX() {
return x;
}
/**
* {@inheritDoc}
*/
public int getY() {
return y;
}
/**
* Returns the location of this point.
* @return a copy of this point, at the same location
* @see org.jemmy.Point#setLocation(org.jemmy.Point)
* @see org.jemmy.Point#setLocation(int, int)
*/
public Point getLocation() {
return new Point(x, y);
}
/**
* Sets the location of the point to the specified location.
* @param p a point, the new location for this point
* @return self
* @see org.jemmy.Point#getLocation
*/
public Point setLocation(Point p) {
setLocation(p.x, p.y);
return this;
}
/**
* Changes the point to have the specified location.
*
* Its behavior is identical with move(int, int)
.
* @param x the X coordinate of the new location
* @param y the Y coordinate of the new location
* @return self
* @see org.jemmy.Point#getLocation
* @see org.jemmy.Point#move(int, int)
*/
public Point setLocation(int x, int y) {
move(x, y);
return this;
}
/**
* 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
.
*
* @param x the X coordinate of the new location
* @param y the Y coordinate of the new location
* @return self
* @see #getLocation
*/
public Point setLocation(double x, double y) {
this.x = (int) Math.round(x);
this.y = (int) Math.round(y);
return this;
}
/**
* Moves this point to the specified location in the
* {@code (x,y)} coordinate plane. This method
* is identical with setLocation(int, int)
.
* @param x the X coordinate of the new location
* @param y the Y coordinate of the new location
* @return self
*/
public Point move(int x, int y) {
this.x = x;
this.y = y;
return this;
}
/**
* Translates this point, at location {@code (x,y)},
* by {@code dx} along the {@code x} axis and {@code dy}
* along the {@code y} axis so that it now represents the point
* {@code (x + dx,y + dy)}.
*
* @param dx the distance to move this point
* along the X axis
* @param dy the distance to move this point
* along the Y axis
* @return self
*/
public Point translate(int dx, int dy) {
this.x += dx;
this.y += dy;
return this;
}
/**
* Translates the current point according to the given vector.
*
* @param v vector by which to translate the point
* @return self
*/
public Point translate(Vector v) {
this.x = (int) Math.round(x + v.getX());
this.y = (int) Math.round(y + v.getY());
return this;
}
/**
* Determines whether or not two points are equal. Two instances of
* Point
are equal if the values of their
* x
and y
member fields, representing
* their position in the coordinate space, are the same.
* @param obj an object to be compared with this Point
* @return true
if the object to be compared is
* an instance of Point
and has
* the same values; false
otherwise.
*/
@Override
public boolean equals(Object obj) {
if (obj instanceof Point) {
Point pt = (Point)obj;
return (x == pt.x) && (y == pt.y);
}
return super.equals(obj);
}
/**
* {@inheritDoc}
*/
@Override
public int hashCode() {
int hash = 7;
hash = 89 * hash + this.x;
hash = 89 * hash + this.y;
return hash;
}
/**
* Returns a string representation of this point and its location
* in the {@code (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
.
*
* @return a string representation of this point
*/
@Override
public String toString() {
return getClass().getName() + "[x=" + x + ",y=" + y + "]";
}
}