SvgGraphics2D

Inheritance: java.lang.Object

public class SvgGraphics2D

提供用于组合 Svg 图像的绘图命令。

构造函数

构造函数描述
SvgGraphics2D(int width, int height, int dpi)初始化 SvgGraphics2D 类的新实例。
SvgGraphics2D(SvgImage image)初始化 SvgGraphics2D 类的新实例。

方法

方法描述
drawImage(RasterImage image, Point origin)在指定位置绘制指定的图像。
drawImage(RasterImage image, Point origin, Size size)在指定位置以指定尺寸绘制指定的图像。
drawImage(Rectangle srcRect, Rectangle destRect, RasterImage image)在指定位置并使用指定尺寸绘制指定图像的指定部分。
drawArc(Pen pen, Rectangle rect, float startAngle, float arcAngle)绘制由 Rectangle 结构指定的椭圆部分的弧线。
fillArc(Pen pen, Brush brush, Rectangle rect, float startAngle, float arcAngle)填充由 Rectangle 结构指定的椭圆的一段弧。
drawCubicBezier(Pen pen, PointF pt1, PointF pt2, PointF pt3, PointF pt4)绘制三次贝塞尔曲线。
drawString(Font font, String text, Point origin, Color textColor)绘制文本字符串。
drawLine(Pen pen, int x1, int y1, int x2, int y2)绘制直线。
drawPath(Pen pen, GraphicsPath path)绘制路径。
fillPath(Pen pen, Brush brush, GraphicsPath path)填充路径。
drawRectangle(Pen pen, int x, int y, int width, int height)绘制矩形。
fillRectangle(Pen pen, Brush brush, int x, int y, int width, int height)填充矩形。
endRecording()获取最终的 Svg 图像,其中包括通过 SvgGraphics2D 对象执行的所有绘图命令。

Example: This example shows how to create an SVG image of the specified size and draw different shapes on it using SvgGraphics2D.

String dir = "c:\\temp\\";

int imageWidth = 600;
int imageHeight = 400;
int dpi = 96;

com.aspose.imaging.fileformats.svg.graphics.SvgGraphics2D graphics = new com.aspose.imaging.fileformats.svg.graphics.SvgGraphics2D(imageWidth, imageHeight, dpi);

// 使用 1 像素宽的黑色笔在图像边框上绘制黑色矩形。
graphics.drawRectangle(new com.aspose.imaging.Pen(com.aspose.imaging.Color.getBlack(), 1), 0, 0, imageWidth, imageHeight);

// 使用 white-smoke 颜色填充矩形。
graphics.fillRectangle(
        new com.aspose.imaging.Pen(com.aspose.imaging.Color.getWhiteSmoke(), 1),
        new com.aspose.imaging.brushes.SolidBrush(com.aspose.imaging.Color.getWhiteSmoke()), 10, 10, 580, 380);

// 使用 1 像素宽的 darkgreen 笔绘制两条对角线。
graphics.drawLine(new com.aspose.imaging.Pen(com.aspose.imaging.Color.getDarkGreen(), 1), 0, 0, imageWidth, imageHeight);
graphics.drawLine(new com.aspose.imaging.Pen(com.aspose.imaging.Color.getDarkGreen(), 1), 0, imageHeight, imageWidth, 0);

// 使用 2 像素宽的 blue 笔在矩形 {0, 0, 200, 200} 内绘制弧线。
graphics.drawArc(
        new com.aspose.imaging.Pen(com.aspose.imaging.Color.getBlue(), 2),
        new com.aspose.imaging.Rectangle(0, 0, 200, 200), 90, 270);

// 填充弧线
graphics.fillArc(new com.aspose.imaging.Pen(com.aspose.imaging.Color.getLightCoral(), 10),
        new com.aspose.imaging.brushes.SolidBrush(com.aspose.imaging.Color.getLightSkyBlue()),
        new com.aspose.imaging.Rectangle(0, 0, 150, 150), 90, 270);

// 使用 2 像素宽的 red 笔绘制三次贝塞尔曲线。
graphics.drawCubicBezier(new com.aspose.imaging.Pen(com.aspose.imaging.Color.getRed(), 2),
        new com.aspose.imaging.PointF(0, 0),
        new com.aspose.imaging.PointF(200, 133),
        new com.aspose.imaging.PointF(400, 166),
        new com.aspose.imaging.PointF(600, 400));

