Class DrawOp

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

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

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

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

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
BufferedImage result = DrawOp.line(image, 10, 10, 100, 100, Color.RED, 2);

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

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

// Draw text
BufferedImage 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 Details

    • line

      public static BufferedImage line(BufferedImage image, int x1, int y1, int x2, int y2, Color color, int thickness)
      Draw a line from (x1, y1) to (x2, y2) 从 (x1, y1) 到 (x2, y2) 绘制线条
      Parameters:
      image - the source image | 源图像
      x1 - start x coordinate | 起点 x 坐标
      y1 - start y coordinate | 起点 y 坐标
      x2 - end x coordinate | 终点 x 坐标
      y2 - end y coordinate | 终点 y 坐标
      color - the line color | 线条颜色
      thickness - the line thickness in pixels | 线条粗细(像素)
      Returns:
      a new BufferedImage with the line drawn | 绘制了线条的新 BufferedImage
      Throws:
      ImageOperationException - if image is null, color is null, or thickness <= 0
    • rect

      public static BufferedImage rect(BufferedImage image, int x, int y, int w, int h, Color color, int thickness, boolean fill)
      Draw a rectangle (outline or filled) 绘制矩形(轮廓或填充)
      Parameters:
      image - the source image | 源图像
      x - top-left x coordinate | 左上角 x 坐标
      y - top-left y coordinate | 左上角 y 坐标
      w - width | 宽度
      h - height | 高度
      color - the rectangle color | 矩形颜色
      thickness - the stroke thickness in pixels | 线条粗细(像素)
      fill - true to fill, false for outline | true 填充, false 仅轮廓
      Returns:
      a new BufferedImage with the rectangle drawn | 绘制了矩形的新 BufferedImage
      Throws:
      ImageOperationException - if image is null, color is null, or thickness <= 0
    • circle

      public static BufferedImage circle(BufferedImage image, int cx, int cy, int radius, Color color, int thickness, boolean fill)
      Draw a circle (outline or filled) 绘制圆形(轮廓或填充)
      Parameters:
      image - the source image | 源图像
      cx - center x coordinate | 圆心 x 坐标
      cy - center y coordinate | 圆心 y 坐标
      radius - the circle radius | 圆的半径
      color - the circle color | 圆的颜色
      thickness - the stroke thickness in pixels | 线条粗细(像素)
      fill - true to fill, false for outline | true 填充, false 仅轮廓
      Returns:
      a new BufferedImage with the circle drawn | 绘制了圆形的新 BufferedImage
      Throws:
      ImageOperationException - if image is null, color is null, or thickness <= 0
    • ellipse

      public static BufferedImage ellipse(BufferedImage image, int cx, int cy, int rx, int ry, Color color, int thickness, boolean fill)
      Draw an ellipse (outline or filled) 绘制椭圆(轮廓或填充)
      Parameters:
      image - the source image | 源图像
      cx - center x coordinate | 椭圆中心 x 坐标
      cy - center y coordinate | 椭圆中心 y 坐标
      rx - horizontal radius | 水平半径
      ry - vertical radius | 垂直半径
      color - the ellipse color | 椭圆颜色
      thickness - the stroke thickness in pixels | 线条粗细(像素)
      fill - true to fill, false for outline | true 填充, false 仅轮廓
      Returns:
      a new BufferedImage with the ellipse drawn | 绘制了椭圆的新 BufferedImage
      Throws:
      ImageOperationException - if image is null, color is null, or thickness <= 0
    • polygon

      public static BufferedImage polygon(BufferedImage image, int[] xPoints, int[] yPoints, Color color, int thickness, boolean fill)
      Draw a polygon (outline or filled) 绘制多边形(轮廓或填充)
      Parameters:
      image - the source image | 源图像
      xPoints - array of x coordinates | x 坐标数组
      yPoints - array of y coordinates | y 坐标数组
      color - the polygon color | 多边形颜色
      thickness - the stroke thickness in pixels | 线条粗细(像素)
      fill - true to fill, false for outline | true 填充, false 仅轮廓
      Returns:
      a new BufferedImage with the polygon drawn | 绘制了多边形的新 BufferedImage
      Throws:
      ImageOperationException - if image is null, color is null, thickness <= 0, or point arrays are null/mismatched
    • arrow

      public static BufferedImage arrow(BufferedImage image, int x1, int y1, int x2, int y2, Color color, int thickness)
      Draw an arrow from (x1, y1) to (x2, y2) with an arrowhead at the endpoint 从 (x1, y1) 到 (x2, y2) 绘制带箭头的线条
      Parameters:
      image - the source image | 源图像
      x1 - start x coordinate | 起点 x 坐标
      y1 - start y coordinate | 起点 y 坐标
      x2 - end x coordinate (arrowhead) | 终点 x 坐标(箭头端)
      y2 - end y coordinate (arrowhead) | 终点 y 坐标(箭头端)
      color - the arrow color | 箭头颜色
      thickness - the line thickness in pixels | 线条粗细(像素)
      Returns:
      a new BufferedImage with the arrow drawn | 绘制了箭头的新 BufferedImage
      Throws:
      ImageOperationException - if image is null, color is null, or thickness <= 0
    • text

      public static BufferedImage text(BufferedImage image, String text, int x, int y, Font font, Color color)
      Draw text at the specified position 在指定位置绘制文本
      Parameters:
      image - the source image | 源图像
      text - the text to draw | 要绘制的文本
      x - the x coordinate of the text baseline start | 文本基线起始 x 坐标
      y - the y coordinate of the text baseline | 文本基线 y 坐标
      font - the font to use | 使用的字体
      color - the text color | 文本颜色
      Returns:
      a new BufferedImage with the text drawn | 绘制了文本的新 BufferedImage
      Throws:
      ImageOperationException - if image, text, font, or color is null