1/* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17/** 18 * @author Denis M. Kishenko 19 * @version $Revision$ 20 */ 21 22package java.awt; 23 24import java.awt.geom.Dimension2D; 25import java.io.Serializable; 26 27import org.apache.harmony.misc.HashCode; 28 29/** 30 * The Dimension represents the size (width and height) of a component. The 31 * width and height values can be negative, but in that case the behavior of 32 * some methods is unexpected. 33 * 34 * @since Android 1.0 35 */ 36public class Dimension extends Dimension2D implements Serializable { 37 38 /** 39 * The Constant serialVersionUID. 40 */ 41 private static final long serialVersionUID = 4723952579491349524L; 42 43 /** 44 * The width dimension. 45 */ 46 public int width; 47 48 /** 49 * The height dimension. 50 */ 51 public int height; 52 53 /** 54 * Instantiates a new Dimension with the same data as the specified 55 * Dimension. 56 * 57 * @param d 58 * the Dimension to copy the data from when creating the new 59 * Dimension object. 60 */ 61 public Dimension(Dimension d) { 62 this(d.width, d.height); 63 } 64 65 /** 66 * Instantiates a new Dimension with zero width and height. 67 */ 68 public Dimension() { 69 this(0, 0); 70 } 71 72 /** 73 * Instantiates a new Dimension with the specified width and height. 74 * 75 * @param width 76 * the width of the new Dimension. 77 * @param height 78 * the height of the new Dimension. 79 */ 80 public Dimension(int width, int height) { 81 setSize(width, height); 82 } 83 84 /** 85 * Returns the hash code of the Dimension. 86 * 87 * @return the hash code of the Dimension. 88 */ 89 @Override 90 public int hashCode() { 91 HashCode hash = new HashCode(); 92 hash.append(width); 93 hash.append(height); 94 return hash.hashCode(); 95 } 96 97 /** 98 * Compares this Dimension object with the specified object. 99 * 100 * @param obj 101 * the Object to be compared. 102 * @return true, if the specified Object is a Dimension with the same width 103 * and height data as this Dimension. 104 */ 105 @Override 106 public boolean equals(Object obj) { 107 if (obj == this) { 108 return true; 109 } 110 if (obj instanceof Dimension) { 111 Dimension d = (Dimension)obj; 112 return (d.width == width && d.height == height); 113 } 114 return false; 115 } 116 117 /** 118 * Returns the String associated to this Dimension object. 119 * 120 * @return the String associated to this Dimension object. 121 */ 122 @Override 123 public String toString() { 124 // The output format based on 1.5 release behaviour. It could be 125 // obtained in the following way 126 // System.out.println(new Dimension().toString()) 127 return getClass().getName() + "[width=" + width + ",height=" + height + "]"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ 128 } 129 130 /** 131 * Sets the size of this Dimension object with the specified width and 132 * height. 133 * 134 * @param width 135 * the width of the Dimension. 136 * @param height 137 * the height of the Dimension. 138 */ 139 public void setSize(int width, int height) { 140 this.width = width; 141 this.height = height; 142 } 143 144 /** 145 * Sets the size of this Dimension object by copying the data from the 146 * specified Dimension object. 147 * 148 * @param d 149 * the Dimension that gives the new size values. 150 */ 151 public void setSize(Dimension d) { 152 setSize(d.width, d.height); 153 } 154 155 /** 156 * Sets the size of this Dimension object with the specified double width 157 * and height. 158 * 159 * @param width 160 * the width of the Dimension. 161 * @param height 162 * the height of the Dimension. 163 * @see java.awt.geom.Dimension2D#setSize(double, double) 164 */ 165 @Override 166 public void setSize(double width, double height) { 167 setSize((int)Math.ceil(width), (int)Math.ceil(height)); 168 } 169 170 /** 171 * Gets the size of the Dimension. 172 * 173 * @return the size of the Dimension. 174 */ 175 public Dimension getSize() { 176 return new Dimension(width, height); 177 } 178 179 /** 180 * Gets the height of the Dimension. 181 * 182 * @return the height of the Dimension. 183 * @see java.awt.geom.Dimension2D#getHeight() 184 */ 185 @Override 186 public double getHeight() { 187 return height; 188 } 189 190 /** 191 * Gets the width of the Dimension. 192 * 193 * @return the width of the Dimension. 194 * @see java.awt.geom.Dimension2D#getWidth() 195 */ 196 @Override 197 public double getWidth() { 198 return width; 199 } 200 201} 202