jdk/src/java.desktop/share/classes/java/awt/Rectangle.java
author serb
Wed, 17 Sep 2014 16:14:12 +0400
changeset 26749 b6598aa90114
parent 25859 3317bb8137f4
child 35667 ed476aba94de
permissions -rw-r--r--
8055326: Fix typos in client-related packages Reviewed-by: prr, azvegint, alexsch Contributed-by: pavel.rappo@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
25770
d132697706ea 8037511: Tidy warnings cleanup for java.awt - 2d part
avstepan
parents: 23010
diff changeset
     2
 * Copyright (c) 1995, 2014, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 466
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 466
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 466
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 466
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 466
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.awt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.geom.Rectangle2D;
466
6acd5ec503a8 4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents: 2
diff changeset
    29
import java.beans.Transient;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * A <code>Rectangle</code> specifies an area in a coordinate space that is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * enclosed by the <code>Rectangle</code> object's upper-left point
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * {@code (x,y)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * in the coordinate space, its width, and its height.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * A <code>Rectangle</code> object's <code>width</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * <code>height</code> are <code>public</code> fields. The constructors
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * that create a <code>Rectangle</code>, and the methods that can modify
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * one, do not prevent setting a negative value for width or height.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <a name="Empty">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * A {@code Rectangle} whose width or height is exactly zero has location
25770
d132697706ea 8037511: Tidy warnings cleanup for java.awt - 2d part
avstepan
parents: 23010
diff changeset
    44
 * along those axes with zero dimension, but is otherwise considered empty.</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * The {@link #isEmpty} method will return true for such a {@code Rectangle}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * Methods which test if an empty {@code Rectangle} contains or intersects
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * a point or rectangle will always return false if either dimension is zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * Methods which combine such a {@code Rectangle} with a point or rectangle
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * will include the location of the {@code Rectangle} on that axis in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * result as if the {@link #add(Point)} method were being called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <p>
26749
b6598aa90114 8055326: Fix typos in client-related packages
serb
parents: 25859
diff changeset
    52
 * <a name="NonExistent">
2
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.
26749
b6598aa90114 8055326: Fix typos in client-related packages
serb
parents: 25859
diff changeset
    55
 * Such a {@code Rectangle} is treated as non-existent along those axes.
2
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:
21777
c0a423e43b0d 8025235: [javadoc] fix some errors in 2D
yan
parents: 5506
diff changeset
    78
 * <pre>{@code
2
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
 *     }
21777
c0a423e43b0d 8025235: [javadoc] fix some errors in 2D
yan
parents: 5506
diff changeset
    83
 * }</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * or if we know that the points array contains at least one point:
21777
c0a423e43b0d 8025235: [javadoc] fix some errors in 2D
yan
parents: 5506
diff changeset
    85
 * <pre>{@code
2
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
 *     }
21777
c0a423e43b0d 8025235: [javadoc] fix some errors in 2D
yan
parents: 5506
diff changeset
    90
 * }</pre>
2
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
26749
b6598aa90114 8055326: Fix typos in client-related packages
serb
parents: 25859
diff changeset
   109
 * non-existent along one or both axes, the stored dimensions will be
2
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
     */
466
6acd5ec503a8 4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents: 2
diff changeset
   311
    @Transient
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    public Rectangle getBounds() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        return new Rectangle(x, y, width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    public Rectangle2D getBounds2D() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        return new Rectangle(x, y, width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * Sets the bounding <code>Rectangle</code> of this <code>Rectangle</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * to match the specified <code>Rectangle</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * This method is included for completeness, to parallel the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * <code>setBounds</code> method of <code>Component</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * @param r the specified <code>Rectangle</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * @see       #getBounds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * @see       java.awt.Component#setBounds(java.awt.Rectangle)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * @since     1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    public void setBounds(Rectangle r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        setBounds(r.x, r.y, r.width, r.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * Sets the bounding <code>Rectangle</code> of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * <code>Rectangle</code> to the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * <code>x</code>, <code>y</code>, <code>width</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * and <code>height</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * This method is included for completeness, to parallel the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * <code>setBounds</code> method of <code>Component</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * @param x the new X coordinate for the upper-left
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     *                    corner of this <code>Rectangle</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * @param y the new Y coordinate for the upper-left
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     *                    corner of this <code>Rectangle</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * @param width the new width for this <code>Rectangle</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * @param height the new height for this <code>Rectangle</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * @see       #getBounds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * @see       java.awt.Component#setBounds(int, int, int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * @since     1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    public void setBounds(int x, int y, int width, int height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        reshape(x, y, width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * Sets the bounds of this {@code Rectangle} to the integer bounds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * which encompass the specified {@code x}, {@code y}, {@code width},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * and {@code height}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * If the parameters specify a {@code Rectangle} that exceeds the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * maximum range of integers, the result will be the best
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * representation of the specified {@code Rectangle} intersected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * with the maximum integer bounds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * @param x the X coordinate of the upper-left corner of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     *                  the specified rectangle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * @param y the Y coordinate of the upper-left corner of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     *                  the specified rectangle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * @param width the width of the specified rectangle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * @param height the new height of the specified rectangle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    public void setRect(double x, double y, double width, double height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        int newx, newy, neww, newh;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        if (x > 2.0 * Integer.MAX_VALUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            // Too far in positive X direction to represent...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            // We cannot even reach the left side of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            // rectangle even with both x & width set to MAX_VALUE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            // The intersection with the "maximal integer rectangle"
26749
b6598aa90114 8055326: Fix typos in client-related packages
serb
parents: 25859
diff changeset
   384
            // is non-existent so we should use a width < 0.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            // REMIND: Should we try to determine a more "meaningful"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            // adjusted value for neww than just "-1"?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            newx = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            neww = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            newx = clip(x, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            if (width >= 0) width += x-newx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            neww = clip(width, width >= 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        if (y > 2.0 * Integer.MAX_VALUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            // Too far in positive Y direction to represent...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            newy = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            newh = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            newy = clip(y, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            if (height >= 0) height += y-newy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            newh = clip(height, height >= 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        reshape(newx, newy, neww, newh);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    // Return best integer representation for v, clipped to integer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    // range and floor-ed or ceiling-ed, depending on the boolean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    private static int clip(double v, boolean doceil) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        if (v <= Integer.MIN_VALUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            return Integer.MIN_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        if (v >= Integer.MAX_VALUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            return Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        return (int) (doceil ? Math.ceil(v) : Math.floor(v));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * Sets the bounding <code>Rectangle</code> of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * <code>Rectangle</code> to the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * <code>x</code>, <code>y</code>, <code>width</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * and <code>height</code>.
25770
d132697706ea 8037511: Tidy warnings cleanup for java.awt - 2d part
avstepan
parents: 23010
diff changeset
   424
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * @param x the new X coordinate for the upper-left
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     *                    corner of this <code>Rectangle</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * @param y the new Y coordinate for the upper-left
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     *                    corner of this <code>Rectangle</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * @param width the new width for this <code>Rectangle</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * @param height the new height for this <code>Rectangle</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * @deprecated As of JDK version 1.1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * replaced by <code>setBounds(int, int, int, int)</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    public void reshape(int x, int y, int width, int height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        this.x = x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        this.y = y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        this.width = width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        this.height = height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * Returns the location of this <code>Rectangle</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * This method is included for completeness, to parallel the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * <code>getLocation</code> method of <code>Component</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * @return the <code>Point</code> that is the upper-left corner of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     *                  this <code>Rectangle</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * @see       java.awt.Component#getLocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * @see       #setLocation(Point)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * @see       #setLocation(int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * @since     1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    public Point getLocation() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        return new Point(x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     * Moves this <code>Rectangle</code> to the specified location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     * This method is included for completeness, to parallel the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * <code>setLocation</code> method of <code>Component</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * @param p the <code>Point</code> specifying the new location
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     *                for this <code>Rectangle</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     * @see       java.awt.Component#setLocation(java.awt.Point)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * @see       #getLocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * @since     1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    public void setLocation(Point p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        setLocation(p.x, p.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * Moves this <code>Rectangle</code> to the specified location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * This method is included for completeness, to parallel the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * <code>setLocation</code> method of <code>Component</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * @param x the X coordinate of the new location
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * @param y the Y coordinate of the new location
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * @see       #getLocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     * @see       java.awt.Component#setLocation(int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * @since     1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    public void setLocation(int x, int y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        move(x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * Moves this <code>Rectangle</code> to the specified location.
25770
d132697706ea 8037511: Tidy warnings cleanup for java.awt - 2d part
avstepan
parents: 23010
diff changeset
   490
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * @param x the X coordinate of the new location
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * @param y the Y coordinate of the new location
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * @deprecated As of JDK version 1.1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * replaced by <code>setLocation(int, int)</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
    public void move(int x, int y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        this.x = x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        this.y = y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     * Translates this <code>Rectangle</code> the indicated distance,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     * to the right along the X coordinate axis, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * downward along the Y coordinate axis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * @param dx the distance to move this <code>Rectangle</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     *                 along the X axis
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * @param dy the distance to move this <code>Rectangle</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     *                 along the Y axis
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * @see       java.awt.Rectangle#setLocation(int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * @see       java.awt.Rectangle#setLocation(java.awt.Point)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    public void translate(int dx, int dy) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        int oldv = this.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        int newv = oldv + dx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        if (dx < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
            // moving leftward
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
            if (newv > oldv) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                // negative overflow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                // Only adjust width if it was valid (>= 0).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                if (width >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                    // The right edge is now conceptually at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                    // newv+width, but we may move newv to prevent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                    // overflow.  But we want the right edge to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                    // remain at its new location in spite of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                    // clipping.  Think of the following adjustment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                    // conceptually the same as:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                    // width += newv; newv = MIN_VALUE; width -= newv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                    width += newv - Integer.MIN_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                    // width may go negative if the right edge went past
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                    // MIN_VALUE, but it cannot overflow since it cannot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                    // have moved more than MIN_VALUE and any non-negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                    // number + MIN_VALUE does not overflow.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                newv = Integer.MIN_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            // moving rightward (or staying still)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            if (newv < oldv) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                // positive overflow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                if (width >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                    // Conceptually the same as:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                    // width += newv; newv = MAX_VALUE; width -= newv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                    width += newv - Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                    // With large widths and large displacements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                    // we may overflow so we need to check it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                    if (width < 0) width = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                newv = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        this.x = newv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        oldv = this.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        newv = oldv + dy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        if (dy < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            // moving upward
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
            if (newv > oldv) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                // negative overflow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                if (height >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                    height += newv - Integer.MIN_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                    // See above comment about no overflow in this case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                newv = Integer.MIN_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
            // moving downward (or staying still)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
            if (newv < oldv) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                // positive overflow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                if (height >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                    height += newv - Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                    if (height < 0) height = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                newv = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        this.y = newv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * Gets the size of this <code>Rectangle</code>, represented by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * the returned <code>Dimension</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * This method is included for completeness, to parallel the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     * <code>getSize</code> method of <code>Component</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * @return a <code>Dimension</code>, representing the size of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     *            this <code>Rectangle</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     * @see       java.awt.Component#getSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     * @see       #setSize(Dimension)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     * @see       #setSize(int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     * @since     1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
    public Dimension getSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        return new Dimension(width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     * Sets the size of this <code>Rectangle</code> to match the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * specified <code>Dimension</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * This method is included for completeness, to parallel the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     * <code>setSize</code> method of <code>Component</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     * @param d the new size for the <code>Dimension</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     * @see       java.awt.Component#setSize(java.awt.Dimension)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     * @see       #getSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     * @since     1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
    public void setSize(Dimension d) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        setSize(d.width, d.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     * Sets the size of this <code>Rectangle</code> to the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     * width and height.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     * This method is included for completeness, to parallel the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     * <code>setSize</code> method of <code>Component</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     * @param width the new width for this <code>Rectangle</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     * @param height the new height for this <code>Rectangle</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     * @see       java.awt.Component#setSize(int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     * @see       #getSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     * @since     1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
    public void setSize(int width, int height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        resize(width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     * Sets the size of this <code>Rectangle</code> to the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * width and height.
25770
d132697706ea 8037511: Tidy warnings cleanup for java.awt - 2d part
avstepan
parents: 23010
diff changeset
   631
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     * @param width the new width for this <code>Rectangle</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     * @param height the new height for this <code>Rectangle</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     * @deprecated As of JDK version 1.1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     * replaced by <code>setSize(int, int)</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
    public void resize(int width, int height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        this.width = width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        this.height = height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     * Checks whether or not this <code>Rectangle</code> contains the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     * specified <code>Point</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     * @param p the <code>Point</code> to test
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     * @return    <code>true</code> if the specified <code>Point</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     *            is inside this <code>Rectangle</code>;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     *            <code>false</code> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     * @since     1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
    public boolean contains(Point p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        return contains(p.x, p.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     * Checks whether or not this <code>Rectangle</code> contains the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     * point at the specified location {@code (x,y)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     * @param  x the specified X coordinate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     * @param  y the specified Y coordinate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     * @return    <code>true</code> if the point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     *            {@code (x,y)} is inside this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     *            <code>Rectangle</code>;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     *            <code>false</code> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     * @since     1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
    public boolean contains(int x, int y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        return inside(x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     * Checks whether or not this <code>Rectangle</code> entirely contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     * the specified <code>Rectangle</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     * @param     r   the specified <code>Rectangle</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     * @return    <code>true</code> if the <code>Rectangle</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     *            is contained entirely inside this <code>Rectangle</code>;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     *            <code>false</code> otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     * @since     1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
    public boolean contains(Rectangle r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        return contains(r.x, r.y, r.width, r.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     * Checks whether this <code>Rectangle</code> entirely contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     * the <code>Rectangle</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     * at the specified location {@code (X,Y)} with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     * specified dimensions {@code (W,H)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     * @param     X the specified X coordinate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     * @param     Y the specified Y coordinate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     * @param     W   the width of the <code>Rectangle</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     * @param     H   the height of the <code>Rectangle</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     * @return    <code>true</code> if the <code>Rectangle</code> specified by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     *            {@code (X, Y, W, H)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     *            is entirely enclosed inside this <code>Rectangle</code>;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
     *            <code>false</code> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
     * @since     1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
    public boolean contains(int X, int Y, int W, int H) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
        int w = this.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        int h = this.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        if ((w | h | W | H) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
            // At least one of the dimensions is negative...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        // Note: if any dimension is zero, tests below must return false...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        int x = this.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        int y = this.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        if (X < x || Y < y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
        w += x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
        W += X;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
        if (W <= X) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
            // X+W overflowed or W was zero, return false if...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
            // either original w or W was zero or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
            // x+w did not overflow or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
            // the overflowed x+w is smaller than the overflowed X+W
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
            if (w >= x || W > w) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
            // X+W did not overflow and W was not zero, return false if...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
            // original w was zero or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
            // x+w did not overflow and x+w is smaller than X+W
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
            if (w >= x && W > w) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        h += y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        H += Y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        if (H <= Y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
            if (h >= y || H > h) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
            if (h >= y && H > h) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
     * Checks whether or not this <code>Rectangle</code> contains the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
     * point at the specified location {@code (X,Y)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
     * @param  X the specified X coordinate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
     * @param  Y the specified Y coordinate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
     * @return    <code>true</code> if the point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
     *            {@code (X,Y)} is inside this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
     *            <code>Rectangle</code>;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
     *            <code>false</code> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
     * @deprecated As of JDK version 1.1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
     * replaced by <code>contains(int, int)</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
    public boolean inside(int X, int Y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
        int w = this.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
        int h = this.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        if ((w | h) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
            // At least one of the dimensions is negative...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        // Note: if either dimension is zero, tests below must return false...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
        int x = this.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
        int y = this.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
        if (X < x || Y < y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
        w += x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
        h += y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
        //    overflow || intersect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
        return ((w < x || w > X) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
                (h < y || h > Y));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
     * Determines whether or not this <code>Rectangle</code> and the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
     * <code>Rectangle</code> intersect. Two rectangles intersect if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
     * their intersection is nonempty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
     * @param r the specified <code>Rectangle</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
     * @return    <code>true</code> if the specified <code>Rectangle</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
     *            and this <code>Rectangle</code> intersect;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
     *            <code>false</code> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
    public boolean intersects(Rectangle r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
        int tw = this.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
        int th = this.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
        int rw = r.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
        int rh = r.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
        if (rw <= 0 || rh <= 0 || tw <= 0 || th <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
        int tx = this.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
        int ty = this.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
        int rx = r.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
        int ry = r.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
        rw += rx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
        rh += ry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
        tw += tx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
        th += ty;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
        //      overflow || intersect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
        return ((rw < rx || rw > tx) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
                (rh < ry || rh > ty) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
                (tw < tx || tw > rx) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
                (th < ty || th > ry));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     * Computes the intersection of this <code>Rectangle</code> with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     * specified <code>Rectangle</code>. Returns a new <code>Rectangle</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     * that represents the intersection of the two rectangles.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
     * If the two rectangles do not intersect, the result will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
     * an empty rectangle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
     * @param     r   the specified <code>Rectangle</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
     * @return    the largest <code>Rectangle</code> contained in both the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
     *            specified <code>Rectangle</code> and in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
     *            this <code>Rectangle</code>; or if the rectangles
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
     *            do not intersect, an empty rectangle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
    public Rectangle intersection(Rectangle r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
        int tx1 = this.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
        int ty1 = this.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
        int rx1 = r.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
        int ry1 = r.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
        long tx2 = tx1; tx2 += this.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
        long ty2 = ty1; ty2 += this.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
        long rx2 = rx1; rx2 += r.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
        long ry2 = ry1; ry2 += r.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
        if (tx1 < rx1) tx1 = rx1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
        if (ty1 < ry1) ty1 = ry1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
        if (tx2 > rx2) tx2 = rx2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
        if (ty2 > ry2) ty2 = ry2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
        tx2 -= tx1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        ty2 -= ty1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
        // tx2,ty2 will never overflow (they will never be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
        // larger than the smallest of the two source w,h)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
        // they might underflow, though...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
        if (tx2 < Integer.MIN_VALUE) tx2 = Integer.MIN_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
        if (ty2 < Integer.MIN_VALUE) ty2 = Integer.MIN_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
        return new Rectangle(tx1, ty1, (int) tx2, (int) ty2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
     * Computes the union of this <code>Rectangle</code> with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
     * specified <code>Rectangle</code>. Returns a new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
     * <code>Rectangle</code> that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
     * represents the union of the two rectangles.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
     * If either {@code Rectangle} has any dimension less than zero
26749
b6598aa90114 8055326: Fix typos in client-related packages
serb
parents: 25859
diff changeset
   848
     * the rules for <a href=#NonExistent>non-existent</a> rectangles
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
     * apply.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
     * If only one has a dimension less than zero, then the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
     * will be a copy of the other {@code Rectangle}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
     * If both have dimension less than zero, then the result will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
     * have at least one dimension less than zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
     * If the resulting {@code Rectangle} would have a dimension
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
     * too large to be expressed as an {@code int}, the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
     * will have a dimension of {@code Integer.MAX_VALUE} along
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
     * that dimension.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
     * @param r the specified <code>Rectangle</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
     * @return    the smallest <code>Rectangle</code> containing both
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
     *            the specified <code>Rectangle</code> and this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
     *            <code>Rectangle</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
    public Rectangle union(Rectangle r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
        long tx2 = this.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
        long ty2 = this.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
        if ((tx2 | ty2) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
            // This rectangle has negative dimensions...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
            // If r has non-negative dimensions then it is the answer.
26749
b6598aa90114 8055326: Fix typos in client-related packages
serb
parents: 25859
diff changeset
   870
            // If r is non-existent (has a negative dimension), then both
b6598aa90114 8055326: Fix typos in client-related packages
serb
parents: 25859
diff changeset
   871
            // are non-existent and we can return any non-existent rectangle
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
            // as an answer.  Thus, returning r meets that criterion.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
            // Either way, r is our answer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
            return new Rectangle(r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
        long rx2 = r.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
        long ry2 = r.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
        if ((rx2 | ry2) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
            return new Rectangle(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
        int tx1 = this.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
        int ty1 = this.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
        tx2 += tx1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
        ty2 += ty1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
        int rx1 = r.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
        int ry1 = r.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
        rx2 += rx1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
        ry2 += ry1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
        if (tx1 > rx1) tx1 = rx1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
        if (ty1 > ry1) ty1 = ry1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
        if (tx2 < rx2) tx2 = rx2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
        if (ty2 < ry2) ty2 = ry2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
        tx2 -= tx1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
        ty2 -= ty1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
        // tx2,ty2 will never underflow since both original rectangles
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
        // were already proven to be non-empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
        // they might overflow, though...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
        if (tx2 > Integer.MAX_VALUE) tx2 = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
        if (ty2 > Integer.MAX_VALUE) ty2 = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
        return new Rectangle(tx1, ty1, (int) tx2, (int) ty2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
     * Adds a point, specified by the integer arguments {@code newx,newy}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
     * to the bounds of this {@code Rectangle}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
     * If this {@code Rectangle} has any dimension less than zero,
26749
b6598aa90114 8055326: Fix typos in client-related packages
serb
parents: 25859
diff changeset
   908
     * the rules for <a href=#NonExistent>non-existent</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
     * rectangles apply.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
     * In that case, the new bounds of this {@code Rectangle} will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
     * have a location equal to the specified coordinates and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
     * width and height equal to zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
     * After adding a point, a call to <code>contains</code> with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
     * added point as an argument does not necessarily return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
     * <code>true</code>. The <code>contains</code> method does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
     * return <code>true</code> for points on the right or bottom
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
     * edges of a <code>Rectangle</code>. Therefore, if the added point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
     * falls on the right or bottom edge of the enlarged
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
     * <code>Rectangle</code>, <code>contains</code> returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
     * <code>false</code> for that point.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
     * If the specified point must be contained within the new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
     * {@code Rectangle}, a 1x1 rectangle should be added instead:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
     *     r.add(newx, newy, 1, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
     * @param newx the X coordinate of the new point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
     * @param newy the Y coordinate of the new point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
    public void add(int newx, int newy) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
        if ((width | height) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
            this.x = newx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
            this.y = newy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
            this.width = this.height = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
        int x1 = this.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
        int y1 = this.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
        long x2 = this.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
        long y2 = this.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
        x2 += x1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
        y2 += y1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
        if (x1 > newx) x1 = newx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
        if (y1 > newy) y1 = newy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
        if (x2 < newx) x2 = newx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
        if (y2 < newy) y2 = newy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
        x2 -= x1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
        y2 -= y1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
        if (x2 > Integer.MAX_VALUE) x2 = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
        if (y2 > Integer.MAX_VALUE) y2 = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
        reshape(x1, y1, (int) x2, (int) y2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
     * Adds the specified {@code Point} to the bounds of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
     * {@code Rectangle}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
     * If this {@code Rectangle} has any dimension less than zero,
26749
b6598aa90114 8055326: Fix typos in client-related packages
serb
parents: 25859
diff changeset
   959
     * the rules for <a href=#NonExistent>non-existent</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
     * rectangles apply.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
     * In that case, the new bounds of this {@code Rectangle} will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
     * have a location equal to the coordinates of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
     * {@code Point} and width and height equal to zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
     * After adding a <code>Point</code>, a call to <code>contains</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
     * with the added <code>Point</code> as an argument does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
     * necessarily return <code>true</code>. The <code>contains</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
     * method does not return <code>true</code> for points on the right
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
     * or bottom edges of a <code>Rectangle</code>. Therefore if the added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
     * <code>Point</code> falls on the right or bottom edge of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
     * enlarged <code>Rectangle</code>, <code>contains</code> returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
     * <code>false</code> for that <code>Point</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
     * If the specified point must be contained within the new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
     * {@code Rectangle}, a 1x1 rectangle should be added instead:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
     *     r.add(pt.x, pt.y, 1, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
     * @param pt the new <code>Point</code> to add to this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
     *           <code>Rectangle</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
    public void add(Point pt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
        add(pt.x, pt.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
     * Adds a <code>Rectangle</code> to this <code>Rectangle</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
     * The resulting <code>Rectangle</code> is the union of the two
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
     * rectangles.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
     * If either {@code Rectangle} has any dimension less than 0, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
     * result will have the dimensions of the other {@code Rectangle}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
     * If both {@code Rectangle}s have at least one dimension less
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
     * than 0, the result will have at least one dimension less than 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
     * If either {@code Rectangle} has one or both dimensions equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
     * to 0, the result along those axes with 0 dimensions will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
     * equivalent to the results obtained by adding the corresponding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
     * origin coordinate to the result rectangle along that axis,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
     * similar to the operation of the {@link #add(Point)} method,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
     * but contribute no further dimension beyond that.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
     * If the resulting {@code Rectangle} would have a dimension
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
     * too large to be expressed as an {@code int}, the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
     * will have a dimension of {@code Integer.MAX_VALUE} along
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
     * that dimension.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
     * @param  r the specified <code>Rectangle</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
    public void add(Rectangle r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
        long tx2 = this.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
        long ty2 = this.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
        if ((tx2 | ty2) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
            reshape(r.x, r.y, r.width, r.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
        long rx2 = r.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
        long ry2 = r.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
        if ((rx2 | ry2) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
        int tx1 = this.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
        int ty1 = this.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
        tx2 += tx1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
        ty2 += ty1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
        int rx1 = r.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
        int ry1 = r.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
        rx2 += rx1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
        ry2 += ry1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
        if (tx1 > rx1) tx1 = rx1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
        if (ty1 > ry1) ty1 = ry1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
        if (tx2 < rx2) tx2 = rx2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
        if (ty2 < ry2) ty2 = ry2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
        tx2 -= tx1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
        ty2 -= ty1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
        // tx2,ty2 will never underflow since both original
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
        // rectangles were non-empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
        // they might overflow, though...
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
        if (tx2 > Integer.MAX_VALUE) tx2 = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
        if (ty2 > Integer.MAX_VALUE) ty2 = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
        reshape(tx1, ty1, (int) tx2, (int) ty2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
     * Resizes the <code>Rectangle</code> both horizontally and vertically.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
     * This method modifies the <code>Rectangle</code> so that it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
     * <code>h</code> units larger on both the left and right side,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
     * and <code>v</code> units larger at both the top and bottom.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
     * The new <code>Rectangle</code> has {@code (x - h, y - v)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
     * as its upper-left corner,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
     * width of {@code (width + 2h)},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
     * and a height of {@code (height + 2v)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
     * If negative values are supplied for <code>h</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
     * <code>v</code>, the size of the <code>Rectangle</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
     * decreases accordingly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
     * The {@code grow} method will check for integer overflow
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
     * and underflow, but does not check whether the resulting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
     * values of {@code width} and {@code height} grow
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
     * from negative to non-negative or shrink from non-negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
     * to negative.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
     * @param h the horizontal expansion
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
     * @param v the vertical expansion
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
    public void grow(int h, int v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
        long x0 = this.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
        long y0 = this.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
        long x1 = this.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
        long y1 = this.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
        x1 += x0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
        y1 += y0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
        x0 -= h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
        y0 -= v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
        x1 += h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
        y1 += v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
        if (x1 < x0) {
26749
b6598aa90114 8055326: Fix typos in client-related packages
serb
parents: 25859
diff changeset
  1078
            // Non-existent in X direction
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
            // Final width must remain negative so subtract x0 before
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
            // it is clipped so that we avoid the risk that the clipping
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
            // of x0 will reverse the ordering of x0 and x1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
            x1 -= x0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
            if (x1 < Integer.MIN_VALUE) x1 = Integer.MIN_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
            if (x0 < Integer.MIN_VALUE) x0 = Integer.MIN_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
            else if (x0 > Integer.MAX_VALUE) x0 = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
        } else { // (x1 >= x0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
            // Clip x0 before we subtract it from x1 in case the clipping
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
            // affects the representable area of the rectangle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
            if (x0 < Integer.MIN_VALUE) x0 = Integer.MIN_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
            else if (x0 > Integer.MAX_VALUE) x0 = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
            x1 -= x0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
            // The only way x1 can be negative now is if we clipped
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
            // x0 against MIN and x1 is less than MIN - in which case
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
            // we want to leave the width negative since the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
            // did not intersect the representable area.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
            if (x1 < Integer.MIN_VALUE) x1 = Integer.MIN_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
            else if (x1 > Integer.MAX_VALUE) x1 = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
        if (y1 < y0) {
26749
b6598aa90114 8055326: Fix typos in client-related packages
serb
parents: 25859
diff changeset
  1101
            // Non-existent in Y direction
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
            y1 -= y0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
            if (y1 < Integer.MIN_VALUE) y1 = Integer.MIN_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
            if (y0 < Integer.MIN_VALUE) y0 = Integer.MIN_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
            else if (y0 > Integer.MAX_VALUE) y0 = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
        } else { // (y1 >= y0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
            if (y0 < Integer.MIN_VALUE) y0 = Integer.MIN_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
            else if (y0 > Integer.MAX_VALUE) y0 = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
            y1 -= y0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
            if (y1 < Integer.MIN_VALUE) y1 = Integer.MIN_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
            else if (y1 > Integer.MAX_VALUE) y1 = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
        reshape((int) x0, (int) y0, (int) x1, (int) y1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
    public boolean isEmpty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
        return (width <= 0) || (height <= 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
    public int outcode(double x, double y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
         * Note on casts to double below.  If the arithmetic of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
         * x+w or y+h is done in int, then we may get integer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
         * overflow. By converting to double before the addition
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
         * we force the addition to be carried out in double to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
         * avoid overflow in the comparison.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
         * See bug 4320890 for problems that this can cause.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
        int out = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
        if (this.width <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
            out |= OUT_LEFT | OUT_RIGHT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
        } else if (x < this.x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
            out |= OUT_LEFT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
        } else if (x > this.x + (double) this.width) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
            out |= OUT_RIGHT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
        if (this.height <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
            out |= OUT_TOP | OUT_BOTTOM;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
        } else if (y < this.y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
            out |= OUT_TOP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
        } else if (y > this.y + (double) this.height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
            out |= OUT_BOTTOM;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
        return out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
    public Rectangle2D createIntersection(Rectangle2D r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
        if (r instanceof Rectangle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
            return intersection((Rectangle) r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
        Rectangle2D dest = new Rectangle2D.Double();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
        Rectangle2D.intersect(this, r, dest);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
        return dest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
    public Rectangle2D createUnion(Rectangle2D r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
        if (r instanceof Rectangle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
            return union((Rectangle) r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
        Rectangle2D dest = new Rectangle2D.Double();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
        Rectangle2D.union(this, r, dest);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
        return dest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
     * Checks whether two rectangles are equal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
     * The result is <code>true</code> if and only if the argument is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
     * <code>null</code> and is a <code>Rectangle</code> object that has the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
     * same upper-left corner, width, and height as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
     * this <code>Rectangle</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
     * @param obj the <code>Object</code> to compare with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
     *                this <code>Rectangle</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
     * @return    <code>true</code> if the objects are equal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
     *            <code>false</code> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
        if (obj instanceof Rectangle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
            Rectangle r = (Rectangle)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
            return ((x == r.x) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
                    (y == r.y) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
                    (width == r.width) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
                    (height == r.height));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
        return super.equals(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
     * Returns a <code>String</code> representing this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
     * <code>Rectangle</code> and its values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
     * @return a <code>String</code> representing this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
     *               <code>Rectangle</code> object's coordinate and size values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
        return getClass().getName() + "[x=" + x + ",y=" + y + ",width=" + width + ",height=" + height + "]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
}