// 在指定位置绘制指定尺寸的光栅图像。
// 图像已缩放以适应所需的矩形。
com.aspose.imaging.RasterImage imageToDraw = (com.aspose.imaging.RasterImage) com.aspose.imaging.Image.load(dir + "sample.bmp");
try {
    graphics.drawImage(imageToDraw, new com.aspose.imaging.Point(400, 200), new com.aspose.imaging.Size(100, 50));
} finally {
    imageToDraw.dispose();
}

// 绘制文本字符串
graphics.drawString(
        new com.aspose.imaging.Font("Arial", 48, com.aspose.imaging.FontStyle.Regular),
        "Hello World!",
        new com.aspose.imaging.Point(200, 300),
        com.aspose.imaging.Color.getDarkRed());

// 创建用于填充的路径
com.aspose.imaging.Figure figureToFill = new com.aspose.imaging.Figure();
figureToFill.setClosed(true);

com.aspose.imaging.GraphicsPath pathToFill = new com.aspose.imaging.GraphicsPath();
pathToFill.addFigure(figureToFill);

figureToFill.addShapes(new com.aspose.imaging.Shape[]
        {
                new com.aspose.imaging.shapes.ArcShape(new com.aspose.imaging.RectangleF(400, 0, 200, 100), 45, 300),
                new com.aspose.imaging.shapes.BezierShape(
                        new com.aspose.imaging.PointF[]
                                {
                                        new com.aspose.imaging.PointF(300, 200),
                                        new com.aspose.imaging.PointF(400, 200),
                                        new com.aspose.imaging.PointF(500, 100),
                                        new com.aspose.imaging.PointF(600, 200),
                                }),
                new com.aspose.imaging.shapes.PolygonShape(
                        new com.aspose.imaging.PointF[]
                                {
                                        new com.aspose.imaging.PointF(300, 100),
                                }),
                new com.aspose.imaging.shapes.RectangleShape(
                        new com.aspose.imaging.RectangleF(0, 100, 200, 200)),
        });

// 使用黄色画刷和绿色笔填充路径并绘制轮廓
graphics.fillPath(
        new com.aspose.imaging.Pen(com.aspose.imaging.Color.getGreen(), 2),
        new com.aspose.imaging.brushes.SolidBrush(com.aspose.imaging.Color.getYellow()), pathToFill);

// 创建用于绘制的路径
com.aspose.imaging.GraphicsPath pathToDraw = new com.aspose.imaging.GraphicsPath();
com.aspose.imaging.Figure figureToDraw = new com.aspose.imaging.Figure();
pathToDraw.addFigure(figureToDraw);

figureToDraw.addShapes(new com.aspose.imaging.Shape[]
        {
                new com.aspose.imaging.shapes.ArcShape(new com.aspose.imaging.RectangleF(200, 200, 200, 200), 0, 360),
        });

// 使用 5 像素宽的橙色笔绘制路径。
graphics.drawPath(new com.aspose.imaging.Pen(com.aspose.imaging.Color.getOrange(), 5), pathToDraw);

// 获取包含所有绘图命令的最终 SVG 图像
com.aspose.imaging.fileformats.svg.SvgImage svgImage = graphics.endRecording();
try {
    svgImage.save(dir + "test.output.svg");
} finally {
    svgImage.dispose();
}

SvgGraphics2D(int width, int height, int dpi)

public SvgGraphics2D(int width, int height, int dpi)

初始化 SvgGraphics2D 类的新实例。

Parameters:

参数类型描述
widthint输出 Svg 图像的宽度。
heightint输出 Svg 图像的宽度。
dpiint设备分辨率,例如每英寸 96 点。

SvgGraphics2D(SvgImage image)

public SvgGraphics2D(SvgImage image)

初始化 SvgGraphics2D 类的新实例。

Parameters:

参数类型描述
imageSvgImage用于执行绘图操作的图像。

drawImage(RasterImage image, Point origin)

public final void drawImage(RasterImage image, Point origin)

在指定位置绘制指定的图像。

Parameters:

参数类型描述
imageRasterImage已绘制的图像。
originPoint已绘制图像的位置。

drawImage(RasterImage image, Point origin, Size size)

public final void drawImage(RasterImage image, Point origin, Size size)

在指定位置以指定尺寸绘制指定的图像。

Parameters:

参数类型描述
imageRasterImage已绘制的图像。
originPoint已绘制图像的位置。
sizeSize绘制图像的期望大小。

drawImage(Rectangle srcRect, Rectangle destRect, RasterImage image)

public final void drawImage(Rectangle srcRect, Rectangle destRect, RasterImage image)

