001: /*
002: * Copyright (C) 2005, 2006 data2c GmbH (www.data2c.com)
003: *
004: * Author: Wolfgang S. Kechel - wolfgang.kechel@data2c.com
005: *
006: * J2ME version of java.awt.Dimension.
007: */
008: //#ifndef j2se
009: package org.awt;
010:
011: import org.awt.geom.Dimension2D;
012:
013: /**
014: * The <code>Dimension</code> class encapsulates the width and
015: * height of a component (in integer precision) in a single object.
016: * The class is
017: * associated with certain properties of components. Several methods
018: * defined by the <code>Component</code> class and the
019: * <code>LayoutManager</code> interface return a
020: * <code>Dimension</code> object.
021: * <p>
022: * Normally the values of <code>width</code>
023: * and <code>height</code> are non-negative integers.
024: * The constructors that allow you to create a dimension do
025: * not prevent you from setting a negative value for these properties.
026: * If the value of <code>width</code> or <code>height</code> is
027: * negative, the behavior of some methods defined by other objects is
028: * undefined.
029: */
030: public class Dimension extends Dimension2D /*implements java.io.Serializable*/{
031:
032: /**
033: * The width dimension; negative values can be used.
034: *
035: * @serial
036: * @see #getSize
037: * @see #setSize
038: */
039: public int width;
040:
041: /**
042: * The height dimension; negative values can be used.
043: *
044: * @serial
045: * @see #getSize
046: * @see #setSize
047: */
048: public int height;
049:
050: /**
051: * Creates an instance of <code>Dimension</code> with a width
052: * of zero and a height of zero.
053: */
054: public Dimension() {
055: this (0, 0);
056: }
057:
058: /**
059: * Creates an instance of <code>Dimension</code> whose width
060: * and height are the same as for the specified dimension.
061: *
062: * @param d the specified dimension for the
063: * <code>width</code> and
064: * <code>height</code> values
065: */
066: public Dimension(Dimension d) {
067: this (d.width, d.height);
068: }
069:
070: /**
071: * Constructs a <code>Dimension</code> and initializes
072: * it to the specified width and specified height.
073: *
074: * @param width the specified width
075: * @param height the specified height
076: */
077: public Dimension(int width, int height) {
078: this .width = width;
079: this .height = height;
080: }
081:
082: /**
083: * Returns the width of this dimension in double precision.
084: * @return the width of this dimension in double precision
085: */
086: public double getWidth() {
087: return width;
088: }
089:
090: /**
091: * Returns the height of this dimension in double precision.
092: * @return the height of this dimension in double precision
093: */
094: public double getHeight() {
095: return height;
096: }
097:
098: /**
099: * Sets the size of this <code>Dimension</code> object to
100: * the specified width and height in double precision.
101: * Note that if <code>width</code> or <code>height</code>
102: * are larger than <code>Integer.MAX_VALUE</code>, they will
103: * be reset to <code>Integer.MAX_VALUE</code>.
104: *
105: * @param width the new width for the <code>Dimension</code> object
106: * @param height the new height for the <code>Dimension</code> object
107: */
108: public void setSize(double width, double height) {
109: this .width = (int) Math.ceil(width);
110: this .height = (int) Math.ceil(height);
111: }
112:
113: /**
114: * Gets the size of this <code>Dimension</code> object.
115: * This method is included for completeness, to parallel the
116: * <code>getSize</code> method defined by <code>Component</code>.
117: *
118: * @return the size of this dimension, a new instance of
119: * <code>Dimension</code> with the same width and height
120: * @see java.awt.Dimension#setSize
121: * @see java.awt.Component#getSize
122: */
123: public Dimension getSize() {
124: return new Dimension(width, height);
125: }
126:
127: /**
128: * Sets the size of this <code>Dimension</code> object to the specified size.
129: * This method is included for completeness, to parallel the
130: * <code>setSize</code> method defined by <code>Component</code>.
131: * @param d the new size for this <code>Dimension</code> object
132: * @see java.awt.Dimension#getSize
133: * @see java.awt.Component#setSize
134: */
135: public void setSize(Dimension d) {
136: setSize(d.width, d.height);
137: }
138:
139: /**
140: * Sets the size of this <code>Dimension</code> object
141: * to the specified width and height.
142: * This method is included for completeness, to parallel the
143: * <code>setSize</code> method defined by <code>Component</code>.
144: *
145: * @param width the new width for this <code>Dimension</code> object
146: * @param height the new height for this <code>Dimension</code> object
147: * @see java.awt.Dimension#getSize
148: * @see java.awt.Component#setSize
149: */
150: public void setSize(int width, int height) {
151: this .width = width;
152: this .height = height;
153: }
154:
155: /**
156: * Checks whether two dimension objects have equal values.
157: */
158: public boolean equals(Object obj) {
159: if (obj instanceof Dimension) {
160: Dimension d = (Dimension) obj;
161: return (width == d.width) && (height == d.height);
162: }
163: return false;
164: }
165:
166: /**
167: * Returns the hash code for this <code>Dimension</code>.
168: *
169: * @return a hash code for this <code>Dimension</code>
170: */
171: public int hashCode() {
172: int sum = width + height;
173: return sum * (sum + 1) / 2 + width;
174: }
175:
176: /**
177: * Returns a string representation of the values of this
178: * <code>Dimension</code> object's <code>height</code> and
179: * <code>width</code> fields. This method is intended to be used only
180: * for debugging purposes, and the content and format of the returned
181: * string may vary between implementations. The returned string may be
182: * empty but may not be <code>null</code>.
183: *
184: * @return a string representation of this <code>Dimension</code>
185: * object
186: */
187: public String toString() {
188: return "Dimension[w=" + width + ", h=" + height + "]";
189: }
190: }
191: //#endif
|