Class DrawOp

java.lang.Object
cloud.opencode.base.image.draw.DrawOp

public final class DrawOp extends Object
Drawing Operations for Raster Raster 绘图操作工具

Provides static methods to draw geometric shapes, lines, arrows, and text onto images. All methods return a NEW Raster and never modify the input.

提供在图像上绘制几何形状、线条、箭头和文本的静态方法。 所有方法返回新的 Raster,不修改输入图像。

Features | 主要功能:

  • Draw lines with configurable thickness - 绘制可配置粗细的线条
  • Draw rectangles (outline or filled) - 绘制矩形(轮廓或填充)
  • Draw circles and ellipses (outline or filled) - 绘制圆和椭圆(轮廓或填充)
  • Draw polygons (outline or filled) - 绘制多边形(轮廓或填充)
  • Draw arrows with arrowheads - 绘制带箭头的线条
  • Draw text with configurable font and color - 绘制可配置字体和颜色的文本

Usage Examples | 使用示例:

// Draw a red line
Raster result = DrawOp.line(image, 10, 10, 100, 100, Color.RED, 2);

// Draw a filled blue rectangle
Raster result = DrawOp.rect(image, 20, 20, 80, 60, Color.BLUE, 1, true);

// Draw a green circle outline
Raster result = DrawOp.circle(image, 50, 50, 30, Color.GREEN, 2, false);

// Draw text
Raster result = DrawOp.text(image, "Hello", 10, 30, new Font("Arial", Font.PLAIN, 16), Color.BLACK);

Security | 安全性:

  • Thread-safe: Yes (stateless, all methods are pure functions) - 线程安全: 是(无状态,所有方法均为纯函数)
  • Null-safe: No (null parameters throw IllegalArgumentException) - 空值安全: 否(null 参数抛出 IllegalArgumentException)
  • Input immutability: All methods copy the input image - 输入不可变性: 所有方法复制输入图像