在指定位置并使用指定尺寸绘制指定图像的指定部分。

Parameters:

参数类型描述
srcRectRectangle要绘制的图像对象的部分。
destRectRectangle绘制图像的位置和大小。图像会缩放以适应矩形。
imageRasterImage要绘制的图像。

drawArc(Pen pen, Rectangle rect, float startAngle, float arcAngle)

public final void drawArc(Pen pen, Rectangle rect, float startAngle, float arcAngle)

绘制由 Rectangle 结构指定的椭圆部分的弧线。

Parameters:

参数类型描述
penPen用于绘制图形轮廓的笔。
rectRectangle椭圆的边界。
startAnglefloat从 x 轴顺时针测量到弧线起点的角度(度)。
arcAnglefloat从 startAngle 参数顺时针测量到弧线终点的角度(度)。

fillArc(Pen pen, Brush brush, Rectangle rect, float startAngle, float arcAngle)

public final void fillArc(Pen pen, Brush brush, Rectangle rect, float startAngle, float arcAngle)

填充由 Rectangle 结构指定的椭圆的一段弧。

Parameters:

参数类型描述
penPen用于绘制图形轮廓的笔。
brushBrush用于填充图形内部的画刷。
rectRectangle椭圆的边界。
startAnglefloat从 x 轴顺时针测量到弧线起点的角度(度)。
arcAnglefloat从 startAngle 参数顺时针测量到弧线终点的角度(度)。

drawCubicBezier(Pen pen, PointF pt1, PointF pt2, PointF pt3, PointF pt4)

public final void drawCubicBezier(Pen pen, PointF pt1, PointF pt2, PointF pt3, PointF pt4)

绘制三次贝塞尔曲线。

Parameters:

参数类型描述
penPen决定图形颜色、宽度和样式的笔。
pt1PointF曲线的起始点。
pt2PointF曲线的第一个控制点。
pt3PointF曲线的第二个控制点。
pt4PointF曲线的结束点。

drawString(Font font, String text, Point origin, Color textColor)

public final void drawString(Font font, String text, Point origin, Color textColor)

绘制文本字符串。

Parameters:

参数类型描述
fontFont用于呈现文本的字体。
文本java.lang.StringUnicode 文本字符串。
originPoint文本运行的左上角。
textColorColor文本颜色。

drawLine(Pen pen, int x1, int y1, int x2, int y2)

public final void drawLine(Pen pen, int x1, int y1, int x2, int y2)

绘制直线。

Parameters:

参数类型描述
penPen决定图形颜色、宽度和样式的笔。
x1int第一个点的 x 坐标。
y1int第一个点的 y 坐标。
x2int第二个点的 x 坐标。
y2int第二个点的 y 坐标。

drawPath(Pen pen, GraphicsPath path)

public final void drawPath(Pen pen, GraphicsPath path)

绘制路径。

Parameters:

参数类型描述
penPen用于绘制图形轮廓的笔。
pathGraphicsPath要绘制的路径。

fillPath(Pen pen, Brush brush, GraphicsPath path)

public final void fillPath(Pen pen, Brush brush, GraphicsPath path)

填充路径。

Parameters:

参数类型描述
penPen用于绘制图形轮廓的笔。
brushBrush用于填充图形内部的画刷。
pathGraphicsPath要绘制的路径。

drawRectangle(Pen pen, int x, int y, int width, int height)

public final void drawRectangle(Pen pen, int x, int y, int width, int height)

绘制矩形。

Parameters:

参数类型描述
penPen用于绘制图形轮廓的笔。
xint要绘制的矩形左上角的 x 坐标。
yint要绘制的矩形左上角的 y 坐标。
widthint要绘制的矩形的宽度。
heightint要绘制的矩形的高度。

fillRectangle(Pen pen, Brush brush, int x, int y, int width, int height)

public final void fillRectangle(Pen pen, Brush brush, int x, int y, int width, int height)

填充矩形。

Parameters:

参数类型描述
penPen用于绘制图形轮廓的笔。
brushBrush用于填充图形内部的画刷。
xint要绘制的矩形左上角的 x 坐标。
yint要绘制的矩形左上角的 y 坐标。
widthint要绘制的矩形的宽度。
heightint要绘制的矩形的高度。

endRecording()

public final SvgImage endRecording()

获取最终的 Svg 图像,其中包括通过 SvgGraphics2D 对象执行的所有绘图命令。

Returns: SvgImage - The final Svg image.