Oval - CoolPotato31F/Java-Graphics GitHub Wiki

Oval Class

Overview

The Oval class is part of the graphics package and implements the GraphicsObject interface. It represents a drawable oval shape defined by two diagonal points and supports various operations, including movement, resizing, and rendering.

Attributes

  • Point point1 - The top-left boundary of the oval.
  • Point point2 - The bottom-right boundary of the oval.
  • Point center - The center of the oval.
  • int width - The outline width of the oval.
  • int w - The oval's width.
  • int h - The oval's height.
  • GraphWin canvas - Reference to the canvas where the oval is drawn.
  • Color fillColor - The fill color of the oval.
  • Color outlineColor - The outline color of the oval (default: BLACK).

Constructors

Oval(Point p1, Point p2)

Creates an oval defined by two diagonal points.

Parameters:

  • Point p1 - One boundary point of the oval.
  • Point p2 - The opposite boundary point of the oval.

Methods

void draw(GraphWin canvas)

Draws the oval on the given canvas.

Parameters:

  • GraphWin canvas - The canvas to draw on.

Throws:

  • Error if the oval is already drawn.

void undraw()

Removes the oval from the canvas.

void setFill(Color color)

Sets the fill color of the oval.

Parameters:

  • Color color - The color to fill the oval with.

void setOutline(Color color)

Sets the outline color of the oval.

Parameters:

  • Color color - The outline color.

String toString()

Returns a string representation of the oval in the format:

Oval(Point(x1, y1), Point(x2, y2))

Point getP1()

Returns the first defining point (top-left boundary) of the oval.

Point getP2()

Returns the second defining point (bottom-right boundary) of the oval.

Point getCenter()

Returns the center point of the oval.

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 oval.

Parameters:

  • int width - The width of the outline.

int getWidth()

Returns the outline width of the oval.

void move(double dx, double dy)

Moves the oval 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 oval onto a Graphics2D panel.

Parameters:

  • Graphics2D graphics - The graphics object used for rendering.

See Also