Since:
JDK 25, opencode-base-image V2.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    arrow(Raster raster, int x1, int y1, int x2, int y2, int argb, int thickness)
    Draw an arrow on a Raster from (x1, y1) to (x2, y2).
    static void
    circle(Raster raster, int cx, int cy, int radius, int argb, int thickness, boolean fill)
    Draw a circle on a Raster, outline or filled.
    static void
    ellipse(Raster raster, int cx, int cy, int rx, int ry, int argb, int thickness, boolean fill)
    Draw an ellipse on a Raster, outline or filled.
    static void
    line(Raster raster, int x1, int y1, int x2, int y2, int argb, int thickness)
    Draw a line on a Raster from (x1, y1) to (x2, y2).
    static void
    polygon(Raster raster, int[] xPoints, int[] yPoints, int argb, int thickness, boolean fill)
    Draw a polygon on a Raster, outline or filled.
    static void
    rect(Raster raster, int x, int y, int w, int h, int argb, int thickness, boolean fill)
    Draw a rectangle on a Raster, outline or filled.

    Methods inherited from class Object

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

    • line

      public static void line(Raster raster, int x1, int y1, int x2, int y2, int argb, int thickness)
      Draw a line on a Raster from (x1, y1) to (x2, y2). 在 Raster 上绘制从 (x1, y1)(x2, y2) 的线条。

      Mutates the raster in place using Bresenham's line algorithm with thickness implemented as filled circles centered at every line pixel.

      原地修改 raster:使用 Bresenham 算法配合在每个线条像素处填充圆来实现粗细。

      Parameters:
      raster - target raster (modified in place) | 目标 raster(原地修改)
      x1 - start x | 起点 x
      y1 - start y | 起点 y
      x2 - end x | 终点 x
      y2 - end y | 终点 y
      argb - packed ARGB color | 打包的 ARGB 颜色
      thickness - line thickness in pixels (must be > 0) | 线条粗细(> 0)
      Throws:
      NullPointerException - if raster is null | 当 raster 为 null 时抛出
      ImageOperationException - if thickness <= 0 | 当 thickness <= 0 时抛出
      Since:
      opencode-base-image V1.0.4
    • rect

      public static void rect(Raster raster, int x, int y, int w, int h, int argb, int thickness, boolean fill)
      Draw a rectangle on a Raster, outline or filled. 在 Raster 上绘制矩形(轮廓或填充)。
      Parameters:
      raster - target raster (modified in place) | 目标 raster(原地修改)
      x - top-left x | 左上 x
      y - top-left y | 左上 y
      w - width | 宽度
      h - height | 高度
      argb - packed ARGB color | 打包的 ARGB 颜色
      thickness - stroke thickness (ignored when fill) | 线宽(fill 时忽略)
      fill - true for filled, false for outline | true 填充,false 轮廓
      Throws:
      NullPointerException - if raster is null | 当 raster 为 null 时抛出
      ImageOperationException - if thickness <= 0 | 当 thickness <= 0 时抛出
      Since:
      opencode-base-image V1.0.4
    • circle

      public static void circle(Raster raster, int cx, int cy, int radius, int argb, int thickness, boolean fill)
      Draw a circle on a Raster, outline or filled. 在 Raster 上绘制圆(轮廓或填充)。
      Parameters:
      raster - target raster (modified in place) | 目标 raster(原地修改)
      cx - center x | 圆心 x
      cy - center y | 圆心 y
      radius - circle radius (must be > 0) | 半径(> 0)
      argb - packed ARGB color | 打包的 ARGB 颜色
      thickness - stroke thickness (ignored when fill) | 线宽(fill 时忽略)
      fill - true for filled, false for outline | true 填充,false 轮廓
      Throws:
      NullPointerException - if raster is null | 当 raster 为 null 时抛出
      ImageOperationException - if thickness <= 0 | 当 thickness <= 0 时抛出
      Since:
      opencode-base-image V1.0.4
    • ellipse

      public static void ellipse(Raster raster, int cx, int cy, int rx, int ry, int argb, int thickness, boolean fill)
      Draw an ellipse on a Raster, outline or filled. 在 Raster 上绘制椭圆(轮廓或填充)。
      Parameters:
      raster - target raster (modified in place) | 目标 raster(原地修改)
      cx - center x | 椭圆中心 x
      cy - center y | 椭圆中心 y
      rx - horizontal radius | 水平半径
      ry - vertical radius | 垂直半径
      argb - packed ARGB color | 打包的 ARGB 颜色
      thickness - stroke thickness (ignored when fill) | 线宽(fill 时忽略)
      fill - true for filled, false for outline | true 填充,false 轮廓
      Throws:
      NullPointerException - if raster is null | 当 raster 为 null 时抛出
      ImageOperationException - if thickness <= 0 | 当 thickness <= 0 时抛出
      Since:
      opencode-base-image V1.0.4
    • polygon

      public static void polygon(Raster raster, int[] xPoints, int[] yPoints, int argb, int thickness, boolean fill)
      Draw a polygon on a Raster, outline or filled. 在 Raster 上绘制多边形(轮廓或填充)。

      Outline mode draws lines between consecutive vertices and closes the polygon. Fill mode uses an even-odd scanline fill rule.

      轮廓模式连接相邻顶点并闭合;填充模式使用奇偶扫描线规则。

      Parameters:
      raster - target raster (modified in place) | 目标 raster(原地修改)
      xPoints - x coordinates (length >= 3) | x 坐标数组(长度 >= 3)
      yPoints - y coordinates (length matches xPoints) | y 坐标数组(与 xPoints 等长)
      argb - packed ARGB color | 打包的 ARGB 颜色
      thickness - stroke thickness (ignored when fill) | 线宽(fill 时忽略)
      fill - true for filled, false for outline | true 填充,false 轮廓
      Throws:
      NullPointerException - if raster, xPoints, or yPoints is null
      ImageOperationException - if length mismatch or fewer than 3 points
      Since:
      opencode-base-image V1.0.4
    • arrow

      public static void arrow(Raster raster, int x1, int y1, int x2, int y2, int argb, int thickness)
      Draw an arrow on a Raster from (x1, y1) to (x2, y2). 在 Raster 上绘制从 (x1, y1)(x2, y2) 的箭头。
      Parameters:
      raster - target raster (modified in place) | 目标 raster(原地修改)
      x1 - start x | 起点 x
      y1 - start y | 起点 y
      x2 - arrowhead x | 箭头端 x
      y2 - arrowhead y | 箭头端 y
      argb - packed ARGB color | 打包的 ARGB 颜色
      thickness - line thickness | 线条粗细
      Throws:
      NullPointerException - if raster is null | 当 raster 为 null 时抛出
      ImageOperationException - if thickness <= 0 | 当 thickness <= 0 时抛出
      Since:
      opencode-base-image V1.0.4