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