using System; namespace System.Drawing { /// /// The Dimension class encapsulates the width and /// height of a component (in int precision) in a single object. /// /// /// The class is /// associated with certain properties of components. Several methods /// defined by the Component class and the /// LayoutManager interface return a Dimension object. ///

/// Normally the values of width /// and height are non-negative ints. /// The constructors that allow you to create a dimension do /// not prevent you from setting a negative value for these properties. /// If the value of width or height is /// negative, the behavior of some methods defined by other objects is /// undefined. /// public class Dimension : Dimension2D { ///

/// The width dimension. Negative values can be used. /// public int width; /// /// The height dimension. Negative values can be used. /// public int height; /// /// Creates an instance of Dimension with a width /// of zero and a height of zero. /// public Dimension() : this(0, 0) {} /// /// Creates an instance of Dimension whose width /// and height are the same as for the specified dimension. /// /// /// the specified dimension for the /// width and /// height values. /// public Dimension(Dimension d) : this(d.width, d.height) {} /// /// Constructs a Dimension and initializes it to the specified width and /// specified height. /// /// the specified width dimension /// the specified height dimension public Dimension(int width, int height) { this.width = width; this.height = height; } /// /// Returns the width of this dimension in double precision. /// /// the width public override double Width { get { return width; } } /// /// Returns the height of this dimension in double precision. /// /// the height public override double Height { get { return height; } } /// /// Set the size of this Dimension object to the specified width /// and height in double precision. /// /// the new width for the Dimension object /// the new height for the Dimension object public override void SetSize(double width, double height) { width = (int) Math.Ceiling(width); height = (int) Math.Ceiling(height); } /// /// Get/set the size of this Dimension object. /// /// the size public new Dimension Size { get { return new Dimension(width, height); } set { SetSize(value.width, value.height); } } /// /// Set the size of this Dimension object /// to the specified width and height. /// /// the new width for this Dimension object. /// the new height for this Dimension object. public void SetSize(int width, int height) { this.width = width; this.height = height; } /// /// Checks whether two dimension objects have equal values. /// /// /// public override bool Equals(Object obj) { if (obj is Dimension) { Dimension d = (Dimension)obj; return (width == d.width) && (height == d.height); } return false; } /// /// Returns the hash code for this Dimension. /// /// a hash code public override int GetHashCode() { int sum = width + height; return sum * (sum + 1)/2 + width; } /// /// Returns a string representation of the values of this /// Dimension object's height and /// width fields. /// /// /// This method is intended to be used only /// for debugging purposes, and the content and format of the returned /// string may vary between implementations. The returned string may be /// empty but may not be null. /// /// a string representation of this Dimension /// object. /// public override string ToString() { return this.GetType().Name + "[width=" + width + ",height=" + height + "]"; } } }