Polygon - CoolPotato31F/Java-Graphics GitHub Wiki
Polygon Class
Overview
The Polygon class represents a polygon that can be drawn and manipulated in a graphical window. It allows for setting the polygon's fill and outline colors, adjusting the outline width, and rendering the polygon on a GraphWin canvas.
Attributes
-
protected Point[] points
An array ofPointobjects that define the vertices of the polygon. -
protected int width = 1
The default stroke width of the polygon's outline. -
protected GraphWin canvas
TheGraphWininstance in which the polygon is drawn. -
protected Color fillColor
The color used to fill the polygon (can be set tonullfor no fill). -
protected Color outlineColor = Color.BLACK
The color of the polygon's outline.
Constructors
Polygon(Point[] p)
Constructs a Polygon from an array of Point objects.
Parameters:
p- An array ofPointobjects defining the polygon vertices.
Throws:
IllegalArgumentException- If the array isnullor contains fewer than 3 points.
Methods
int[] getXCords()
Gets the x-coordinates of the polygon's points.
Returns:
int[]- An array of x-coordinates.
int[] getYCords()
Gets the y-coordinates of the polygon's points.
Returns:
int[]- An array of y-coordinates.
Point[] getPoints()
Gets every point in the polygon.
Returns:
Point[]- An array of allPointobjects that define the polygon's vertices.
void setFill(Color color)
Sets the fill color of the polygon.
Parameters:
color- The color to fill the polygon with.
void setOutline(Color color)
Sets the outline color of the polygon.
Parameters:
color- The outline color of the polygon.
void setWidth(int width)
Sets the width of the polygon's outline.
Parameters:
width- The width of the outline.
String toString()
Returns a string representation of the polygon, including its points.
Returns:
String- A string representation of the polygon.
void draw(GraphWin canvas)
Draws the polygon on the provided GraphWin canvas. The polygon is added to the window and displayed.
Parameters:
canvas- TheGraphWininstance where the polygon will be drawn.
Throws:
IllegalStateException- If the polygon is already drawn.
void undraw()
Removes the polygon from the graphical window.
void drawPanel(Graphics2D graphics)
Renders the polygon onto a Graphics2D panel.
Parameters:
graphics- TheGraphics2Dobject used for rendering the polygon.