The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | * contributor license agreements. See the NOTICE file distributed with |
| 4 | * this work for additional information regarding copyright ownership. |
| 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | * (the "License"); you may not use this file except in compliance with |
| 7 | * the License. You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | /** |
| 18 | * @author Denis M. Kishenko |
| 19 | * @version $Revision$ |
| 20 | */ |
| 21 | |
| 22 | package java.awt; |
| 23 | |
| 24 | import java.awt.geom.Point2D; |
| 25 | import java.io.Serializable; |
| 26 | |
| 27 | /** |
| 28 | * The Point class represents a point location with coordinates X, Y in current |
| 29 | * coordinate system. |
| 30 | * |
| 31 | * @since Android 1.0 |
| 32 | */ |
| 33 | public class Point extends Point2D implements Serializable { |
| 34 | |
| 35 | /** |
| 36 | * The Constant serialVersionUID. |
| 37 | */ |
| 38 | private static final long serialVersionUID = -5276940640259749850L; |
| 39 | |
| 40 | /** |
| 41 | * The X coordinate of Point. |
| 42 | */ |
| 43 | public int x; |
| 44 | |
| 45 | /** |
| 46 | * The Y coordinate of Point. |
| 47 | */ |
| 48 | public int y; |
| 49 | |
| 50 | /** |
| 51 | * Instantiates a new point with (0, O) coordinates, the origin of |
| 52 | * coordinate system. |
| 53 | */ |
| 54 | public Point() { |
| 55 | setLocation(0, 0); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Instantiates a new point with (x, y) coordinates. |
| 60 | * |
| 61 | * @param x |
| 62 | * the X coordinate of Point. |
| 63 | * @param y |
| 64 | * the Y coordinate of Point. |
| 65 | */ |
| 66 | public Point(int x, int y) { |
| 67 | setLocation(x, y); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Instantiates a new point, giving it the same location as the parameter p. |
| 72 | * |
| 73 | * @param p |
| 74 | * the Point object giving the coordinates of the new point. |
| 75 | */ |
| 76 | public Point(Point p) { |
| 77 | setLocation(p.x, p.y); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Compares current Point with the specified object. |
| 82 | * |
| 83 | * @param obj |
| 84 | * the Object to be compared. |
| 85 | * @return true, if the Object being compared is a Point whose coordinates |
| 86 | * are equal to the coordinates of this Point, false otherwise. |
| 87 | * @see java.awt.geom.Point2D#equals(Object) |
| 88 | */ |
| 89 | @Override |
| 90 | public boolean equals(Object obj) { |
| 91 | if (obj == this) { |
| 92 | return true; |
| 93 | } |
| 94 | if (obj instanceof Point) { |
| 95 | Point p = (Point)obj; |
| 96 | return x == p.x && y == p.y; |
| 97 | } |
| 98 | return false; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Returns string representation of the current Point object. |
| 103 | * |
| 104 | * @return a string representation of the current Point object. |
| 105 | */ |
| 106 | @Override |
| 107 | public String toString() { |
| 108 | return getClass().getName() + "[x=" + x + ",y=" + y + "]"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Gets X coordinate of Point as a double. |
| 113 | * |
| 114 | * @return X coordinate of the point as a double. |
| 115 | * @see java.awt.geom.Point2D#getX() |
| 116 | */ |
| 117 | @Override |
| 118 | public double getX() { |
| 119 | return x; |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Gets Y coordinate of Point as a double. |
| 124 | * |
| 125 | * @return Y coordinate of the point as a double. |
| 126 | * @see java.awt.geom.Point2D#getY() |
| 127 | */ |
| 128 | @Override |
| 129 | public double getY() { |
| 130 | return y; |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Gets the location of the Point as a new Point object. |
| 135 | * |
| 136 | * @return a copy of the Point. |
| 137 | */ |
| 138 | public Point getLocation() { |
| 139 | return new Point(x, y); |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Sets the location of the Point to the same coordinates as p. |
| 144 | * |
| 145 | * @param p |
| 146 | * the Point that gives the new location. |
| 147 | */ |
| 148 | public void setLocation(Point p) { |
| 149 | setLocation(p.x, p.y); |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Sets the location of the Point to the coordinates X, Y. |
| 154 | * |
| 155 | * @param x |
| 156 | * the X coordinate of the Point's new location. |
| 157 | * @param y |
| 158 | * the Y coordinate of the Point's new location. |
| 159 | */ |
| 160 | public void setLocation(int x, int y) { |
| 161 | this.x = x; |
| 162 | this.y = y; |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Sets the location of Point to the specified double coordinates. |
| 167 | * |
| 168 | * @param x |
| 169 | * the X the Point's new location. |
| 170 | * @param y |
| 171 | * the Y the Point's new location. |
| 172 | * @see java.awt.geom.Point2D#setLocation(double, double) |
| 173 | */ |
| 174 | @Override |
| 175 | public void setLocation(double x, double y) { |
| 176 | x = x < Integer.MIN_VALUE ? Integer.MIN_VALUE : x > Integer.MAX_VALUE ? Integer.MAX_VALUE |
| 177 | : x; |
| 178 | y = y < Integer.MIN_VALUE ? Integer.MIN_VALUE : y > Integer.MAX_VALUE ? Integer.MAX_VALUE |
| 179 | : y; |
| 180 | setLocation((int)Math.round(x), (int)Math.round(y)); |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Moves the Point to the specified (x, y) location. |
| 185 | * |
| 186 | * @param x |
| 187 | * the X coordinate of the new location. |
| 188 | * @param y |
| 189 | * the Y coordinate of the new location. |
| 190 | */ |
| 191 | public void move(int x, int y) { |
| 192 | setLocation(x, y); |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Translates current Point moving it from the position (x, y) to the new |
| 197 | * position given by (x+dx, x+dy) coordinates. |
| 198 | * |
| 199 | * @param dx |
| 200 | * the horizontal delta - the Point is moved to this distance |
| 201 | * along X axis. |
| 202 | * @param dy |
| 203 | * the vertical delta - the Point is moved to this distance along |
| 204 | * Y axis. |
| 205 | */ |
| 206 | public void translate(int dx, int dy) { |
| 207 | x += dx; |
| 208 | y += dy; |
| 209 | } |
| 210 | |
| 211 | } |