jdk/src/share/classes/java/awt/Dimension.java
author martin
Mon, 10 Mar 2008 14:32:51 -0700
changeset 48 dc5744ca15ea
parent 2 90ce3da70b43
child 466 6acd5ec503a8
permissions -rw-r--r--
4960438: (process) Need IO redirection API for subprocesses Reviewed-by: alanb, iris
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1995-2006 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.awt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.geom.Dimension2D;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * The <code>Dimension</code> class encapsulates the width and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * height of a component (in integer precision) in a single object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * The class is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * associated with certain properties of components. Several methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * defined by the <code>Component</code> class and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * <code>LayoutManager</code> interface return a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * <code>Dimension</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * Normally the values of <code>width</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * and <code>height</code> are non-negative integers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * The constructors that allow you to create a dimension do
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * not prevent you from setting a negative value for these properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * If the value of <code>width</code> or <code>height</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * negative, the behavior of some methods defined by other objects is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * undefined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * @author      Sami Shaio
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * @author      Arthur van Hoff
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * @see         java.awt.Component
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * @see         java.awt.LayoutManager
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * @since       1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
public class Dimension extends Dimension2D implements java.io.Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * The width dimension; negative values can be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * @see #getSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * @see #setSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * @since 1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    public int width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * The height dimension; negative values can be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * @see #getSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * @see #setSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * @since 1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    public int height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * JDK 1.1 serialVersionUID
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     private static final long serialVersionUID = 4723952579491349524L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * Initialize JNI field and method IDs
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    private static native void initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        /* ensure that the necessary native libraries are loaded */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        Toolkit.loadLibraries();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        if (!GraphicsEnvironment.isHeadless()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * Creates an instance of <code>Dimension</code> with a width
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * of zero and a height of zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    public Dimension() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        this(0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * Creates an instance of <code>Dimension</code> whose width
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * and height are the same as for the specified dimension.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * @param    d   the specified dimension for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     *               <code>width</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     *               <code>height</code> values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    public Dimension(Dimension d) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        this(d.width, d.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * Constructs a <code>Dimension</code> and initializes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * it to the specified width and specified height.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * @param width the specified width
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * @param height the specified height
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    public Dimension(int width, int height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        this.width = width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        this.height = height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    public double getWidth() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        return width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    public double getHeight() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        return height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * Sets the size of this <code>Dimension</code> object to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * the specified width and height in double precision.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * Note that if <code>width</code> or <code>height</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * are larger than <code>Integer.MAX_VALUE</code>, they will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * be reset to <code>Integer.MAX_VALUE</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * @param width  the new width for the <code>Dimension</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * @param height the new height for the <code>Dimension</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    public void setSize(double width, double height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        this.width = (int) Math.ceil(width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        this.height = (int) Math.ceil(height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * Gets the size of this <code>Dimension</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * This method is included for completeness, to parallel the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * <code>getSize</code> method defined by <code>Component</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * @return   the size of this dimension, a new instance of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     *           <code>Dimension</code> with the same width and height
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * @see      java.awt.Dimension#setSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * @see      java.awt.Component#getSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * @since    1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    public Dimension getSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        return new Dimension(width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * Sets the size of this <code>Dimension</code> object to the specified size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * This method is included for completeness, to parallel the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * <code>setSize</code> method defined by <code>Component</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * @param    d  the new size for this <code>Dimension</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * @see      java.awt.Dimension#getSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * @see      java.awt.Component#setSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * @since    1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    public void setSize(Dimension d) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        setSize(d.width, d.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * Sets the size of this <code>Dimension</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * to the specified width and height.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * This method is included for completeness, to parallel the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * <code>setSize</code> method defined by <code>Component</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * @param    width   the new width for this <code>Dimension</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * @param    height  the new height for this <code>Dimension</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * @see      java.awt.Dimension#getSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * @see      java.awt.Component#setSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * @since    1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    public void setSize(int width, int height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        this.width = width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        this.height = height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * Checks whether two dimension objects have equal values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        if (obj instanceof Dimension) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            Dimension d = (Dimension)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            return (width == d.width) && (height == d.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * Returns the hash code for this <code>Dimension</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * @return    a hash code for this <code>Dimension</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        int sum = width + height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        return sum * (sum + 1)/2 + width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * Returns a string representation of the values of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * <code>Dimension</code> object's <code>height</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * <code>width</code> fields. This method is intended to be used only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * for debugging purposes, and the content and format of the returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * string may vary between implementations. The returned string may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * empty but may not be <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * @return  a string representation of this <code>Dimension</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     *          object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        return getClass().getName() + "[width=" + width + ",height=" + height + "]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
}