- java.lang.Object
-
- java.awt.geom.RectangularShape
-
- java.awt.geom.Rectangle2D
-
- Direct Known Subclasses:
- Rectangle, Rectangle2D.Double, Rectangle2D.Float
public abstract class Rectangle2D extends RectangularShape
TheRectangle2D
class describes a rectangle defined by a location(x,y)
and dimension(w x h)
.This class is only the abstract superclass for all objects that store a 2D rectangle. The actual storage representation of the coordinates is left to the subclass.
- Since:
- 1.2
-
-
Nested Class Summary
Nested Classes Modifier and Type Class and Description static class
Rectangle2D.Double
TheDouble
class defines a rectangle specified in double coordinates.static class
Rectangle2D.Float
TheFloat
class defines a rectangle specified in float coordinates.
-
Field Summary
Fields Modifier and Type Field and Description static int
OUT_BOTTOM
The bitmask that indicates that a point lies below thisRectangle2D
.static int
OUT_LEFT
The bitmask that indicates that a point lies to the left of thisRectangle2D
.static int
OUT_RIGHT
The bitmask that indicates that a point lies to the right of thisRectangle2D
.static int
OUT_TOP
The bitmask that indicates that a point lies above thisRectangle2D
.
-
Constructor Summary
Constructors Modifier Constructor and Description protected
Rectangle2D()
This is an abstract class that cannot be instantiated directly.
-
Method Summary
Methods Modifier and Type Method and Description void
add(double newx, double newy)
Adds a point, specified by the double precision argumentsnewx
andnewy
, to thisRectangle2D
.void
add(Point2D pt)
Adds thePoint2D
objectpt
to thisRectangle2D
.void
add(Rectangle2D r)
Adds aRectangle2D
object to thisRectangle2D
.boolean
contains(double x, double y)
Tests if the specified coordinates are inside the boundary of theShape
, as described by the definition of insideness.boolean
contains(double x, double y, double w, double h)
Tests if the interior of theShape
entirely contains the specified rectangular area.abstract Rectangle2D
createIntersection(Rectangle2D r)
Returns a newRectangle2D
object representing the intersection of thisRectangle2D
with the specifiedRectangle2D
.abstract Rectangle2D
createUnion(Rectangle2D r)
Returns a newRectangle2D
object representing the union of thisRectangle2D
with the specifiedRectangle2D
.boolean
equals(Object obj)
Determines whether or not the specifiedObject
is equal to thisRectangle2D
.Rectangle2D
getBounds2D()
Returns a high precision and more accurate bounding box of theShape
than thegetBounds
method.PathIterator
getPathIterator(AffineTransform at)
Returns an iteration object that defines the boundary of thisRectangle2D
.PathIterator
getPathIterator(AffineTransform at, double flatness)
Returns an iteration object that defines the boundary of the flattenedRectangle2D
.int
hashCode()
Returns the hashcode for thisRectangle2D
.static void
intersect(Rectangle2D src1, Rectangle2D src2, Rectangle2D dest)
Intersects the pair of specified sourceRectangle2D
objects and puts the result into the specified destinationRectangle2D
object.boolean
intersects(double x, double y, double w, double h)
Tests if the interior of theShape
intersects the interior of a specified rectangular area.boolean
intersectsLine(double x1, double y1, double x2, double y2)
Tests if the specified line segment intersects the interior of thisRectangle2D
.boolean
intersectsLine(Line2D l)
Tests if the specified line segment intersects the interior of thisRectangle2D
.abstract int
outcode(double x, double y)
Determines where the specified coordinates lie with respect to thisRectangle2D
.int
outcode(Point2D p)
Determines where the specifiedPoint2D
lies with respect to thisRectangle2D
.void
setFrame(double x, double y, double w, double h)
Sets the location and size of the outer bounds of thisRectangle2D
to the specified rectangular values.abstract void
setRect(double x, double y, double w, double h)
Sets the location and size of thisRectangle2D
to the specifieddouble
values.void
setRect(Rectangle2D r)
Sets thisRectangle2D
to be the same as the specifiedRectangle2D
.static void
union(Rectangle2D src1, Rectangle2D src2, Rectangle2D dest)
Unions the pair of sourceRectangle2D
objects and puts the result into the specified destinationRectangle2D
object.-
Methods inherited from class java.awt.geom.RectangularShape
clone, contains, contains, getBounds, getCenterX, getCenterY, getFrame, getHeight, getMaxX, getMaxY, getMinX, getMinY, getWidth, getX, getY, intersects, isEmpty, setFrame, setFrame, setFrameFromCenter, setFrameFromCenter, setFrameFromDiagonal, setFrameFromDiagonal
-
-
-
-
Field Detail
-
OUT_LEFT
public static final int OUT_LEFT
The bitmask that indicates that a point lies to the left of thisRectangle2D
.- Since:
- 1.2
- See Also:
- Constant Field Values
-
OUT_TOP
public static final int OUT_TOP
The bitmask that indicates that a point lies above thisRectangle2D
.- Since:
- 1.2
- See Also:
- Constant Field Values
-
OUT_RIGHT
public static final int OUT_RIGHT
The bitmask that indicates that a point lies to the right of thisRectangle2D
.- Since:
- 1.2
- See Also:
- Constant Field Values
-
OUT_BOTTOM
public static final int OUT_BOTTOM
The bitmask that indicates that a point lies below thisRectangle2D
.- Since:
- 1.2
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
Rectangle2D
protected Rectangle2D()
This is an abstract class that cannot be instantiated directly. Type-specific implementation subclasses are available for instantiation and provide a number of formats for storing the information necessary to satisfy the various accessor methods below.- Since:
- 1.2
- See Also:
Rectangle2D.Float
,Rectangle2D.Double
,Rectangle
-
-
Method Detail
-
setRect
public abstract void setRect(double x, double y, double w, double h)
Sets the location and size of thisRectangle2D
to the specifieddouble
values.- Parameters:
x
- the X coordinate of the upper-left corner of thisRectangle2D
y
- the Y coordinate of the upper-left corner of thisRectangle2D
w
- the width of thisRectangle2D
h
- the height of thisRectangle2D
- Since:
- 1.2
-
setRect
public void setRect(Rectangle2D r)
Sets thisRectangle2D
to be the same as the specifiedRectangle2D
.- Parameters:
r
- the specifiedRectangle2D
- Since:
- 1.2
-
intersectsLine
public boolean intersectsLine(double x1, double y1, double x2, double y2)
Tests if the specified line segment intersects the interior of thisRectangle2D
.- Parameters:
x1
- the X coordinate of the start point of the specified line segmenty1
- the Y coordinate of the start point of the specified line segmentx2
- the X coordinate of the end point of the specified line segmenty2
- the Y coordinate of the end point of the specified line segment- Returns:
true
if the specified line segment intersects the interior of thisRectangle2D
;false
otherwise.- Since:
- 1.2
-
intersectsLine
public boolean intersectsLine(Line2D l)
Tests if the specified line segment intersects the interior of thisRectangle2D
.- Parameters:
l
- the specifiedLine2D
to test for intersection with the interior of thisRectangle2D
- Returns:
true
if the specifiedLine2D
intersects the interior of thisRectangle2D
;false
otherwise.- Since:
- 1.2
-
outcode
public abstract int outcode(double x, double y)
Determines where the specified coordinates lie with respect to thisRectangle2D
. This method computes a binary OR of the appropriate mask values indicating, for each side of thisRectangle2D
, whether or not the specified coordinates are on the same side of the edge as the rest of thisRectangle2D
.- Parameters:
x
- the specified X coordinatey
- the specified Y coordinate- Returns:
- the logical OR of all appropriate out codes.
- Since:
- 1.2
- See Also:
OUT_LEFT
,OUT_TOP
,OUT_RIGHT
,OUT_BOTTOM
-
outcode
public int outcode(Point2D p)
Determines where the specifiedPoint2D
lies with respect to thisRectangle2D
. This method computes a binary OR of the appropriate mask values indicating, for each side of thisRectangle2D
, whether or not the specifiedPoint2D
is on the same side of the edge as the rest of thisRectangle2D
.- Parameters:
p
- the specifiedPoint2D
- Returns:
- the logical OR of all appropriate out codes.
- Since:
- 1.2
- See Also:
OUT_LEFT
,OUT_TOP
,OUT_RIGHT
,OUT_BOTTOM
-
setFrame
public void setFrame(double x, double y, double w, double h)
Sets the location and size of the outer bounds of thisRectangle2D
to the specified rectangular values.- Specified by:
setFrame
in classRectangularShape
- Parameters:
x
- the X coordinate of the upper-left corner of thisRectangle2D
y
- the Y coordinate of the upper-left corner of thisRectangle2D
w
- the width of thisRectangle2D
h
- the height of thisRectangle2D
- Since:
- 1.2
- See Also:
RectangularShape.getFrame()
-
getBounds2D
public Rectangle2D getBounds2D()
Returns a high precision and more accurate bounding box of theShape
than thegetBounds
method. Note that there is no guarantee that the returnedRectangle2D
is the smallest bounding box that encloses theShape
, only that theShape
lies entirely within the indicatedRectangle2D
. The bounding box returned by this method is usually tighter than that returned by thegetBounds
method and never fails due to overflow problems since the return value can be an instance of theRectangle2D
that uses double precision values to store the dimensions.Note that the definition of insideness can lead to situations where points on the defining outline of the
shape
may not be considered contained in the returnedbounds
object, but only in cases where those points are also not considered contained in the originalshape
.If a
point
is inside theshape
according to thecontains(point)
method, then it must be inside the returnedRectangle2D
bounds object according to thecontains(point)
method of thebounds
. Specifically:shape.contains(p)
requiresbounds.contains(p)
If a
point
is not inside theshape
, then it might still be contained in thebounds
object:bounds.contains(p)
does not implyshape.contains(p)
- Returns:
- an instance of
Rectangle2D
that is a high-precision bounding box of theShape
. - Since:
- 1.2
- See Also:
Shape.getBounds()
-
contains
public boolean contains(double x, double y)
Tests if the specified coordinates are inside the boundary of theShape
, as described by the definition of insideness.- Parameters:
x
- the specified X coordinate to be testedy
- the specified Y coordinate to be tested- Returns:
true
if the specified coordinates are inside theShape
boundary;false
otherwise.- Since:
- 1.2
-
intersects
public boolean intersects(double x, double y, double w, double h)
Tests if the interior of theShape
intersects the interior of a specified rectangular area. The rectangular area is considered to intersect theShape
if any point is contained in both the interior of theShape
and the specified rectangular area.The
Shape.intersects()
method allows aShape
implementation to conservatively returntrue
when:-
there is a high probability that the rectangular area and the
Shape
intersect, but - the calculations to accurately determine this intersection are prohibitively expensive.
Shapes
this method might returntrue
even though the rectangular area does not intersect theShape
. TheArea
class performs more accurate computations of geometric intersection than mostShape
objects and therefore can be used if a more precise answer is required.- Parameters:
x
- the X coordinate of the upper-left corner of the specified rectangular areay
- the Y coordinate of the upper-left corner of the specified rectangular areaw
- the width of the specified rectangular areah
- the height of the specified rectangular area- Returns:
true
if the interior of theShape
and the interior of the rectangular area intersect, or are both highly likely to intersect and intersection calculations would be too expensive to perform;false
otherwise.- Since:
- 1.2
- See Also:
Area
-
there is a high probability that the rectangular area and the
-
contains
public boolean contains(double x, double y, double w, double h)
Tests if the interior of theShape
entirely contains the specified rectangular area. All coordinates that lie inside the rectangular area must lie within theShape
for the entire rectanglar area to be considered contained within theShape
.The
Shape.contains()
method allows aShape
implementation to conservatively returnfalse
when:-
the
intersect
method returnstrue
and -
the calculations to determine whether or not the
Shape
entirely contains the rectangular area are prohibitively expensive.
Shapes
this method might returnfalse
even though theShape
contains the rectangular area. TheArea
class performs more accurate geometric computations than mostShape
objects and therefore can be used if a more precise answer is required.- Parameters:
x
- the X coordinate of the upper-left corner of the specified rectangular areay
- the Y coordinate of the upper-left corner of the specified rectangular areaw
- the width of the specified rectangular areah
- the height of the specified rectangular area- Returns:
true
if the interior of theShape
entirely contains the specified rectangular area;false
otherwise or, if theShape
contains the rectangular area and theintersects
method returnstrue
and the containment calculations would be too expensive to perform.- Since:
- 1.2
- See Also:
Area
,Shape.intersects(double, double, double, double)
-
the
-
createIntersection
public abstract Rectangle2D createIntersection(Rectangle2D r)
Returns a newRectangle2D
object representing the intersection of thisRectangle2D
with the specifiedRectangle2D
.- Parameters:
r
- theRectangle2D
to be intersected with thisRectangle2D
- Returns:
- the largest
Rectangle2D
contained in both the specifiedRectangle2D
and in thisRectangle2D
. - Since:
- 1.2
-
intersect
public static void intersect(Rectangle2D src1, Rectangle2D src2, Rectangle2D dest)
Intersects the pair of specified sourceRectangle2D
objects and puts the result into the specified destinationRectangle2D
object. One of the source rectangles can also be the destination to avoid creating a third Rectangle2D object, but in this case the original points of this source rectangle will be overwritten by this method.- Parameters:
src1
- the first of a pair ofRectangle2D
objects to be intersected with each othersrc2
- the second of a pair ofRectangle2D
objects to be intersected with each otherdest
- theRectangle2D
that holds the results of the intersection ofsrc1
andsrc2
- Since:
- 1.2
-
createUnion
public abstract Rectangle2D createUnion(Rectangle2D r)
Returns a newRectangle2D
object representing the union of thisRectangle2D
with the specifiedRectangle2D
.- Parameters:
r
- theRectangle2D
to be combined with thisRectangle2D
- Returns:
- the smallest
Rectangle2D
containing both the specifiedRectangle2D
and thisRectangle2D
. - Since:
- 1.2
-
union
public static void union(Rectangle2D src1, Rectangle2D src2, Rectangle2D dest)
Unions the pair of sourceRectangle2D
objects and puts the result into the specified destinationRectangle2D
object. One of the source rectangles can also be the destination to avoid creating a third Rectangle2D object, but in this case the original points of this source rectangle will be overwritten by this method.- Parameters:
src1
- the first of a pair ofRectangle2D
objects to be combined with each othersrc2
- the second of a pair ofRectangle2D
objects to be combined with each otherdest
- theRectangle2D
that holds the results of the union ofsrc1
andsrc2
- Since:
- 1.2
-
add
public void add(double newx, double newy)
Adds a point, specified by the double precision argumentsnewx
andnewy
, to thisRectangle2D
. The resultingRectangle2D
is the smallestRectangle2D
that contains both the originalRectangle2D
and the specified point.After adding a point, a call to
contains
with the added point as an argument does not necessarily returntrue
. Thecontains
method does not returntrue
for points on the right or bottom edges of a rectangle. Therefore, if the added point falls on the left or bottom edge of the enlarged rectangle,contains
returnsfalse
for that point.- Parameters:
newx
- the X coordinate of the new pointnewy
- the Y coordinate of the new point- Since:
- 1.2
-
add
public void add(Point2D pt)
Adds thePoint2D
objectpt
to thisRectangle2D
. The resultingRectangle2D
is the smallestRectangle2D
that contains both the originalRectangle2D
and the specifiedPoint2D
.After adding a point, a call to
contains
with the added point as an argument does not necessarily returntrue
. Thecontains
method does not returntrue
for points on the right or bottom edges of a rectangle. Therefore, if the added point falls on the left or bottom edge of the enlarged rectangle,contains
returnsfalse
for that point.- Parameters:
pt
- the newPoint2D
to add to thisRectangle2D
.- Since:
- 1.2
-
add
public void add(Rectangle2D r)
Adds aRectangle2D
object to thisRectangle2D
. The resultingRectangle2D
is the union of the twoRectangle2D
objects.- Parameters:
r
- theRectangle2D
to add to thisRectangle2D
.- Since:
- 1.2
-
getPathIterator
public PathIterator getPathIterator(AffineTransform at)
Returns an iteration object that defines the boundary of thisRectangle2D
. The iterator for this class is multi-threaded safe, which means that thisRectangle2D
class guarantees that modifications to the geometry of thisRectangle2D
object do not affect any iterations of that geometry that are already in process.- Parameters:
at
- an optionalAffineTransform
to be applied to the coordinates as they are returned in the iteration, ornull
if untransformed coordinates are desired- Returns:
- the
PathIterator
object that returns the geometry of the outline of thisRectangle2D
, one segment at a time. - Since:
- 1.2
-
getPathIterator
public PathIterator getPathIterator(AffineTransform at, double flatness)
Returns an iteration object that defines the boundary of the flattenedRectangle2D
. Since rectangles are already flat, theflatness
parameter is ignored. The iterator for this class is multi-threaded safe, which means that thisRectangle2D
class guarantees that modifications to the geometry of thisRectangle2D
object do not affect any iterations of that geometry that are already in process.- Specified by:
getPathIterator
in interfaceShape
- Overrides:
getPathIterator
in classRectangularShape
- Parameters:
at
- an optionalAffineTransform
to be applied to the coordinates as they are returned in the iteration, ornull
if untransformed coordinates are desiredflatness
- the maximum distance that the line segments used to approximate the curved segments are allowed to deviate from any point on the original curve. Since rectangles are already flat, theflatness
parameter is ignored.- Returns:
- the
PathIterator
object that returns the geometry of the outline of thisRectangle2D
, one segment at a time. - Since:
- 1.2
-
hashCode
public int hashCode()
Returns the hashcode for thisRectangle2D
.- Overrides:
hashCode
in classObject
- Returns:
- the hashcode for this
Rectangle2D
. - Since:
- 1.2
- See Also:
Object.equals(java.lang.Object)
,System.identityHashCode(java.lang.Object)
-
equals
public boolean equals(Object obj)
Determines whether or not the specifiedObject
is equal to thisRectangle2D
. The specifiedObject
is equal to thisRectangle2D
if it is an instance ofRectangle2D
and if its location and size are the same as thisRectangle2D
.- Overrides:
equals
in classObject
- Parameters:
obj
- anObject
to be compared with thisRectangle2D
.- Returns:
true
ifobj
is an instance ofRectangle2D
and has the same values;false
otherwise.- Since:
- 1.2
- See Also:
Object.hashCode()
,HashMap
-
-
Traduction non disponible
Les API Java ne sont pas encore traduites en français sur l'infobrol. Seule la version anglaise est disponible pour l'instant.
Version en cache
24/11/2024 20:18:44 Cette version de la page est en cache (à la date du 24/11/2024 20:18:44) afin d'accélérer le traitement. Vous pouvez activer le mode utilisateur dans le menu en haut pour afficher la dernère version de la page.Document créé le 29/08/2006, dernière modification le 04/03/2020
Source du document imprimé : https://www.gaudry.be/java-api-rf-java/awt/geom/Rectangle2D.html
L'infobrol est un site personnel dont le contenu n'engage que moi. Le texte est mis à disposition sous licence CreativeCommons(BY-NC-SA). Plus d'info sur les conditions d'utilisation et sur l'auteur.
Références
Ces références et liens indiquent des documents consultés lors de la rédaction de cette page, ou qui peuvent apporter un complément d'information, mais les auteurs de ces sources ne peuvent être tenus responsables du contenu de cette page.
L'auteur de ce site est seul responsable de la manière dont sont présentés ici les différents concepts, et des libertés qui sont prises avec les ouvrages de référence. N'oubliez pas que vous devez croiser les informations de sources multiples afin de diminuer les risques d'erreurs.