Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/awt/Dimension.java
38829 views
/*1* Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.awt;2627import java.awt.geom.Dimension2D;28import java.beans.Transient;2930/**31* The <code>Dimension</code> class encapsulates the width and32* height of a component (in integer precision) in a single object.33* The class is34* associated with certain properties of components. Several methods35* defined by the <code>Component</code> class and the36* <code>LayoutManager</code> interface return a37* <code>Dimension</code> object.38* <p>39* Normally the values of <code>width</code>40* and <code>height</code> are non-negative integers.41* The constructors that allow you to create a dimension do42* not prevent you from setting a negative value for these properties.43* If the value of <code>width</code> or <code>height</code> is44* negative, the behavior of some methods defined by other objects is45* undefined.46*47* @author Sami Shaio48* @author Arthur van Hoff49* @see java.awt.Component50* @see java.awt.LayoutManager51* @since 1.052*/53public class Dimension extends Dimension2D implements java.io.Serializable {5455/**56* The width dimension; negative values can be used.57*58* @serial59* @see #getSize60* @see #setSize61* @since 1.062*/63public int width;6465/**66* The height dimension; negative values can be used.67*68* @serial69* @see #getSize70* @see #setSize71* @since 1.072*/73public int height;7475/*76* JDK 1.1 serialVersionUID77*/78private static final long serialVersionUID = 4723952579491349524L;7980/**81* Initialize JNI field and method IDs82*/83private static native void initIDs();8485static {86/* ensure that the necessary native libraries are loaded */87Toolkit.loadLibraries();88if (!GraphicsEnvironment.isHeadless()) {89initIDs();90}91}9293/**94* Creates an instance of <code>Dimension</code> with a width95* of zero and a height of zero.96*/97public Dimension() {98this(0, 0);99}100101/**102* Creates an instance of <code>Dimension</code> whose width103* and height are the same as for the specified dimension.104*105* @param d the specified dimension for the106* <code>width</code> and107* <code>height</code> values108*/109public Dimension(Dimension d) {110this(d.width, d.height);111}112113/**114* Constructs a <code>Dimension</code> and initializes115* it to the specified width and specified height.116*117* @param width the specified width118* @param height the specified height119*/120public Dimension(int width, int height) {121this.width = width;122this.height = height;123}124125/**126* {@inheritDoc}127* @since 1.2128*/129public double getWidth() {130return width;131}132133/**134* {@inheritDoc}135* @since 1.2136*/137public double getHeight() {138return height;139}140141/**142* Sets the size of this <code>Dimension</code> object to143* the specified width and height in double precision.144* Note that if <code>width</code> or <code>height</code>145* are larger than <code>Integer.MAX_VALUE</code>, they will146* be reset to <code>Integer.MAX_VALUE</code>.147*148* @param width the new width for the <code>Dimension</code> object149* @param height the new height for the <code>Dimension</code> object150* @since 1.2151*/152public void setSize(double width, double height) {153this.width = (int) Math.ceil(width);154this.height = (int) Math.ceil(height);155}156157/**158* Gets the size of this <code>Dimension</code> object.159* This method is included for completeness, to parallel the160* <code>getSize</code> method defined by <code>Component</code>.161*162* @return the size of this dimension, a new instance of163* <code>Dimension</code> with the same width and height164* @see java.awt.Dimension#setSize165* @see java.awt.Component#getSize166* @since 1.1167*/168@Transient169public Dimension getSize() {170return new Dimension(width, height);171}172173/**174* Sets the size of this <code>Dimension</code> object to the specified size.175* This method is included for completeness, to parallel the176* <code>setSize</code> method defined by <code>Component</code>.177* @param d the new size for this <code>Dimension</code> object178* @see java.awt.Dimension#getSize179* @see java.awt.Component#setSize180* @since 1.1181*/182public void setSize(Dimension d) {183setSize(d.width, d.height);184}185186/**187* Sets the size of this <code>Dimension</code> object188* to the specified width and height.189* This method is included for completeness, to parallel the190* <code>setSize</code> method defined by <code>Component</code>.191*192* @param width the new width for this <code>Dimension</code> object193* @param height the new height for this <code>Dimension</code> object194* @see java.awt.Dimension#getSize195* @see java.awt.Component#setSize196* @since 1.1197*/198public void setSize(int width, int height) {199this.width = width;200this.height = height;201}202203/**204* Checks whether two dimension objects have equal values.205*/206public boolean equals(Object obj) {207if (obj instanceof Dimension) {208Dimension d = (Dimension)obj;209return (width == d.width) && (height == d.height);210}211return false;212}213214/**215* Returns the hash code for this <code>Dimension</code>.216*217* @return a hash code for this <code>Dimension</code>218*/219public int hashCode() {220int sum = width + height;221return sum * (sum + 1)/2 + width;222}223224/**225* Returns a string representation of the values of this226* <code>Dimension</code> object's <code>height</code> and227* <code>width</code> fields. This method is intended to be used only228* for debugging purposes, and the content and format of the returned229* string may vary between implementations. The returned string may be230* empty but may not be <code>null</code>.231*232* @return a string representation of this <code>Dimension</code>233* object234*/235public String toString() {236return getClass().getName() + "[width=" + width + ",height=" + height + "]";237}238}239240241