From 3bd483f2a189ed92cefd9ad0cfa5f15fabdf1592 Mon Sep 17 00:00:00 2001 From: Tom Tromey Date: Wed, 16 Jan 2002 04:21:35 +0000 Subject: Start of AWT merge with Classpath: * Makefile.in: Rebuilt. * Makefile.am (awt_java_source_files): Reference files in gnu/java/awt, not gnu/gcj/awt. * java/awt/image/BufferedImage.java: Updated copyright. * java/awt/image/ComponentColorModel.java: Updated copyright. * java/awt/image/ComponentSampleModel.java: Updated copyright. * java/awt/image/DataBuffer.java: Updated copyright. * java/awt/image/DataBufferByte.java: Updated copyright. * java/awt/image/DataBufferInt.java: Updated copyright. * java/awt/image/DataBufferUShort.java: Updated copyright. * java/awt/image/IndexColorModel.java: Updated copyright. * java/awt/image/PackedColorModel.java: Updated copyright. * java/awt/image/Raster.java: Updated copyright. * java/awt/image/RasterOp.java: Updated copyright. * java/awt/image/SampleModel.java: Updated copyright. * java/awt/image/SinglePixelPackedSampleModel.java: Updated copyright. * java/awt/image/WritableRaster.java: Updated copyright. * java/awt/color/ColorSpace.java: Updated copyright. * java/awt/color/ICC_ColorSpace.java: Updated copyright * java/awt/color/ICC_Profile.java: Updated copyright. * java/awt/event/HierarchyBoundsAdapter.java: Updated copyright. * java/awt/event/HierarchyBoundsListener.java: Updated copyright. * java/awt/event/HierarchyEvent.java: Updated copyright. * java/awt/event/HierarchyListener.java: Updated copyright. * java/awt/geom/AffineTransform.java: Updated copyright. * java/awt/geom/Dimension2D.java: Updated copyright. * java/awt/geom/Ellipse2D.java: Updated copyright. * java/awt/geom/IllegalPathStateException.java: Updated copyright. * java/awt/geom/Line2D.java: Updated copyright. * java/awt/geom/NoninvertibleTransformException.java: Updated copyright. * java/awt/geom/PathIterator.java: Updated copyright. * java/awt/geom/Point2D.java: Updated copyright. * java/awt/geom/Rectangle2D.java: Updated copyright. * java/awt/geom/RectangularShape.java: Updated copyright. * java/awt/geom/RoundRectangle2D.java: Updated copyright. * java/awt/Toolkit.java: Updated import for file moves. * java/awt/Rectangle.java: Updated copyright; added javadoc from Classpath. (hashCode): New method from Classpath. * java/awt/Graphics2D.java: Updated copyright. * java/awt/Transparency.java: Updated copyright. * java/awt/Paint.java: Updated copyright. * java/awt/Graphics.java: New version from Classpath. * java/awt/EventDispatchThread.java: Updated copyright. * java/awt/CardLayout.java (layoutContainer): Don't skip invisible children. (gotoComponent): Wrap around on next/previous. * gnu/gcj/awt/BitMaskExtent.java: Removed. * gnu/gcj/awt/Buffers.java: Removed. * gnu/gcj/awt/ComponentDataBlitOp.java: Removed. * gnu/gcj/awt/GLightweightPeer.java: Removed. * gnu/java/awt/BitMaskExtent.java: Added. * gnu/java/awt/Buffers.java: Added. * gnu/java/awt/ComponentDataBlitOp.java: Added. * gnu/java/awt/GLightweightPeer.java: Added. * java/awt/geom/Line2D.java (clone): Ignore CloneNotSupportedException. * gnu/gcj/awt/GLightweightPeer.java (getColorModel): New method. * java/awt/Frame.java: Merged with Classpath. * java/awt/RenderingHints.java: Copyright update. * java/awt/Paint.java: Copyright update. * java/awt/image/DirectColorModel.java: Merged with Classpath. * java/awt/image/ColorModel.java: Merged with Classpath. * java/awt/Window.java (show): New Implementation from Classpath. (isShowing): Use super.isShowing(). * java/awt/EventQueue.java: Merged with Classpath. * java/awt/AWTEventMulticaster.java (save): Throw RuntimeException. (saveInternal): Likewise. * java/awt/AWTEvent.java: Now implements Serializable. * java/awt/Event.java: Copyright update. * java/awt/peer/ComponentPeer.java: Merged with Classpath. * java/awt/image/BufferedImage.java: Copyright update. * java/awt/GraphicsConfiguration.java: Copyright update. * java/awt/Component.java: (addNotify): Don't call addNotifyContainerChildren(). (addNotifyContainerChildren): Removed. (setPeer): New method from Classpath. (setTreeLock): Likewise. (setVisible): Rewrote. (show): Use it. (hide): Likewise. (validate): Set `valid'. (checkImage(Image,ImageObserver)): Implementation from Classpath. (createImage(ImageProducer)): Likewise. (prepareImage): Likewise. * java/awt/Container.java (addImpl): Handle case where constraint is not a String. Post event via system event queue. (remove): Post event via system event queue. (validateTree): Only validate child if it is invalid. (getAlignmentX): Call super method as default. (getAlignmentY): Likewise. (addContainerListener): Now synchronized. (removeContainerListener): Likewise. (addNotifyContainerChildren): Now private. * java/awt/ComponentOrientation.java: Updated copyright. Added @author. * java/awt/FlowLayout.java (serialVersionUID): New field. (setAlignment): Better exception message. (layoutContainer): Don't compute component's preferred size unless we're going to use it. * java/awt/BorderLayout.java (AFTER_LAST_LINE, AFTER_LINE_ENDS, BEFORE_FIRST_LINE, BEFORE_LINE_BEGINS): New constants. (firstLine, lastLine, firstItem, lastItem): New fields. (addLayoutComponent): Handle case where constraints is null. Also, handle relative locations. (removeLayoutComponent): Handle relative locations. (MIN, MAX, PREF): New constants. (calcCompSize): New method. (calcSize): New method. (minimumLayoutSize): Use it. (preferredLayoutSize): Likewise. (maximumLayoutSize): Likewise. (toString): Include more information. (setBounds): New method. (layoutContainer): Use libgcj implementation; extended to handle relative locations. From-SVN: r48896 --- libjava/java/awt/Rectangle.java | 323 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 308 insertions(+), 15 deletions(-) (limited to 'libjava/java/awt/Rectangle.java') diff --git a/libjava/java/awt/Rectangle.java b/libjava/java/awt/Rectangle.java index 7e7ef728d32..a78adc0828b 100644 --- a/libjava/java/awt/Rectangle.java +++ b/libjava/java/awt/Rectangle.java @@ -1,10 +1,28 @@ -/* Copyright (C) 1999, 2000, 2001 Free Software Foundation +/* Copyright (C) 1999, 2000, 2001, 2002 Free Software Foundation - This file is part of libjava. +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA +02111-1307 USA. + +As a special exception, if you link this library with other files to +produce an executable, this library does not by itself cause the +resulting executable to be covered by the GNU General Public License. +This exception does not however invalidate any other reasons why the +executable file might be covered by the GNU General Public License. */ -This software is copyrighted work licensed under the terms of the -Libjava License. Please consult the file "LIBJAVA_LICENSE" for -details. */ package java.awt; @@ -13,14 +31,42 @@ import java.io.Serializable; /* Status: Mostly complete. Some of the Java2D stuff is commented out. */ +/** + * This class represents a rectangle and all the interesting things you + * might want to do with it. Note that the coordinate system uses + * the origin (0,0) as the top left of the screen, with the x and y + * values increasing as they move to the right and down respectively. + * + * @author Warren Levy + * @author Aaron M. Renn (arenn@urbanophile.com) + */ public class Rectangle extends Rectangle2D implements Cloneable, Shape, Serializable { + /** + * The X coordinate of the top-left corner of the rectangle. + */ public int x; + + /** + * The Y coordinate of the top-left corner of the rectangle; + */ public int y; + + /** + * The width of the rectangle + */ public int width; + + /** + * The height of the rectangle + */ public int height; + /** + * Initializes a new instance of Rectangle with a top + * left corner at (0,0) and a width and height of 0. + */ public Rectangle() { x = 0; @@ -29,6 +75,12 @@ public class Rectangle extends Rectangle2D height = 0; } + /** + * Initializes a new instance of Rectangle from the + * coordinates of the specified rectangle. + * + * @param rect The rectangle to copy from. + */ public Rectangle(Rectangle r) { x = r.x; @@ -37,6 +89,15 @@ public class Rectangle extends Rectangle2D height = r.height; } + /** + * Initializes a new instance of Rectangle from the specified + * inputs. + * + * @param x The X coordinate of the top left corner of the rectangle. + * @param y The Y coordinate of the top left corner of the rectangle. + * @param width The width of the rectangle. + * @param height The height of the rectangle. + */ public Rectangle(int x, int y, int width, int height) { this.x = x; @@ -45,6 +106,14 @@ public class Rectangle extends Rectangle2D this.height = height; } + /** + * Initializes a new instance of Rectangle with the specified + * width and height. The upper left corner of the rectangle will be at + * the origin (0,0). + * + * @param width The width of the rectangle. + * @param height the height of the rectange. + */ public Rectangle(int width, int height) { x = 0; @@ -53,6 +122,14 @@ public class Rectangle extends Rectangle2D this.height = height; } + /** + * Initializes a new instance of Rectangle with a top-left + * corner represented by the specified point and the width and height + * represented by the specified dimension. + * + * @param point The upper left corner of the rectangle. + * @param dim The width and height of the rectangle. + */ public Rectangle(Point p, Dimension d) { x = p.x; @@ -61,6 +138,12 @@ public class Rectangle extends Rectangle2D height = d.height; } + /** + * Initializes a new instance of Rectangle with a top left + * corner at the specified point and a width and height of zero. + * + * @param poin The upper left corner of the rectangle. + */ public Rectangle(Point p) { x = p.x; @@ -69,6 +152,13 @@ public class Rectangle extends Rectangle2D height = 0; } + /** + * Initializes a new instance of Rectangle with an + * upper left corner at the origin (0,0) and a width and height represented + * by the specified dimension. + * + * @param dim The width and height of the rectangle. + */ public Rectangle(Dimension d) { x = 0; @@ -77,11 +167,24 @@ public class Rectangle extends Rectangle2D height = d.height; } + /** + * Returns the bounding rectangle for this rectangle, which is simply + * this rectange itself. + * + * @return This rectangle. + */ public Rectangle getBounds () { return (Rectangle) this.clone(); } + /** + * Modifies this rectangle so that it represents the smallest rectangle + * that contains both the existing rectangle and the specified point. + * + * @param x The X coordinate of the point to add to this rectangle. + * @param y The Y coordinate of the point to add to this rectangle. + */ public void add(int newx, int newy) { int x = this.x > newx ? newx : this.x; @@ -92,11 +195,23 @@ public class Rectangle extends Rectangle2D this.y = y; } + /** + * Modifies this rectangle so that it represents the smallest rectangle + * that contains both the existing rectangle and the specified point. + * + * @param point The point to add to this rectangle. + */ public void add(Point pt) { add (pt.x, pt.y); } + /** + * Modifies this rectangle so that it represents the smallest rectangle + * that contains both the existing rectangle and the specified rectangle. + * + * @param rect The rectangle to add to this rectangle. + */ public void add(Rectangle r) { int x = this.x > r.x ? r.x : this.x; @@ -109,6 +224,15 @@ public class Rectangle extends Rectangle2D this.y = y; } + /** + * Tests whether or not the specified point is inside this rectangle. + * + * @param x The X coordinate of the point to test. + * @param y The Y coordinate of the point to test. + * + * @return true if the point is inside the rectangle, + * false otherwise. + */ public boolean contains(int x, int y) { return (x >= this.x && x <= this.x + this.width @@ -121,6 +245,14 @@ public class Rectangle extends Rectangle2D && y >= this.y && y + h <= this.y + this.height); } + /** + * Tests whether or not the specified point is inside this rectangle. + * + * @param point The point to test. + * + * @return true if the point is inside the rectangle, + * false otherwise. + */ public boolean contains(Point p) { return contains(p.x, p.y); @@ -131,6 +263,22 @@ public class Rectangle extends Rectangle2D return contains(r.x, r.y, r.width, r.height); } + /** + * Tests this rectangle for equality against the specified object. This + * will be true if an only if the specified object: + *

+ *

+ * + * @param obj The object to test against for equality. + * + * @return true if the specified object is equal to this one, + * false otherwise. + */ public boolean equals(Object obj) { if (obj instanceof Rectangle) @@ -149,11 +297,22 @@ public class Rectangle extends Rectangle2D return (double) this.height; } + /** + * Returns the location of this rectangle, which is the coordinates of + * its upper left corner. + * + * @return The point where this rectangle is located. + */ public Point getLocation() { return new Point(x,y); } + /** + * Returns the size of this rectangle. + * + * @return The size of this rectangle. + */ public Dimension getSize() { return new Dimension(width, height); @@ -174,21 +333,49 @@ public class Rectangle extends Rectangle2D return (double) y; } + /** + * Expands the rectangle by the specified amount. The horizontal + * and vertical expansion values are applied both to the X,Y coordinate + * of this rectangle, and its width and height. Thus the width and + * height will increase by 2h and 2v accordingly. + * + * @param h The horizontal expansion value. + * @param v The vertical expansion value. + */ public void grow(int h, int v) { width += h; height += v; } - /** @deprecated Use contains() instead. */ + /** + * Tests whether or not the specified point is inside this rectangle. + * + * @param x The X coordinate of the point to test. + * @param y The Y coordinate of the point to test. + * + * @return true if the point is inside the rectangle, + * false otherwise. + * + * @deprecated This method is deprecated in favor of + * contains(int, int). + */ public boolean inside(int x, int y) { return contains(x, y); } - /** @specnote If there is no intersection, an empty rectangle at 0,0 - * is returned. - */ + /** + * Determines the rectange which is formed by the intersection of this + * rectangle with the specified rectangle. + * + * @param rect The rectange to calculate the intersection with. + * + * @return The rectangle bounding the intersection. + * + * @specnote If there is no intersection, an empty rectangle at 0,0 + * is returned. + */ public Rectangle intersection(Rectangle r) { int newx = x < r.x ? r.x : x; @@ -203,9 +390,17 @@ public class Rectangle extends Rectangle2D return new Rectangle(0, 0, 0, 0); } - /** @specnote If the intersection is at an edge or corner only (an empty - * intersection with a non-zero location), false is returned. - */ + /** + * Tests whether or not the specified rectangle intersects this rectangle. + * + * @param rect The rectangle to test against. + * + * @return true if the specified rectangle intersects this + * one, false otherwise. + * + * @specnote If the intersection is at an edge or corner only (an empty + * intersection with a non-zero location), false is returned. + */ public boolean intersects(Rectangle r) { int neww = (x + width < r.x + r.width ? @@ -215,12 +410,29 @@ public class Rectangle extends Rectangle2D return (neww > 0 && newh > 0); } + /** + * Tests whether or not this rectangle is empty. An empty rectangle + * has a width or height of zero. + * + * @return true if the rectangle is empty, false + * otherwise. + */ public boolean isEmpty() { return !(width > 0 && height > 0); } - /** @deprecated Use setLocation() instead. */ + /** + * Moves the location of this rectangle by setting its upper left + * corner to the specified coordinates. + * // FIXME: Is this true? + * + * @param x The new X coordinate for this rectangle. + * @param y The new Y coordinate for this rectangle. + * + * @deprecated This method is deprecated in favor of + * setLocation(int, int). + */ public void move(int x, int y) { setLocation(x, y); @@ -232,18 +444,44 @@ public class Rectangle extends Rectangle2D return 0; } - /** @deprecated Use setBounds() instead. */ + /** + * Updates this rectangle to have the specified dimensions. + * + * @param x The new X coordinate of the upper left hand corner. + * @param y The new Y coordinate of the upper left hand corner. + * @param width The new width of this rectangle. + * @param height The new height of this rectangle. + * + * @deprecated This method is deprecated in favor of + * setBounds(int, int, int, int). + */ public void reshape(int x, int y, int width, int height) { setBounds(x, y, width, height); } - /** @deprecated Use setSize() instead. */ + /** + * Sets the size of this rectangle based on the specified dimensions. + * + * @param width The new width of the rectangle. + * @param height The new height of the rectangle. + * + * @deprecated This method is deprecated in favor of + * setSize(int, int). + */ public void resize(int width, int height) { setSize(width, height); } + /** + * Updates this rectangle to have the specified dimensions. + * + * @param x The new X coordinate of the upper left hand corner. + * @param y The new Y coordinate of the upper left hand corner. + * @param width The new width of this rectangle. + * @param height The new height of this rectangle. + */ public void setBounds(int x, int y, int width, int height) { this.x = x; @@ -252,6 +490,12 @@ public class Rectangle extends Rectangle2D this.height = height; } + /** + * Updates this rectangle to match the dimensions of the specified + * rectangle. + * + * @param rect The rectangle to update from. + */ public void setBounds(Rectangle r) { this.x = r.x; @@ -260,12 +504,27 @@ public class Rectangle extends Rectangle2D this.height = r.height; } + /** + * Moves the location of this rectangle by setting its upper left + * corner to the specified coordinates. + * // FIXME: Is this true? + * + * @param x The new X coordinate for this rectangle. + * @param y The new Y coordinate for this rectangle. + */ public void setLocation(int x, int y) { this.x = x; this.y = y; } + /** + * Moves the location of this rectangle by setting its upper left + * corner to the specified point. + * // FIXME: Is this true? + * + * @param point The point to move the rectange to. + */ public void setLocation(Point p) { this.x = p.x; @@ -280,12 +539,23 @@ public class Rectangle extends Rectangle2D this.height = (int) height; } + /** + * Sets the size of this rectangle based on the specified dimensions. + * + * @param dim The new dimensions of the rectangle. + */ public void setSize(Dimension d) { this.width = d.width; this.height = d.height; } + /** + * Sets the size of this rectangle based on the specified dimensions. + * + * @param width The new width of the rectangle. + * @param height The new height of the rectangle. + */ public void setSize(int width, int height) { this.width = width; @@ -298,6 +568,14 @@ public class Rectangle extends Rectangle2D y += y; } + /** + * Returns the smallest rectangle that contains both this rectangle + * and the specified rectangle. + * + * @param rect The rectangle to compute the union with. + * + * @return The smallest rectangle containing both rectangles. + */ public Rectangle union(Rectangle r) { int newx = x > r.x ? r.x : x; @@ -333,9 +611,24 @@ public class Rectangle extends Rectangle2D return new Rectangle (x, y, width, height); } + /** + * Returns a string representation of this rectangle. + * + * @return A string representation of this rectangle. + */ public String toString() { return getClass().getName() + "[x=" + x + ",y=" + y + ",width=" + width + ",height=" + height + "]"; } + + /** + * Returns a hash value for this object. + * + * @return A hash value for this object. + */ + public int hashCode() + { + return x * y * width * height * 37; + } } -- cgit v1.2.3