GraphicsObject - CoolPotato31F/Java-Graphics GitHub Wiki

GraphicsObject Interface

Overview

The GraphicsObject interface defines the fundamental operations for graphical objects in the graphics package. It provides methods for rendering, removing, and managing objects within a GraphWin canvas or a Graphics2D panel. This interface is essential for creating drawable objects that can be dynamically manipulated in a graphical environment.

Features

  • Standardized interface for drawable objects
  • Supports rendering on GraphWin canvas
  • Enables removal of objects from the canvas
  • Provides Graphics2D support for advanced rendering

Interface Definition

package graphics;

import java.awt.Graphics2D;

public interface GraphicsObject {
    void draw(GraphWin canvas);
    void undraw();
    void drawPanel(Graphics2D graphics);
}

Methods

draw(GraphWin canvas)

void draw(GraphWin canvas)

Draws the graphical object on the specified GraphWin canvas.

  • Parameters:
    • canvas: The GraphWin instance where the object will be drawn.
  • Usage:
GraphicsObject obj = new Circle(new Point(100, 100), 50);
obj.draw(window);

undraw()

void undraw()

Removes the graphical object from the GraphWin canvas.

  • Usage:
obj.undraw();

drawPanel(Graphics2D graphics)

void drawPanel(Graphics2D graphics)

Draws the object using a Graphics2D panel, allowing integration with Java's 2D graphics API.

  • Parameters:
    • graphics: The Graphics2D object used for rendering.
  • Usage:
@Override
public void drawPanel(Graphics2D g) {
    g.setColor(Color.RED);
    g.fillOval(50, 50, 100, 100);
}

Implementation Example

Circle Class Implementing GraphicsObject

public class Circle implements GraphicsObject {
    private Point center;
    private int radius;

    public Circle(Point center, int radius) {
        this.center = center;
        this.radius = radius;
    }

    @Override
    public void draw(GraphWin canvas) {
        canvas.addItem(this);
    }

    @Override
    public void undraw() {
        // Logic to remove object
    }

    @Override
    public void drawPanel(Graphics2D g) {
        g.fillOval(center.getX() - radius, center.getY() - radius, radius * 2, radius * 2);
    }
}

Integration with GraphWin

This interface is meant to be used in conjunction with the GraphWin class for rendering objects dynamically. It allows users to create custom graphical elements that can be added or removed at runtime.

License

GraphicsObject is released under the MIT License.

See Also