Point - CoolPotato31F/Java-Graphics GitHub Wiki

Point Class

Overview

The Point class represents a point in a 2D space that can be drawn, moved, and checked for collisions. It implements the GraphicsObject interface, allowing it to be rendered on a graphical window (GraphWin) or a Graphics2D panel.

Constructor

Point(double x1, double y1)

Creates a point at the specified coordinates.

Parameters:

  • x1 - The x-coordinate of the point.
  • y1 - The y-coordinate of the point.

Methods

double getX()

Returns the x-coordinate of the point.

double getY()

Returns the y-coordinate of the point.

void move(double dx, double dy)

Moves the point by the specified delta values.

Parameters:

  • dx - The change in x-coordinate.
  • dy - The change in y-coordinate.

void moveTo(double x, double y)

Sets the location of the point to the specified coordinates.

Parameters:

  • x - The new x-coordinate.
  • y - The new y-coordinate.

void setWidth(int width)

Sets the width (size) of the point when drawn.

Parameter:

  • width - The new width of the point.

Point clone()

Creates a new point with the same coordinates.

Returns:

  • A clone of the current point.

void draw(GraphWin canvas)

Draws the point on the specified GraphWin.

Parameter:

  • canvas - The GraphWin instance.

Throws:

  • IllegalStateException if the point is already drawn.

void undraw()

Removes the point from the graphical window.

void setOutline(Color color)

Sets the outline color of the point.

Parameter:

  • color - The new outline color.

void drawPanel(Graphics2D graphics)

Renders the point onto a Graphics2D panel.

Parameter:

  • graphics - The Graphics2D object used for rendering.

Usage Example

Point p = new Point(50, 50);
p.setWidth(5);
p.setOutline(Color.RED);
GraphWin canvas = new GraphWin("Canvas", 500, 500);
p.draw(canvas);