/* * Copyright (c) 1995, 2021, 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.
*/
/** * A point representing a location in {@code (x,y)} coordinate space, * specified in integer precision. * * @author Sami Shaio * @since 1.0
*/ publicclass Point extends Point2D implements java.io.Serializable { /** * The X coordinate of this {@code Point}. * If no X coordinate is set it will default to 0. * * @serial * @see #getLocation() * @see #move(int, int) * @since 1.0
*/ publicint x;
/** * The Y coordinate of this {@code Point}. * If no Y coordinate is set it will default to 0. * * @serial * @see #getLocation() * @see #move(int, int) * @since 1.0
*/ publicint y;
/** * Use serialVersionUID from JDK 1.1 for interoperability.
*/
@Serial privatestaticfinallong 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} 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} * @param y the Y coordinate of the newly constructed {@code Point} * @since 1.0
*/ public Point(int x, int y) { this.x = x; this.y = y;
}
/** * Returns the location of this point. * This method is included for completeness, to parallel the * {@code getLocation} method of {@code Component}. * @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
*/
@Transient public Point getLocation() { returnnew Point(x, y);
}
/** * Sets the location of the point to the specified location. * This method is included for completeness, to parallel the * {@code setLocation} method of {@code Component}. * @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
*/ publicvoid 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} method of {@code Component}. * 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
*/ publicvoid 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} * will be reset to {@code MIN_VALUE}, and any number * larger than {@code Integer.MAX_VALUE} will be * reset to {@code MAX_VALUE}. * * @param x the X coordinate of the new location * @param y the Y coordinate of the new location * @see #getLocation
*/ publicvoid 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)
*/ publicvoid 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
*/ publicvoid translate(int dx, int dy) { this.x += dx; this.y += dy;
}
/** * Determines whether or not two points are equal. Two instances of * {@code Point2D} are equal if the values of their * {@code x} and {@code y} member fields, representing * their position in the coordinate space, are the same. * @param obj an object to be compared with this {@code Point2D} * @return {@code true} if the object to be compared is * an instance of {@code Point2D} and has * the same values; {@code false} otherwise.
*/ publicboolean equals(Object obj) { if (obj instanceof Point) {
Point pt = (Point)obj; return (x == pt.x) && (y == pt.y);
} returnsuper.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}. * * @return a string representation of this point
*/ public String toString() { return getClass().getName() + "[x=" + x + ",y=" + y + "]";
}
}
¤ Dauer der Verarbeitung: 0.28 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.