jdk/src/share/classes/java/awt/Rectangle.java
author malenkov
Wed, 07 May 2008 23:20:32 +0400
changeset 466 6acd5ec503a8
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE Summary: Add the Transient annotation and support it (JSR-273) Reviewed-by: peterz, loneid
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
466
6acd5ec503a8 4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents: 2
diff changeset
     2
 * Copyright 1995-2008 Sun Microsystems, Inc.  All Rights Reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.awt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.geom.Rectangle2D;
466
6acd5ec503a8 4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents: 2
diff changeset
    29
import java.beans.Transient;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * A <code>Rectangle</code> specifies an area in a coordinate space that is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * enclosed by the <code>Rectangle</code> object's upper-left point
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * {@code (x,y)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * in the coordinate space, its width, and its height.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * A <code>Rectangle</code> object's <code>width</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * <code>height</code> are <code>public</code> fields. The constructors
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * that create a <code>Rectangle</code>, and the methods that can modify
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * one, do not prevent setting a negative value for width or height.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <a name="Empty">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * A {@code Rectangle} whose width or height is exactly zero has location
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * along those axes with zero dimension, but is otherwise considered empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * The {@link #isEmpty} method will return true for such a {@code Rectangle}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * Methods which test if an empty {@code Rectangle} contains or intersects
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * a point or rectangle will always return false if either dimension is zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * Methods which combine such a {@code Rectangle} with a point or rectangle
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * will include the location of the {@code Rectangle} on that axis in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * result as if the {@link #add(Point)} method were being called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * </a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * <a name="NonExistant">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * A {@code Rectangle} whose width or height is negative has neither
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * location nor dimension along those axes with negative dimensions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * Such a {@code Rectangle} is treated as non-existant along those axes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * Such a {@code Rectangle} is also empty with respect to containment
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * calculations and methods which test if it contains or intersects a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * point or rectangle will always return false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * Methods which combine such a {@code Rectangle} with a point or rectangle
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * will ignore the {@code Rectangle} entirely in generating the result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * If two {@code Rectangle} objects are combined and each has a negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * dimension, the result will have at least one negative dimension.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * </a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * Methods which affect only the location of a {@code Rectangle} will
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * operate on its location regardless of whether or not it has a negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * or zero dimension along either axis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * Note that a {@code Rectangle} constructed with the default no-argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * constructor will have dimensions of {@code 0x0} and therefore be empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * That {@code Rectangle} will still have a location of {@code (0,0)} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * will contribute that location to the union and add operations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * Code attempting to accumulate the bounds of a set of points should
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * therefore initially construct the {@code Rectangle} with a specifically
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * negative width and height or it should use the first point in the set
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * to construct the {@code Rectangle}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * For example:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *     Rectangle bounds = new Rectangle(0, 0, -1, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *     for (int i = 0; i < points.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *         bounds.add(points[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * or if we know that the points array contains at least one point:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 *     Rectangle bounds = new Rectangle(points[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 *     for (int i = 1; i < points.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 *         bounds.add(points[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * This class uses 32-bit integers to store its location and dimensions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * Frequently operations may produce a result that exceeds the range of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * a 32-bit integer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * The methods will calculate their results in a way that avoids any
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * 32-bit overflow for intermediate results and then choose the best
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * representation to store the final results back into the 32-bit fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * which hold the location and dimensions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * The location of the result will be stored into the {@link #x} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * {@link #y} fields by clipping the true result to the nearest 32-bit value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * The values stored into the {@link #width} and {@link #height} dimension
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * fields will be chosen as the 32-bit values that encompass the largest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * part of the true result as possible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * Generally this means that the dimension will be clipped independently
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * to the range of 32-bit integers except that if the location had to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * moved to store it into its pair of 32-bit fields then the dimensions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * will be adjusted relative to the "best representation" of the location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * If the true result had a negative dimension and was therefore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * non-existant along one or both axes, the stored dimensions will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * negative numbers in those axes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * If the true result had a location that could be represented within
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * the range of 32-bit integers, but zero dimension along one or both
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * axes, then the stored dimensions will be zero in those axes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 * @author      Sami Shaio
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 * @since 1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
public class Rectangle extends Rectangle2D
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    implements Shape, java.io.Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * The X coordinate of the upper-left corner of the <code>Rectangle</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * @see #setLocation(int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * @see #getLocation()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * @since 1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    public int x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * The Y coordinate of the upper-left corner of the <code>Rectangle</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * @see #setLocation(int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * @see #getLocation()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * @since 1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    public int y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * The width of the <code>Rectangle</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * @see #setSize(int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * @see #getSize()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * @since 1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    public int width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * The height of the <code>Rectangle</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * @see #setSize(int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * @see #getSize()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * @since 1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    public int height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * JDK 1.1 serialVersionUID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     private static final long serialVersionUID = -4345857070255674764L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * Initialize JNI field and method IDs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    private static native void initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        /* ensure that the necessary native libraries are loaded */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        Toolkit.loadLibraries();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        if (!GraphicsEnvironment.isHeadless()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * Constructs a new <code>Rectangle</code> whose upper-left corner
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * is at (0,&nbsp;0) in the coordinate space, and whose width and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * height are both zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    public Rectangle() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        this(0, 0, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * Constructs a new <code>Rectangle</code>, initialized to match
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * the values of the specified <code>Rectangle</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * @param r  the <code>Rectangle</code> from which to copy initial values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     *           to a newly constructed <code>Rectangle</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * @since 1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    public Rectangle(Rectangle r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        this(r.x, r.y, r.width, r.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * Constructs a new <code>Rectangle</code> whose upper-left corner is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * specified as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * {@code (x,y)} and whose width and height
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * are specified by the arguments of the same name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * @param     x the specified X coordinate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * @param     y the specified Y coordinate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * @param     width    the width of the <code>Rectangle</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * @param     height   the height of the <code>Rectangle</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * @since 1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    public Rectangle(int x, int y, int width, int height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        this.x = x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        this.y = y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        this.width = width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        this.height = height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * Constructs a new <code>Rectangle</code> whose upper-left corner
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * is at (0,&nbsp;0) in the coordinate space, and whose width and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * height are specified by the arguments of the same name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * @param width the width of the <code>Rectangle</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * @param height the height of the <code>Rectangle</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    public Rectangle(int width, int height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        this(0, 0, width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * Constructs a new <code>Rectangle</code> whose upper-left corner is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * specified by the {@link Point} argument, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * whose width and height are specified by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * {@link Dimension} argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * @param p a <code>Point</code> that is the upper-left corner of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * the <code>Rectangle</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * @param d a <code>Dimension</code>, representing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * width and height of the <code>Rectangle</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    public Rectangle(Point p, Dimension d) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        this(p.x, p.y, d.width, d.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * Constructs a new <code>Rectangle</code> whose upper-left corner is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * specified <code>Point</code>, and whose width and height are both zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * @param p a <code>Point</code> that is the top left corner
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * of the <code>Rectangle</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    public Rectangle(Point p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        this(p.x, p.y, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * Constructs a new <code>Rectangle</code> whose top left corner is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * (0,&nbsp;0) and whose width and height are specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * by the <code>Dimension</code> argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * @param d a <code>Dimension</code>, specifying width and height
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    public Rectangle(Dimension d) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        this(0, 0, d.width, d.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * Returns the X coordinate of the bounding <code>Rectangle</code> in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * <code>double</code> precision.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * @return the X coordinate of the bounding <code>Rectangle</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    public double getX() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        return x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * Returns the Y coordinate of the bounding <code>Rectangle</code> in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * <code>double</code> precision.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * @return the Y coordinate of the bounding <code>Rectangle</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    public double getY() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        return y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * Returns the width of the bounding <code>Rectangle</code> in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * <code>double</code> precision.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * @return the width of the bounding <code>Rectangle</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    public double getWidth() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        return width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * Returns the height of the bounding <code>Rectangle</code> in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * <code>double</code> precision.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * @return the height of the bounding <code>Rectangle</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    public double getHeight() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        return height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * Gets the bounding <code>Rectangle</code> of this <code>Rectangle</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * This method is included for completeness, to parallel the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * <code>getBounds</code> method of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * {@link Component}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * @return    a new <code>Rectangle</code>, equal to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * bounding <code>Rectangle</code> for this <code>Rectangle</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * @see       java.awt.Component#getBounds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * @see       #setBounds(Rectangle)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * @see       #setBounds(int, int, int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * @since     1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     */
466
6acd5ec503a8 4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents: 2
diff changeset
   312
    @Transient
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    public Rectangle getBounds() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        return new Rectangle(x, y, width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    public Rectangle2D getBounds2D() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        return new Rectangle(x, y, width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * Sets the bounding <code>Rectangle</code> of this <code>Rectangle</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * to match the specified <code>Rectangle</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * This method is included for completeness, to parallel the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * <code>setBounds</code> method of <code>Component</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * @param r the specified <code>Rectangle</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * @see       #getBounds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * @see       java.awt.Component#setBounds(java.awt.Rectangle)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * @since     1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    public void setBounds(Rectangle r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        setBounds(r.x, r.y, r.width, r.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * Sets the bounding <code>Rectangle</code> of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * <code>Rectangle</code> to the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * <code>x</code>, <code>y</code>, <code>width</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * and <code>height</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * This method is included for completeness, to parallel the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * <code>setBounds</code> method of <code>Component</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * @param x the new X coordinate for the upper-left
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     *                    corner of this <code>Rectangle</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * @param y the new Y coordinate for the upper-left
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     *                    corner of this <code>Rectangle</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * @param width the new width for this <code>Rectangle</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * @param height the new height for this <code>Rectangle</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * @see       #getBounds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * @see       java.awt.Component#setBounds(int, int, int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * @since     1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    public void setBounds(int x, int y, int width, int height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        reshape(x, y, width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * Sets the bounds of this {@code Rectangle} to the integer bounds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * which encompass the specified {@code x}, {@code y}, {@code width},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * and {@code height}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * If the parameters specify a {@code Rectangle} that exceeds the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * maximum range of integers, the result will be the best
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * representation of the specified {@code Rectangle} intersected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * with the maximum integer bounds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * @param x the X coordinate of the upper-left corner of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     *                  the specified rectangle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * @param y the Y coordinate of the upper-left corner of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     *                  the specified rectangle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * @param width the width of the specified rectangle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * @param height the new height of the specified rectangle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    public void setRect(double x, double y, double width, double height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        int newx, newy, neww, newh;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        if (x > 2.0 * Integer.MAX_VALUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            // Too far in positive X direction to represent...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            // We cannot even reach the left side of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            // rectangle even with both x & width set to MAX_VALUE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            // The intersection with the "maximal integer rectangle"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            // is non-existant so we should use a width < 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            // REMIND: Should we try to determine a more "meaningful"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            // adjusted value for neww than just "-1"?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            newx = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            neww = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            newx = clip(x, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            if (width >= 0) width += x-newx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            neww = clip(width, width >= 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        if (y > 2.0 * Integer.MAX_VALUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            // Too far in positive Y direction to represent...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            newy = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            newh = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            newy = clip(y, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            if (height >= 0) height += y-newy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            newh = clip(height, height >= 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        reshape(newx, newy, neww, newh);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    // Return best integer representation for v, clipped to integer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    // range and floor-ed or ceiling-ed, depending on the boolean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    private static int clip(double v, boolean doceil) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        if (v <= Integer.MIN_VALUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            return Integer.MIN_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        if (v >= Integer.MAX_VALUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            return Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        return (int) (doceil ? Math.ceil(v) : Math.floor(v));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * Sets the bounding <code>Rectangle</code> of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * <code>Rectangle</code> to the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * <code>x</code>, <code>y</code>, <code>width</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * and <code>height</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * @param x the new X coordinate for the upper-left
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     *                    corner of this <code>Rectangle</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * @param y the new Y coordinate for the upper-left
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     *                    corner of this <code>Rectangle</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * @param width the new width for this <code>Rectangle</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * @param height the new height for this <code>Rectangle</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * @deprecated As of JDK version 1.1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * replaced by <code>setBounds(int, int, int, int)</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
    public void reshape(int x, int y, int width, int height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        this.x = x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        this.y = y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        this.width = width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        this.height = height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * Returns the location of this <code>Rectangle</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * This method is included for completeness, to parallel the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * <code>getLocation</code> method of <code>Component</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * @return the <code>Point</code> that is the upper-left corner of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     *                  this <code>Rectangle</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * @see       java.awt.Component#getLocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * @see       #setLocation(Point)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * @see       #setLocation(int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * @since     1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    public Point getLocation() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        return new Point(x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     * Moves this <code>Rectangle</code> to the specified location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * This method is included for completeness, to parallel the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * <code>setLocation</code> method of <code>Component</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     * @param p the <code>Point</code> specifying the new location
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     *                for this <code>Rectangle</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * @see       java.awt.Component#setLocation(java.awt.Point)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * @see       #getLocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * @since     1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    public void setLocation(Point p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        setLocation(p.x, p.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * Moves this <code>Rectangle</code> to the specified location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * This method is included for completeness, to parallel the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * <code>setLocation</code> method of <code>Component</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * @param x the X coordinate of the new location
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * @param y the Y coordinate of the new location
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     * @see       #getLocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * @see       java.awt.Component#setLocation(int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * @since     1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    public void setLocation(int x, int y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        move(x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * Moves this <code>Rectangle</code> to the specified location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * @param x the X coordinate of the new location
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * @param y the Y coordinate of the new location
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * @deprecated As of JDK version 1.1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * replaced by <code>setLocation(int, int)</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
    public void move(int x, int y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        this.x = x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        this.y = y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     * Translates this <code>Rectangle</code> the indicated distance,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * to the right along the X coordinate axis, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * downward along the Y coordinate axis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * @param dx the distance to move this <code>Rectangle</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     *                 along the X axis
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * @param dy the distance to move this <code>Rectangle</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     *                 along the Y axis
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * @see       java.awt.Rectangle#setLocation(int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * @see       java.awt.Rectangle#setLocation(java.awt.Point)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
    public void translate(int dx, int dy) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        int oldv = this.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        int newv = oldv + dx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        if (dx < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
            // moving leftward
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            if (newv > oldv) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                // negative overflow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                // Only adjust width if it was valid (>= 0).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                if (width >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                    // The right edge is now conceptually at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                    // newv+width, but we may move newv to prevent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                    // overflow.  But we want the right edge to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                    // remain at its new location in spite of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                    // clipping.  Think of the following adjustment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                    // conceptually the same as:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                    // width += newv; newv = MIN_VALUE; width -= newv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                    width += newv - Integer.MIN_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                    // width may go negative if the right edge went past
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                    // MIN_VALUE, but it cannot overflow since it cannot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                    // have moved more than MIN_VALUE and any non-negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                    // number + MIN_VALUE does not overflow.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                newv = Integer.MIN_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            // moving rightward (or staying still)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            if (newv < oldv) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                // positive overflow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                if (width >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                    // Conceptually the same as:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                    // width += newv; newv = MAX_VALUE; width -= newv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                    width += newv - Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                    // With large widths and large displacements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                    // we may overflow so we need to check it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                    if (width < 0) width = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                newv = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        this.x = newv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        oldv = this.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        newv = oldv + dy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        if (dy < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
            // moving upward
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            if (newv > oldv) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                // negative overflow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                if (height >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                    height += newv - Integer.MIN_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                    // See above comment about no overflow in this case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                newv = Integer.MIN_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
            // moving downward (or staying still)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            if (newv < oldv) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                // positive overflow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                if (height >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                    height += newv - Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                    if (height < 0) height = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                newv = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        this.y = newv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * Gets the size of this <code>Rectangle</code>, represented by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * the returned <code>Dimension</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     * This method is included for completeness, to parallel the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * <code>getSize</code> method of <code>Component</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     * @return a <code>Dimension</code>, representing the size of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     *            this <code>Rectangle</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     * @see       java.awt.Component#getSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     * @see       #setSize(Dimension)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     * @see       #setSize(int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * @since     1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
    public Dimension getSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        return new Dimension(width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * Sets the size of this <code>Rectangle</code> to match the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     * specified <code>Dimension</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     * This method is included for completeness, to parallel the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     * <code>setSize</code> method of <code>Component</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     * @param d the new size for the <code>Dimension</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     * @see       java.awt.Component#setSize(java.awt.Dimension)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     * @see       #getSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     * @since     1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
    public void setSize(Dimension d) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        setSize(d.width, d.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     * Sets the size of this <code>Rectangle</code> to the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     * width and height.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     * This method is included for completeness, to parallel the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     * <code>setSize</code> method of <code>Component</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     * @param width the new width for this <code>Rectangle</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     * @param height the new height for this <code>Rectangle</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     * @see       java.awt.Component#setSize(int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     * @see       #getSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     * @since     1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
    public void setSize(int width, int height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        resize(width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * Sets the size of this <code>Rectangle</code> to the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     * width and height.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     * @param width the new width for this <code>Rectangle</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     * @param height the new height for this <code>Rectangle</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     * @deprecated As of JDK version 1.1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     * replaced by <code>setSize(int, int)</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
    public void resize(int width, int height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        this.width = width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        this.height = height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     * Checks whether or not this <code>Rectangle</code> contains the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     * specified <code>Point</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     * @param p the <code>Point</code> to test
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     * @return    <code>true</code> if the specified <code>Point</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     *            is inside this <code>Rectangle</code>;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     *            <code>false</code> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     * @since     1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
    public boolean contains(Point p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        return contains(p.x, p.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     * Checks whether or not this <code>Rectangle</code> contains the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     * point at the specified location {@code (x,y)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     * @param  x the specified X coordinate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     * @param  y the specified Y coordinate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     * @return    <code>true</code> if the point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     *            {@code (x,y)} is inside this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     *            <code>Rectangle</code>;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     *            <code>false</code> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     * @since     1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
    public boolean contains(int x, int y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        return inside(x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     * Checks whether or not this <code>Rectangle</code> entirely contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     * the specified <code>Rectangle</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     * @param     r   the specified <code>Rectangle</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     * @return    <code>true</code> if the <code>Rectangle</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     *            is contained entirely inside this <code>Rectangle</code>;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     *            <code>false</code> otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     * @since     1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
    public boolean contains(Rectangle r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        return contains(r.x, r.y, r.width, r.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     * Checks whether this <code>Rectangle</code> entirely contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     * the <code>Rectangle</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     * at the specified location {@code (X,Y)} with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     * specified dimensions {@code (W,H)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     * @param     X the specified X coordinate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     * @param     Y the specified Y coordinate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     * @param     W   the width of the <code>Rectangle</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     * @param     H   the height of the <code>Rectangle</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     * @return    <code>true</code> if the <code>Rectangle</code> specified by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     *            {@code (X, Y, W, H)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
     *            is entirely enclosed inside this <code>Rectangle</code>;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
     *            <code>false</code> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
     * @since     1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
    public boolean contains(int X, int Y, int W, int H) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        int w = this.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        int h = this.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        if ((w | h | W | H) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
            // At least one of the dimensions is negative...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        // Note: if any dimension is zero, tests below must return false...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        int x = this.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        int y = this.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
        if (X < x || Y < y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
        w += x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
        W += X;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
        if (W <= X) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
            // X+W overflowed or W was zero, return false if...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
            // either original w or W was zero or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
            // x+w did not overflow or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
            // the overflowed x+w is smaller than the overflowed X+W
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
            if (w >= x || W > w) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
            // X+W did not overflow and W was not zero, return false if...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
            // original w was zero or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
            // x+w did not overflow and x+w is smaller than X+W
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
            if (w >= x && W > w) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        h += y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        H += Y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        if (H <= Y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
            if (h >= y || H > h) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
            if (h >= y && H > h) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
     * Checks whether or not this <code>Rectangle</code> contains the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
     * point at the specified location {@code (X,Y)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
     * @param  X the specified X coordinate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
     * @param  Y the specified Y coordinate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
     * @return    <code>true</code> if the point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
     *            {@code (X,Y)} is inside this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
     *            <code>Rectangle</code>;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
     *            <code>false</code> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
     * @deprecated As of JDK version 1.1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
     * replaced by <code>contains(int, int)</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
    public boolean inside(int X, int Y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
        int w = this.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        int h = this.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        if ((w | h) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
            // At least one of the dimensions is negative...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
        // Note: if either dimension is zero, tests below must return false...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
        int x = this.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
        int y = this.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
        if (X < x || Y < y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
        w += x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
        h += y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
        //    overflow || intersect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        return ((w < x || w > X) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
                (h < y || h > Y));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
     * Determines whether or not this <code>Rectangle</code> and the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
     * <code>Rectangle</code> intersect. Two rectangles intersect if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
     * their intersection is nonempty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
     * @param r the specified <code>Rectangle</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
     * @return    <code>true</code> if the specified <code>Rectangle</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
     *            and this <code>Rectangle</code> intersect;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
     *            <code>false</code> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
    public boolean intersects(Rectangle r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
        int tw = this.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
        int th = this.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
        int rw = r.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
        int rh = r.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
        if (rw <= 0 || rh <= 0 || tw <= 0 || th <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
        int tx = this.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
        int ty = this.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
        int rx = r.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
        int ry = r.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
        rw += rx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
        rh += ry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
        tw += tx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
        th += ty;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
        //      overflow || intersect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
        return ((rw < rx || rw > tx) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
                (rh < ry || rh > ty) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
                (tw < tx || tw > rx) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
                (th < ty || th > ry));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     * Computes the intersection of this <code>Rectangle</code> with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     * specified <code>Rectangle</code>. Returns a new <code>Rectangle</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
     * that represents the intersection of the two rectangles.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
     * If the two rectangles do not intersect, the result will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
     * an empty rectangle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
     * @param     r   the specified <code>Rectangle</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
     * @return    the largest <code>Rectangle</code> contained in both the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
     *            specified <code>Rectangle</code> and in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
     *            this <code>Rectangle</code>; or if the rectangles
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
     *            do not intersect, an empty rectangle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
    public Rectangle intersection(Rectangle r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
        int tx1 = this.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
        int ty1 = this.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
        int rx1 = r.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
        int ry1 = r.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
        long tx2 = tx1; tx2 += this.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
        long ty2 = ty1; ty2 += this.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
        long rx2 = rx1; rx2 += r.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
        long ry2 = ry1; ry2 += r.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
        if (tx1 < rx1) tx1 = rx1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
        if (ty1 < ry1) ty1 = ry1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
        if (tx2 > rx2) tx2 = rx2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
        if (ty2 > ry2) ty2 = ry2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        tx2 -= tx1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
        ty2 -= ty1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
        // tx2,ty2 will never overflow (they will never be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
        // larger than the smallest of the two source w,h)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
        // they might underflow, though...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
        if (tx2 < Integer.MIN_VALUE) tx2 = Integer.MIN_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
        if (ty2 < Integer.MIN_VALUE) ty2 = Integer.MIN_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
        return new Rectangle(tx1, ty1, (int) tx2, (int) ty2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
     * Computes the union of this <code>Rectangle</code> with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
     * specified <code>Rectangle</code>. Returns a new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
     * <code>Rectangle</code> that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
     * represents the union of the two rectangles.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
     * If either {@code Rectangle} has any dimension less than zero
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
     * the rules for <a href=#NonExistant>non-existant</a> rectangles
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
     * apply.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
     * If only one has a dimension less than zero, then the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
     * will be a copy of the other {@code Rectangle}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
     * If both have dimension less than zero, then the result will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
     * have at least one dimension less than zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
     * If the resulting {@code Rectangle} would have a dimension
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
     * too large to be expressed as an {@code int}, the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
     * will have a dimension of {@code Integer.MAX_VALUE} along
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
     * that dimension.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
     * @param r the specified <code>Rectangle</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
     * @return    the smallest <code>Rectangle</code> containing both
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
     *            the specified <code>Rectangle</code> and this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
     *            <code>Rectangle</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
    public Rectangle union(Rectangle r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
        long tx2 = this.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
        long ty2 = this.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
        if ((tx2 | ty2) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
            // This rectangle has negative dimensions...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
            // If r has non-negative dimensions then it is the answer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
            // If r is non-existant (has a negative dimension), then both
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
            // are non-existant and we can return any non-existant rectangle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
            // as an answer.  Thus, returning r meets that criterion.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
            // Either way, r is our answer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
            return new Rectangle(r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
        long rx2 = r.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
        long ry2 = r.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
        if ((rx2 | ry2) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
            return new Rectangle(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
        int tx1 = this.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
        int ty1 = this.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
        tx2 += tx1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
        ty2 += ty1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
        int rx1 = r.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
        int ry1 = r.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
        rx2 += rx1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
        ry2 += ry1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
        if (tx1 > rx1) tx1 = rx1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
        if (ty1 > ry1) ty1 = ry1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
        if (tx2 < rx2) tx2 = rx2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
        if (ty2 < ry2) ty2 = ry2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
        tx2 -= tx1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
        ty2 -= ty1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
        // tx2,ty2 will never underflow since both original rectangles
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
        // were already proven to be non-empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
        // they might overflow, though...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
        if (tx2 > Integer.MAX_VALUE) tx2 = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
        if (ty2 > Integer.MAX_VALUE) ty2 = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
        return new Rectangle(tx1, ty1, (int) tx2, (int) ty2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
     * Adds a point, specified by the integer arguments {@code newx,newy}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
     * to the bounds of this {@code Rectangle}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
     * If this {@code Rectangle} has any dimension less than zero,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
     * the rules for <a href=#NonExistant>non-existant</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
     * rectangles apply.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
     * In that case, the new bounds of this {@code Rectangle} will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
     * have a location equal to the specified coordinates and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
     * width and height equal to zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
     * After adding a point, a call to <code>contains</code> with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
     * added point as an argument does not necessarily return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
     * <code>true</code>. The <code>contains</code> method does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
     * return <code>true</code> for points on the right or bottom
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
     * edges of a <code>Rectangle</code>. Therefore, if the added point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
     * falls on the right or bottom edge of the enlarged
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
     * <code>Rectangle</code>, <code>contains</code> returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
     * <code>false</code> for that point.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
     * If the specified point must be contained within the new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
     * {@code Rectangle}, a 1x1 rectangle should be added instead:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
     *     r.add(newx, newy, 1, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
     * @param newx the X coordinate of the new point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
     * @param newy the Y coordinate of the new point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
    public void add(int newx, int newy) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
        if ((width | height) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
            this.x = newx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
            this.y = newy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
            this.width = this.height = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
        int x1 = this.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
        int y1 = this.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
        long x2 = this.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
        long y2 = this.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
        x2 += x1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
        y2 += y1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
        if (x1 > newx) x1 = newx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
        if (y1 > newy) y1 = newy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
        if (x2 < newx) x2 = newx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
        if (y2 < newy) y2 = newy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
        x2 -= x1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
        y2 -= y1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
        if (x2 > Integer.MAX_VALUE) x2 = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
        if (y2 > Integer.MAX_VALUE) y2 = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
        reshape(x1, y1, (int) x2, (int) y2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
     * Adds the specified {@code Point} to the bounds of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
     * {@code Rectangle}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
     * If this {@code Rectangle} has any dimension less than zero,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
     * the rules for <a href=#NonExistant>non-existant</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
     * rectangles apply.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
     * In that case, the new bounds of this {@code Rectangle} will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
     * have a location equal to the coordinates of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
     * {@code Point} and width and height equal to zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
     * After adding a <code>Point</code>, a call to <code>contains</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
     * with the added <code>Point</code> as an argument does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
     * necessarily return <code>true</code>. The <code>contains</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
     * method does not return <code>true</code> for points on the right
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
     * or bottom edges of a <code>Rectangle</code>. Therefore if the added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
     * <code>Point</code> falls on the right or bottom edge of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
     * enlarged <code>Rectangle</code>, <code>contains</code> returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
     * <code>false</code> for that <code>Point</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
     * If the specified point must be contained within the new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
     * {@code Rectangle}, a 1x1 rectangle should be added instead:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
     *     r.add(pt.x, pt.y, 1, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
     * @param pt the new <code>Point</code> to add to this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
     *           <code>Rectangle</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
    public void add(Point pt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
        add(pt.x, pt.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
     * Adds a <code>Rectangle</code> to this <code>Rectangle</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
     * The resulting <code>Rectangle</code> is the union of the two
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
     * rectangles.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
     * If either {@code Rectangle} has any dimension less than 0, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
     * result will have the dimensions of the other {@code Rectangle}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
     * If both {@code Rectangle}s have at least one dimension less
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
     * than 0, the result will have at least one dimension less than 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
     * If either {@code Rectangle} has one or both dimensions equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
     * to 0, the result along those axes with 0 dimensions will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
     * equivalent to the results obtained by adding the corresponding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
     * origin coordinate to the result rectangle along that axis,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
     * similar to the operation of the {@link #add(Point)} method,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
     * but contribute no further dimension beyond that.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
     * If the resulting {@code Rectangle} would have a dimension
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
     * too large to be expressed as an {@code int}, the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
     * will have a dimension of {@code Integer.MAX_VALUE} along
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
     * that dimension.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
     * @param  r the specified <code>Rectangle</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
    public void add(Rectangle r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
        long tx2 = this.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
        long ty2 = this.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
        if ((tx2 | ty2) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
            reshape(r.x, r.y, r.width, r.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
        long rx2 = r.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
        long ry2 = r.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
        if ((rx2 | ry2) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
        int tx1 = this.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
        int ty1 = this.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
        tx2 += tx1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
        ty2 += ty1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
        int rx1 = r.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
        int ry1 = r.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
        rx2 += rx1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
        ry2 += ry1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
        if (tx1 > rx1) tx1 = rx1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
        if (ty1 > ry1) ty1 = ry1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
        if (tx2 < rx2) tx2 = rx2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
        if (ty2 < ry2) ty2 = ry2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
        tx2 -= tx1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
        ty2 -= ty1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
        // tx2,ty2 will never underflow since both original
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
        // rectangles were non-empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
        // they might overflow, though...
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
        if (tx2 > Integer.MAX_VALUE) tx2 = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
        if (ty2 > Integer.MAX_VALUE) ty2 = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
        reshape(tx1, ty1, (int) tx2, (int) ty2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
     * Resizes the <code>Rectangle</code> both horizontally and vertically.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
     * This method modifies the <code>Rectangle</code> so that it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
     * <code>h</code> units larger on both the left and right side,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
     * and <code>v</code> units larger at both the top and bottom.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
     * The new <code>Rectangle</code> has {@code (x - h, y - v)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
     * as its upper-left corner,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
     * width of {@code (width + 2h)},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
     * and a height of {@code (height + 2v)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
     * If negative values are supplied for <code>h</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
     * <code>v</code>, the size of the <code>Rectangle</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
     * decreases accordingly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
     * The {@code grow} method will check for integer overflow
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
     * and underflow, but does not check whether the resulting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
     * values of {@code width} and {@code height} grow
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
     * from negative to non-negative or shrink from non-negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
     * to negative.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
     * @param h the horizontal expansion
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
     * @param v the vertical expansion
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
    public void grow(int h, int v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
        long x0 = this.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
        long y0 = this.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
        long x1 = this.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
        long y1 = this.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
        x1 += x0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
        y1 += y0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
        x0 -= h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
        y0 -= v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
        x1 += h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
        y1 += v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
        if (x1 < x0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
            // Non-existant in X direction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
            // Final width must remain negative so subtract x0 before
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
            // it is clipped so that we avoid the risk that the clipping
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
            // of x0 will reverse the ordering of x0 and x1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
            x1 -= x0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
            if (x1 < Integer.MIN_VALUE) x1 = Integer.MIN_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
            if (x0 < Integer.MIN_VALUE) x0 = Integer.MIN_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
            else if (x0 > Integer.MAX_VALUE) x0 = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
        } else { // (x1 >= x0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
            // Clip x0 before we subtract it from x1 in case the clipping
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
            // affects the representable area of the rectangle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
            if (x0 < Integer.MIN_VALUE) x0 = Integer.MIN_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
            else if (x0 > Integer.MAX_VALUE) x0 = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
            x1 -= x0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
            // The only way x1 can be negative now is if we clipped
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
            // x0 against MIN and x1 is less than MIN - in which case
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
            // we want to leave the width negative since the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
            // did not intersect the representable area.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
            if (x1 < Integer.MIN_VALUE) x1 = Integer.MIN_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
            else if (x1 > Integer.MAX_VALUE) x1 = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
        if (y1 < y0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
            // Non-existant in Y direction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
            y1 -= y0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
            if (y1 < Integer.MIN_VALUE) y1 = Integer.MIN_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
            if (y0 < Integer.MIN_VALUE) y0 = Integer.MIN_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
            else if (y0 > Integer.MAX_VALUE) y0 = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
        } else { // (y1 >= y0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
            if (y0 < Integer.MIN_VALUE) y0 = Integer.MIN_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
            else if (y0 > Integer.MAX_VALUE) y0 = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
            y1 -= y0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
            if (y1 < Integer.MIN_VALUE) y1 = Integer.MIN_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
            else if (y1 > Integer.MAX_VALUE) y1 = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
        reshape((int) x0, (int) y0, (int) x1, (int) y1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
    public boolean isEmpty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
        return (width <= 0) || (height <= 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
    public int outcode(double x, double y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
         * Note on casts to double below.  If the arithmetic of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
         * x+w or y+h is done in int, then we may get integer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
         * overflow. By converting to double before the addition
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
         * we force the addition to be carried out in double to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
         * avoid overflow in the comparison.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
         * See bug 4320890 for problems that this can cause.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
        int out = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
        if (this.width <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
            out |= OUT_LEFT | OUT_RIGHT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
        } else if (x < this.x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
            out |= OUT_LEFT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
        } else if (x > this.x + (double) this.width) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
            out |= OUT_RIGHT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
        if (this.height <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
            out |= OUT_TOP | OUT_BOTTOM;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
        } else if (y < this.y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
            out |= OUT_TOP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
        } else if (y > this.y + (double) this.height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
            out |= OUT_BOTTOM;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
        return out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
    public Rectangle2D createIntersection(Rectangle2D r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
        if (r instanceof Rectangle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
            return intersection((Rectangle) r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
        Rectangle2D dest = new Rectangle2D.Double();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
        Rectangle2D.intersect(this, r, dest);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
        return dest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
    public Rectangle2D createUnion(Rectangle2D r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
        if (r instanceof Rectangle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
            return union((Rectangle) r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
        Rectangle2D dest = new Rectangle2D.Double();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
        Rectangle2D.union(this, r, dest);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
        return dest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
     * Checks whether two rectangles are equal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
     * The result is <code>true</code> if and only if the argument is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
     * <code>null</code> and is a <code>Rectangle</code> object that has the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
     * same upper-left corner, width, and height as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
     * this <code>Rectangle</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
     * @param obj the <code>Object</code> to compare with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
     *                this <code>Rectangle</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
     * @return    <code>true</code> if the objects are equal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
     *            <code>false</code> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
        if (obj instanceof Rectangle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
            Rectangle r = (Rectangle)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
            return ((x == r.x) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
                    (y == r.y) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
                    (width == r.width) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
                    (height == r.height));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
        return super.equals(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
     * Returns a <code>String</code> representing this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
     * <code>Rectangle</code> and its values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
     * @return a <code>String</code> representing this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
     *               <code>Rectangle</code> object's coordinate and size values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
        return getClass().getName() + "[x=" + x + ",y=" + y + ",width=" + width + ",height=" + height + "]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
}