Class Rectangle

java.lang.Object
com.example.renderer.factory.Rectangle
All Implemented Interfaces:
Shape

public class Rectangle extends Object implements Shape
  • Constructor Summary

    Constructors
    Constructor
    Description
    Default constructor required for serialization.
    Rectangle(int x, int y, int width, int height)
    Creates a new Rectangle with specified dimensions.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    接受访问者访问,实现访问者模式。 允许外部访问者对图形对象进行操作,而不需要修改图形类本身。
    int
    获取矩形高度
    int
    获取矩形宽度
    int
    获取矩形左上角X坐标
    int
    获取矩形左上角Y坐标
    void
    move(int dx, int dy)
    移动图形的位置。 根据给定的偏移量调整图形的坐标位置,正数表示向右/下移动,负数表示向左/上移动。
    void
    render(Renderer renderer)
    使用指定的渲染器绘制图形。

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • Rectangle

      public Rectangle()
      Default constructor required for serialization.
    • Rectangle

      public Rectangle(int x, int y, int width, int height)
      Creates a new Rectangle with specified dimensions.
      Parameters:
      x - the x coordinate of top-left corner
      y - the y coordinate of top-left corner
      width - the width of rectangle (must be > 0)
      height - the height of rectangle (must be > 0)
      Throws:
      IllegalArgumentException - 如果宽度或高度不合法 (<=0)
  • Method Details

    • render

      public void render(Renderer renderer)
      Description copied from interface: Shape
      使用指定的渲染器绘制图形。

      实现类应确保:

      • 参数renderer不为null
      • 正确调用renderer的对应绘制方法
      • 处理渲染器抛出的异常
      Specified by:
      render in interface Shape
      Parameters:
      renderer - 用于绘制图形的渲染器实现
    • accept

      public void accept(ExportVisitor visitor)
      Description copied from interface: Shape
      接受访问者访问,实现访问者模式。 允许外部访问者对图形对象进行操作,而不需要修改图形类本身。
      Specified by:
      accept in interface Shape
      Parameters:
      visitor - 用于处理图形的访问者对象,不能为null
    • move

      public void move(int dx, int dy)
      Description copied from interface: Shape
      移动图形的位置。 根据给定的偏移量调整图形的坐标位置,正数表示向右/下移动,负数表示向左/上移动。
      Specified by:
      move in interface Shape
      Parameters:
      dx - X轴方向的移动距离(像素)
      dy - Y轴方向的移动距离(像素)
    • getX

      public int getX()
      获取矩形左上角X坐标
      Returns:
      矩形左上角的X坐标值
    • getY

      public int getY()
      获取矩形左上角Y坐标
      Returns:
      矩形左上角的Y坐标值
    • getWidth

      public int getWidth()
      获取矩形宽度
      Returns:
      矩形的宽度值
    • getHeight

      public int getHeight()
      获取矩形高度
      Returns:
      矩形的高度值