001: /*
002: * Copyright (C) 2005, 2006 data2c GmbH (www.data2c.com)
003: *
004: * Author: Wolfgang S. Kechel - wolfgang.kechel@data2c.com
005: *
006: * J2ME version of java.awt.Point.
007: */
008:
009: package org.awt;
010:
011: //#ifndef j2se
012: import org.awt.geom.Point2D;
013:
014: public class Point extends Point2D {
015: /**
016: * The <i>x</i> coordinate.
017: * If no <i>x</i> coordinate is set it will default to 0.
018: *
019: * @serial
020: * @see #getLocation()
021: * @see #move(int, int)
022: */
023: public int x;
024:
025: /**
026: * The <i>y</i> coordinate.
027: * If no <i>y</i> coordinate is set it will default to 0.
028: *
029: * @serial
030: * @see #getLocation()
031: * @see #move(int, int)
032: */
033: public int y;
034:
035: /**
036: * Constructs and initializes a point at the origin
037: * (0, 0) of the coordinate space.
038: */
039: public Point() {
040: this (0, 0);
041: }
042:
043: /**
044: * Constructs and initializes a point with the same location as
045: * the specified <code>Point</code> object.
046: * @param p a point
047: */
048: public Point(Point p) {
049: this (p.x, p.y);
050: }
051:
052: /**
053: * Constructs and initializes a point at the specified
054: * (<i>x</i>, <i>y</i>) location in the coordinate space.
055: * @param x the <i>x</i> coordinate
056: * @param y the <i>y</i> coordinate
057: */
058: public Point(int x, int y) {
059: this .x = x;
060: this .y = y;
061: }
062:
063: public Object clone() /*throws CloneNotSupportedException*/{
064: return new Point(x, y);
065: }
066:
067: /**
068: * Returns the X coordinate of the point in double precision.
069: * @return the X coordinate of the point in double precision
070: */
071: public double getX() {
072: return x;
073: }
074:
075: /**
076: * Returns the Y coordinate of the point in double precision.
077: * @return the Y coordinate of the point in double precision
078: */
079: public double getY() {
080: return y;
081: }
082:
083: /**
084: * Returns the location of this point.
085: * This method is included for completeness, to parallel the
086: * <code>getLocation</code> method of <code>Component</code>.
087: * @return a copy of this point, at the same location
088: */
089: public Point getLocation() {
090: return new Point(x, y);
091: }
092:
093: /**
094: * Sets the location of the point to the specified location.
095: * This method is included for completeness, to parallel the
096: * <code>setLocation</code> method of <code>Component</code>.
097: * @param p a point, the new location for this point
098: */
099: public void setLocation(Point p) {
100: setLocation(p.x, p.y);
101: }
102:
103: /**
104: * Changes the point to have the specified location.
105: * <p>
106: * This method is included for completeness, to parallel the
107: * <code>setLocation</code> method of <code>Component</code>.
108: * Its behavior is identical with <code>move(int, int)</code>.
109: * @param x the <i>x</i> coordinate of the new location
110: * @param y the <i>y</i> coordinate of the new location
111: */
112: public void setLocation(int x, int y) {
113: move(x, y);
114: }
115:
116: /**
117: * Sets the location of this point to the specified double coordinates.
118: * The double values will be rounded to integer values.
119: * Any number smaller than <code>Integer.MIN_VALUE</code>
120: * will be reset to <code>MIN_VALUE</code>, and any number
121: * larger than <code>Integer.MAX_VALUE</code> will be
122: * reset to <code>MAX_VALUE</code>.
123: *
124: * @param x the <i>x</i> coordinate of the new location
125: * @param y the <i>y</i> coordinate of the new location
126: * @see #getLocation
127: */
128: public void setLocation(double x, double y) {
129: this .x = (int) Math.floor(x + 0.5);
130: this .y = (int) Math.floor(y + 0.5);
131: }
132:
133: /**
134: * Moves this point to the specified location in the
135: * (<i>x</i>, <i>y</i>) coordinate plane. This method
136: * is identical with <code>setLocation(int, int)</code>.
137: * @param x the <i>x</i> coordinate of the new location
138: * @param y the <i>y</i> coordinate of the new location
139: */
140: public void move(int x, int y) {
141: this .x = x;
142: this .y = y;
143: }
144:
145: /**
146: * Translates this point, at location (<i>x</i>, <i>y</i>),
147: * by <code>dx</code> along the <i>x</i> axis and <code>dy</code>
148: * along the <i>y</i> axis so that it now represents the point
149: * (<code>x</code> <code>+</code> <code>dx</code>,
150: * <code>y</code> <code>+</code> <code>dy</code>).
151: * @param dx the distance to move this point
152: * along the <i>x</i> axis
153: * @param dy the distance to move this point
154: * along the <i>y</i> axis
155: */
156: public void translate(int dx, int dy) {
157: this .x += dx;
158: this .y += dy;
159: }
160:
161: /**
162: * Determines whether or not two points are equal. Two instances of
163: * <code>Point</code> are equal if the values of their
164: * <code>x</code> and <code>y</code> member fields, representing
165: * their position in the coordinate space, are the same.
166: * @param obj an object to be compared with this <code>Point</code>
167: * @return <code>true</code> if the object to be compared is
168: * an instance of <code>Point</code> and has
169: * the same values; <code>false</code> otherwise.
170: */
171: public boolean equals(Object obj) {
172: if (obj instanceof Point) {
173: Point pt = (Point) obj;
174: return (x == pt.x) && (y == pt.y);
175: }
176: return super .equals(obj);
177: }
178:
179: /**
180: * Returns a string representation of this point and its location
181: * in the (<i>x</i>, <i>y</i>) coordinate space. This method is
182: * intended to be used only for debugging purposes, and the content
183: * and format of the returned string may vary between implementations.
184: * The returned string may be empty but may not be <code>null</code>.
185: *
186: * @return a string representation of this point
187: */
188: public String toString() {
189: return "Point[" + x + ", " + y + "]";
190: }
191: }
192: //#endif
|