1 /* 2 * Copyright (c) 2007, 2017 Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 package org.jemmy; 25 26 27 import java.io.Serializable; 28 29 30 /** 31 * Replacement for java.awt.Point 32 * @author Alexander Kouznetsov <mrkam@mail.ru> 33 */ 34 public class Point implements Serializable{ 35 /** 36 * The X coordinate of this <code>Point</code>. 37 * If no X coordinate is set it will default to 0. 38 * 39 * @serial 40 * @see #getLocation() 41 * @see #move(int, int) 42 */ 43 public int x; 44 45 /** 46 * The Y coordinate of this <code>Point</code>. 47 * If no Y coordinate is set it will default to 0. 48 * 49 * @serial 50 * @see #getLocation() 51 * @see #move(int, int) 52 */ 53 public int y; 54 55 /* 56 * JDK 1.1 serialVersionUID 57 */ 58 private static final long serialVersionUID = -5276940640259749850L; 59 60 /** 61 * Constructs and initializes a point at the origin 62 * (0, 0) of the coordinate space. 63 */ 64 public Point() { 65 this(0, 0); 66 } 67 68 /** 69 * Constructs and initializes a point with the same location as 70 * the specified <code>Point</code> object. 71 * @param p a point 72 */ 73 public Point(Point p) { 74 this(p.x, p.y); 75 } 76 77 /** 78 * Constructs and initializes a point at the specified 79 * {@code (x,y)} location in the coordinate space. 80 * @param x the X coordinate of the newly constructed <code>Point</code> 81 * @param y the Y coordinate of the newly constructed <code>Point</code> 82 */ 83 public Point(int x, int y) { 84 this.x = x; 85 this.y = y; 86 } 87 88 /** 89 * Constructs and initializes a point at the specified 90 * {@code (x,y)} location in the coordinate space. All {@code double} 91 * values are rounded and stored as {@code int} values. 92 * @param x the X coordinate of the newly constructed <code>Point</code> 93 * @param y the Y coordinate of the newly constructed <code>Point</code> 94 */ 95 public Point(double x, double y) { 96 this.x = (int) Math.round(x); 97 this.y = (int) Math.round(y); 98 } 99 100 /** 101 * {@inheritDoc} 102 * @return 103 */ 104 public int getX() { 105 return x; 106 } 107 108 /** 109 * {@inheritDoc} 110 * @return 111 */ 112 public int getY() { 113 return y; 114 } 115 116 /** 117 * Returns the location of this point. 118 * @return a copy of this point, at the same location 119 * @see org.jemmy.Point#setLocation(org.jemmy.Point) 120 * @see org.jemmy.Point#setLocation(int, int) 121 */ 122 public Point getLocation() { 123 return new Point(x, y); 124 } 125 126 /** 127 * Sets the location of the point to the specified location. 128 * @param p a point, the new location for this point 129 * @return 130 * @see org.jemmy.Point#getLocation 131 */ 132 public Point setLocation(Point p) { 133 setLocation(p.x, p.y); 134 return this; 135 } 136 137 /** 138 * Changes the point to have the specified location. 139 * <p> 140 * Its behavior is identical with <code>move(int, int)</code>. 141 * @param x the X coordinate of the new location 142 * @param y the Y coordinate of the new location 143 * @return self 144 * @see org.jemmy.Point#getLocation 145 * @see org.jemmy.Point#move(int, int) 146 */ 147 public Point setLocation(int x, int y) { 148 move(x, y); 149 return this; 150 } 151 152 /** 153 * Sets the location of this point to the specified double coordinates. 154 * The double values will be rounded to integer values. 155 * Any number smaller than <code>Integer.MIN_VALUE</code> 156 * will be reset to <code>MIN_VALUE</code>, and any number 157 * larger than <code>Integer.MAX_VALUE</code> will be 158 * reset to <code>MAX_VALUE</code>. 159 * 160 * @param x the X coordinate of the new location 161 * @param y the Y coordinate of the new location 162 * @return self 163 * @see #getLocation 164 */ 165 public Point setLocation(double x, double y) { 166 this.x = (int) Math.round(x); 167 this.y = (int) Math.round(y); 168 return this; 169 } 170 171 /** 172 * Moves this point to the specified location in the 173 * {@code (x,y)} coordinate plane. This method 174 * is identical with <code>setLocation(int, int)</code>. 175 * @param x the X coordinate of the new location 176 * @param y the Y coordinate of the new location 177 * @return self 178 */ 179 public Point move(int x, int y) { 180 this.x = x; 181 this.y = y; 182 return this; 183 } 184 185 /** 186 * Translates this point, at location {@code (x,y)}, 187 * by {@code dx} along the {@code x} axis and {@code dy} 188 * along the {@code y} axis so that it now represents the point 189 * {@code (x + dx,y + dy)}. 190 * 191 * @param dx the distance to move this point 192 * along the X axis 193 * @param dy the distance to move this point 194 * along the Y axis 195 * @return self 196 */ 197 public Point translate(int dx, int dy) { 198 this.x += dx; 199 this.y += dy; 200 return this; 201 } 202 203 /** 204 * 205 * @param v 206 * @return self 207 */ 208 public Point translate(Vector v) { 209 this.x = (int) Math.round(x + v.getX()); 210 this.y = (int) Math.round(y + v.getY()); 211 return this; 212 } 213 /** 214 * Determines whether or not two points are equal. Two instances of 215 * <code>Point</code> are equal if the values of their 216 * <code>x</code> and <code>y</code> member fields, representing 217 * their position in the coordinate space, are the same. 218 * @param obj an object to be compared with this <code>Point</code> 219 * @return <code>true</code> if the object to be compared is 220 * an instance of <code>Point</code> and has 221 * the same values; <code>false</code> otherwise. 222 */ 223 @Override 224 public boolean equals(Object obj) { 225 if (obj instanceof Point) { 226 Point pt = (Point)obj; 227 return (x == pt.x) && (y == pt.y); 228 } 229 return super.equals(obj); 230 } 231 232 /** 233 * {@inheritDoc} 234 * @return 235 */ 236 @Override 237 public int hashCode() { 238 int hash = 7; 239 hash = 89 * hash + this.x; 240 hash = 89 * hash + this.y; 241 return hash; 242 } 243 244 /** 245 * Returns a string representation of this point and its location 246 * in the {@code (x,y)} coordinate space. This method is 247 * intended to be used only for debugging purposes, and the content 248 * and format of the returned string may vary between implementations. 249 * The returned string may be empty but may not be <code>null</code>. 250 * 251 * @return a string representation of this point 252 */ 253 @Override 254 public String toString() { 255 return getClass().getName() + "[x=" + x + ",y=" + y + "]"; 256 } 257 }