Path: blob/master/src/java.desktop/share/classes/java/awt/Dimension.java
41152 views
/*1* Copyright (c) 1995, 2021, 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;29import java.io.Serial;3031/**32* The {@code Dimension} class encapsulates the width and33* height of a component (in integer precision) in a single object.34* The class is35* associated with certain properties of components. Several methods36* defined by the {@code Component} class and the37* {@code LayoutManager} interface return a38* {@code Dimension} object.39* <p>40* Normally the values of {@code width}41* and {@code height} are non-negative integers.42* The constructors that allow you to create a dimension do43* not prevent you from setting a negative value for these properties.44* If the value of {@code width} or {@code height} is45* negative, the behavior of some methods defined by other objects is46* undefined.47*48* @author Sami Shaio49* @author Arthur van Hoff50* @see java.awt.Component51* @see java.awt.LayoutManager52* @since 1.053*/54public class Dimension extends Dimension2D implements java.io.Serializable {5556/**57* The width dimension; negative values can be used.58*59* @serial60* @see #getSize61* @see #setSize62* @since 1.063*/64public int width;6566/**67* The height dimension; negative values can be used.68*69* @serial70* @see #getSize71* @see #setSize72* @since 1.073*/74public int height;7576/**77* Use serialVersionUID from JDK 1.1 for interoperability.78*/79@Serial80private static final long serialVersionUID = 4723952579491349524L;8182/**83* Initialize JNI field and method IDs84*/85private static native void initIDs();8687static {88/* ensure that the necessary native libraries are loaded */89Toolkit.loadLibraries();90if (!GraphicsEnvironment.isHeadless()) {91initIDs();92}93}9495/**96* Creates an instance of {@code Dimension} with a width97* of zero and a height of zero.98*/99public Dimension() {100this(0, 0);101}102103/**104* Creates an instance of {@code Dimension} whose width105* and height are the same as for the specified dimension.106*107* @param d the specified dimension for the108* {@code width} and109* {@code height} values110*/111public Dimension(Dimension d) {112this(d.width, d.height);113}114115/**116* Constructs a {@code Dimension} and initializes117* it to the specified width and specified height.118*119* @param width the specified width120* @param height the specified height121*/122public Dimension(int width, int height) {123this.width = width;124this.height = height;125}126127/**128* {@inheritDoc}129* @since 1.2130*/131public double getWidth() {132return width;133}134135/**136* {@inheritDoc}137* @since 1.2138*/139public double getHeight() {140return height;141}142143/**144* Sets the size of this {@code Dimension} object to145* the specified width and height in double precision.146* Note that if {@code width} or {@code height}147* are larger than {@code Integer.MAX_VALUE}, they will148* be reset to {@code Integer.MAX_VALUE}.149*150* @param width the new width for the {@code Dimension} object151* @param height the new height for the {@code Dimension} object152* @since 1.2153*/154public void setSize(double width, double height) {155this.width = (int) Math.ceil(width);156this.height = (int) Math.ceil(height);157}158159/**160* Gets the size of this {@code Dimension} object.161* This method is included for completeness, to parallel the162* {@code getSize} method defined by {@code Component}.163*164* @return the size of this dimension, a new instance of165* {@code Dimension} with the same width and height166* @see java.awt.Dimension#setSize167* @see java.awt.Component#getSize168* @since 1.1169*/170@Transient171public Dimension getSize() {172return new Dimension(width, height);173}174175/**176* Sets the size of this {@code Dimension} object to the specified size.177* This method is included for completeness, to parallel the178* {@code setSize} method defined by {@code Component}.179* @param d the new size for this {@code Dimension} object180* @see java.awt.Dimension#getSize181* @see java.awt.Component#setSize182* @since 1.1183*/184public void setSize(Dimension d) {185setSize(d.width, d.height);186}187188/**189* Sets the size of this {@code Dimension} object190* to the specified width and height.191* This method is included for completeness, to parallel the192* {@code setSize} method defined by {@code Component}.193*194* @param width the new width for this {@code Dimension} object195* @param height the new height for this {@code Dimension} object196* @see java.awt.Dimension#getSize197* @see java.awt.Component#setSize198* @since 1.1199*/200public void setSize(int width, int height) {201this.width = width;202this.height = height;203}204205/**206* Checks whether two dimension objects have equal values.207*/208public boolean equals(Object obj) {209if (obj instanceof Dimension) {210Dimension d = (Dimension)obj;211return (width == d.width) && (height == d.height);212}213return false;214}215216/**217* Returns the hash code for this {@code Dimension}.218*219* @return a hash code for this {@code Dimension}220*/221public int hashCode() {222int sum = width + height;223return sum * (sum + 1)/2 + width;224}225226/**227* Returns a string representation of the values of this228* {@code Dimension} object's {@code height} and229* {@code width} fields. This method is intended to be used only230* for debugging purposes, and the content and format of the returned231* string may vary between implementations. The returned string may be232* empty but may not be {@code null}.233*234* @return a string representation of this {@code Dimension}235* object236*/237public String toString() {238return getClass().getName() + "[width=" + width + ",height=" + height + "]";239}240}241242243