Important: The information in this document is obsolete and should not be used for new development.
Inherits from | Object |
Implements |
Cloneable |
Package | com.apple.cocoa.foundation |
Companion guide | Geometry and Range Utilities |
An NSRect object represents a rectangle. The elemental attributes of a rectangle are its origin (its starting x coordinate and y coordinate) and its size (width and height as measured from the origin). An NSRect with height or width of 0 or less is considered an “empty” rectangle. The methods of NSRect give you access to these values and to computed values, and they allow you to compare rectangles and perform various tests, such as determining whether a point falls in a rectangle. They also convert NSRects between string objects and AWT rectangles, and they yield new NSRects based on intersection, union, inset, offset, and other operations.
rectByInsettingRect
rectByIntersectingRect
rectByMakingIntegral
rectByOffsettingRect
rectByUnioningRect
sliceRect
public NSRect
()
Initializes an empty rectangle (that is, a rectangle with at least one dimension of 0).
public NSRect
(float x, float y, float w, float h)
Initializes an NSRect from a starting x coordinate (x), a starting y coordinate (y), a width value (w), and a height value (h). If either width or height is 0, initializes an empty rectangle. Throws an IllegalArgumentException
if any argument is not a valid float value (NaN).
public NSRect
(NSPoint aPoint, NSSize aSize)
Initializes an NSRect from an NSPoint object, aPoint, and an NSSize object, aSize.
public NSRect
(NSPoint pointOne, NSPoint pointTwo)
Initializes an NSRect from two NSPoint objects, pointOne and pointTwo, creating the smallest rectangle whose opposite corners touch the two points.
public NSRect
(java.awt.Rectangle aRectangle)
Initializes an NSRect from an AWT Rectangle object, aRectangle.
public NSRect
(NSRect aRectangle)
Initializes an NSRect from another NSRect object, aRectangle; this constructor is used in cloning the receiver.
public static NSRect fromString
(String rectAsString)
Creates an NSRect from the string rectAsString, which must be of the form “{{x, y}, {w, h}}”, where x is a float representation of the origin x coordinate, y is a float representation of the origin y coordinate, w is a float representation of the width, and h is a float representation of the height. Throws an IllegalArgumentException
if the string is improperly formatted.
Creates and returns a copy of the receiver.
public Object clone
()
Returns whether the receiver contains the point aPoint.
public boolean containsPoint
(NSPoint aPoint, boolean isFlipped)
If isFlipped is true
, the measurements occur in a flipped coordinate system (where the y axis extends down instead of up). If aPoint occurs inside or coincides with the receiver’s x axis or y axis and falls within the opposite edges of the rectangle, it is considered to be contained by the rectangle.
Returns whether otherObject is an NSRect and is equal in origin and size to the receiver.
public boolean equals
(Object otherObject)
Provides an appropriate hash code useful for storing the receiver in any hash-based data structure.
public int hashCode
()
This value is the sum of the receiver’s origin x coordinate, its origin y coordinate, half of its width, and half of its height, rounded to the nearest integer.
Returns the height dimension of the receiver.
public float height
()
Returns whether the receiver intersects rectangle aRect.
public boolean intersectsRect
(NSRect aRect)
Alignment of outer edges is not considered an intersection. Returns false
if either NSRect is an empty rectangle.
Returns whether either dimension (width or height) of the receiver is 0.
public boolean isEmpty
()
Returns whether the NSRect aRect is equal in origin and size to the receiver.
public boolean isEqualToRect
(NSRect aRect)
Returns whether the receiver is entirely enclosed by rectangle aRect.
public boolean isSubrectOfRect
(NSRect aRect)
Coincidence of x axes and y axes or the edges opposite the x axis or y axis are acceptable. Returns false
if either NSRect is an empty rectangle. By reversing receiver and argument, you can use this method as a virtual “containsRect
”.
Returns the farthest extent of the receiver along the x axis in the current coordinate system; or, to put it another way, the x coordinate value of the receiver’s right edge.
public float maxX
()
This value is the sum of the receiver’s origin x coordinate and its width.
Returns the farthest extent of the receiver along the y axis in the current coordinate system; or, to put it another way, the y coordinate value of the receiver’s top edge (or bottom, if the coordinate system is flipped).
public float maxY
()
This value is the sum of the receiver’s origin y coordinate and its height.
Returns the extent of the receiver along the x axis halfway between its origin x coordinate and the x coordinate of its right edge.
public float midX
()
Returns the extent of the receiver along the y axis halfway between its origin y coordinate and the y coordinate of its upper edge (or bottom edge, if used in a flipped coordinate system).
public float midY
()
Returns the origin of the receiver, the point from which the dimensions of the receiver are measured.
public NSPoint origin
()
Returns an NSRect that is the result of insetting the receiver horizInset units horizontally and vertInset units vertically from all edges.
public NSRect rectByInsettingRect
(float horizInset, float vertInset)
Returns an NSRect that is the result of the intersection of the receiver with otherRect.
public NSRect rectByIntersectingRect
(NSRect otherRect)
Returns ZeroRect
if either receiver or otherRect is an empty rectangle or the two NSRects do not intersect at all.
Returns an NSRect that is the result of rounding down the receiver’s x coordinate and y coordinate to the nearest integer and rounding up the receiver’s height and width to the nearest integer.
public NSRect rectByMakingIntegral
()
The resulting rectangle thus completely encloses the original. Returns ZeroRect
if the receiver is an empty rectangle.
Returns an NSRect that is the result of offsetting the receiver horizInset units horizontally and vertInset units vertically.
public NSRect rectByOffsettingRect
(float horizOffset, float vertOffset)
This method affects the origin only, while rectByInsettingRect
affects all edges.
Returns an NSRect that is the result of the union of the receiver with otherRect, which is a rectangle that contains the two NSRects combined.
public NSRect rectByUnioningRect
(NSRect otherRect)
Returns ZeroRect
if both receiver and otherRect are empty rectangles. If one of the two rectangles, the receiver and otherRect, is an empty rectangle, a copy of the other rectangle is returned.
Returns the size of the receiver.
public NSSize size
()
The NSSize object encapsulates the width and height of the receiver.
Makes two smaller rectangles from the receiver and returns them by modifying two mutable rectangles passed in as arguments.
public void sliceRect
(float thickness, int edge, NSMutableRect rect1, NSMutableRect rect2)
One of these NSMutableRects has the width or height thickness as determined by the constant edge. One modified rectangle is returned in rect1 and the other in rect2; which rectangle goes where is also determined by edge. Here is a summary of how the edge constant and the other arguments interact:
Constant |
Description |
---|---|
|
The rectangle is sliced vertically, and the rectangle with the width of thickness is placed in rect1. |
|
The rectangle is sliced horizontally, and the rectangle with the height of thickness is placed in rect2. |
|
The rectangle is sliced horizontally, and the rectangle with the height of thickness is placed in rect1. |
|
The rectangle is sliced vertically, and the rectangle with the width of thickness is placed in rect2. |
If thickness is greater than the width or height of the receiver (as determined by edge), it is made the same width or height as the receiver. This method does not affect the receiver.
Returns the receiver as a AWT Rectangle object.
public java.awt.Rectangle toAWTRectangle
()
This method calls rectByMakingIntegral
to round the receiver’s float values to appropriate integers.
Returns the receiver as converted to a string object.
public String toString
()
The string has the form of “{{x, y}, {w, h}}”, where x is the origin x coordinate, y is the origin y coordinate, w is the width, and h is the height.
Returns the width of the receiver.
public float width
()
Returns the origin x coordinate of the receiver.
public float x
()
Returns the origin y coordinate of the receiver.
public float y
()
NSRect provides the following constant as a convenience; you can use it to compare values returned by many NSRect methods:
The following constants can be used to specify window edges:
Constant | Description |
---|---|
MinXEdge | The left edge of a window. |
MinYEdge | The bottom edge of a window. |
MaxXEdge | The right edge of a window. |
MaxYEdge | The top edge of a window. |
© 1997, 2006 Apple Computer, Inc. All Rights Reserved. (Last updated: 2006-07-24)