Rectangle - CoolPotato31F/Java-Graphics GitHub Wiki
Rectangle Class
Overview
The Rectangle class is part of the graphics package and implements the GraphicsObject interface. It represents a drawable rectangle defined by two diagonal points and supports various operations, including movement, collision detection, and rendering.
Attributes
Point point1- The top-left corner of the rectangle.Point point2- The bottom-right corner of the rectangle.Point center- The center of the rectangle.int width- The outline width of the rectangle.int w- The rectangle's width.int h- The rectangle's height.GraphWin canvas- Reference to the canvas where the rectangle is drawn.Color fillColor- The fill color of the rectangle.Color outlineColor- The outline color of the rectangle (default:BLACK).
Constructors
Rectangle(Point p1, Point p2)
Creates a rectangle defined by two diagonal points.
Parameters:
Point p1- One corner of the rectangle.Point p2- The opposite corner of the rectangle.
Methods
void draw(GraphWin canvas)
Draws the rectangle on the given canvas.
Parameters:
GraphWin canvas- The canvas to draw on.
Throws:
Errorif the rectangle is already drawn.
void undraw()
Removes the rectangle from the canvas.
void setFill(Color color)
Sets the fill color of the rectangle.
Parameters:
Color color- The color to fill the rectangle with.
void setOutline(Color color)
Sets the outline color of the rectangle.
Parameters:
Color color- The outline color.
String toString()
Returns a string representation of the rectangle in the format:
Rectangle(Point(x1, y1), Point(x2, y2))
Point getP1()
Returns the first corner (top-left) of the rectangle.
Point getP2()
Returns the second corner (bottom-right) of the rectangle.
Point getCenter()
Returns the center point of the rectangle.
Point getSize()
Returns a Point where x represents the width and y represents the height.
void setWidth(int width)
Sets the outline width of the rectangle.
Parameters:
int width- The width of the outline.
int getWidth()
Returns the outline width of the rectangle.
void move(double dx, double dy)
Moves the rectangle by the given x and y amounts.
Parameters:
double dx- The amount to move along the x-axis.double dy- The amount to move along the y-axis.
void drawPanel(Graphics2D graphics)
Renders the rectangle onto a Graphics2D panel.
Parameters:
Graphics2D graphics- The graphics object used for rendering.