1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
/* * @(#)Point.java 1.40 06/02/24 * * Copyright 2006 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ package java.awt; import java.awt.geom.Point2D; /** * A point representing a location in {@code (x,y)} coordinate space, * specified in integer precision. * * @version 1.40, 02/24/06 * @author Sami Shaio * @since 1.0 */ public class Point extends Point2D implements java.io.Serializable { /** * The X coordinate of this <code>Point</code>. * If no X coordinate is set it will default to 0. * * @serial * @see #getLocation() * @see #move(int, int) * @since 1.0 */ public int x; /** * The Y coordinate of this <code>Point</code>. * If no Y coordinate is set it will default to 0. * * @serial * @see #getLocation() * @see #move(int, int) * @since 1.0 */ 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. * @since 1.1 */ public Point() { this(0, 0); } /** * Constructs and initializes a point with the same location as * the specified <code>Point</code> object. * @param p a point * @since 1.1 */ 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 <code>Point</code> * @param y the Y coordinate of the newly constructed <code>Point</code> * @since 1.0 */ public Point(int x, int y) { this.x = x; this.y = y; } /** * {@inheritDoc} * @since 1.2 */ public double getX() { return x; } /** * {@inheritDoc} * @since 1.2 */ public double getY() { return y; } /** * Returns the location of this point. * This method is included for completeness, to parallel the * <code>getLocation</code> method of <code>Component</code>. * @return a copy of this point, at the same location * @see java.awt.Component#getLocation * @see java.awt.Point#setLocation(java.awt.Point) * @see java.awt.Point#setLocation(int, int) * @since 1.1 */ public Point getLocation() { return new Point(x, y); } /** * Sets the location of the point to the specified location. * This method is included for completeness, to parallel the * <code>setLocation</code> method of <code>Component</code>. * @param p a point, the new location for this point * @see java.awt.Component#setLocation(java.awt.Point) * @see java.awt.Point#getLocation * @since 1.1 */ public void setLocation(Point p) { setLocation(p.x, p.y); } /** * Changes the point to have the specified location. * <p> * This method is included for completeness, to parallel the * <code>setLocation</code> method of <code>Component</code>. * Its behavior is identical with <code>move(int, int)</code>. * @param x the X coordinate of the new location * @param y the Y coordinate of the new location * @see java.awt.Component#setLocation(int, int) * @see java.awt.Point#getLocation * @see java.awt.Point#move(int, int) * @since 1.1 */ public void setLocation(int x, int y) { move(x, y); } /** * Sets the location of this point to the specified double coordinates. * The double values will be rounded to integer values. * Any number smaller than <code>Integer.MIN_VALUE</code> * will be reset to <code>MIN_VALUE</code>, and any number * larger than <code>Integer.MAX_VALUE</code> will be * reset to <code>MAX_VALUE</code>. * * @param x the X coordinate of the new location * @param y the Y coordinate of the new location * @see #getLocation */ public void setLocation(double x, double y) { this.x = (int) Math.floor(x+0.5); this.y = (int) Math.floor(y+0.5); } /** * Moves this point to the specified location in the * {@code (x,y)} coordinate plane. This method * is identical with <code>setLocation(int, int)</code>. * @param x the X coordinate of the new location * @param y the Y coordinate of the new location * @see java.awt.Component#setLocation(int, int) */ public void move(int x, int y) { this.x = x; this.y = y; } /** * 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 */ public void translate(int dx, int dy) { this.x += dx; this.y += dy; } /** * Determines whether or not two points are equal. Two instances of * <code>Point2D</code> are equal if the values of their * <code>x</code> and <code>y</code> member fields, representing * their position in the coordinate space, are the same. * @param obj an object to be compared with this <code>Point2D</code> * @return <code>true</code> if the object to be compared is * an instance of <code>Point2D</code> and has * the same values; <code>false</code> otherwise. */ public boolean equals(Object obj) { if (obj instanceof Point) { Point pt = (Point)obj; return (x == pt.x) && (y == pt.y); } return super.equals(obj); } /** * 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 <code>null</code>. * * @return a string representation of this point */ public String toString() { return getClass().getName() + "[x=" + x + ",y=" + y + "]"; } }