借助图形控件Aspose.PSD, 在 Java 中绘制几何形状

最近,我们使用Aspose.PSD for Java实现了绘制诸如日食和线条等形状的功能。然而,这篇博文将更进一步,向您展示如何在 Java 中绘制几何形状。幸运的是,您可以使用这个Java 绘图库以编程方式执行此操作,因为它是一个完整的包,可以在 Java 应用程序中处理形状。因此,没有额外的要求,我们可以直接进入安装和实施部分。

Aspose.PSD 是高级PSD文件格式操作API,没有任何Adobe Photoshop依赖项。API允许创建或编辑Photoshop文件,并提供更新图层属性,添加水印,执行图形操作或将一种文件格式转换为另一种文件的功能。Aspose API支持流行文件格式处理,并允许将各类文档导出或转换为固定布局文件格式和最常用的图像/多媒体格式。

Aspose.PSD 最新下载   

Java绘图库安装

现在,您可以转到此 安装 指南查看所有提到的详细信息。但是,您可以 在此处下载 JAR 文件。

Maven 配置:

<repositories>
<repository>
<id>snapshots</id>
<name>repo</name>
<url>http://repository.aspose.com/repo/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-psd</artifactId>
<version>24.4</version>
<classifier>jdk16</classifier>
</dependency>
</dependencies>

java绘图库安装

在 Java 中绘制矩形 - 代码示例

安装完成后,您可以按照以下步骤操作:

  • 创建BmpOptions类的实例。
  • 调用setBitsPerPixel方法来设置每像素的位数。
  • 创建Image类的实例并用PsdImage类的对象初始化它。
  • 使用Image 类的对象实例化Graphics类的实例。
  • 通过调用getYellow方法设置图像的背景颜色。
  • drawRectangle方法将绘制一个矩形。
  • 通过调用保存方法将图像导出为BMP文件格式。

以下代码示例演示了如何使用 Aspose.PSD for Java 绘制矩形:

public class Main {
// Drawing rectangle in java
public static void main(String[] args) throws Exception {

String outpath = "/files/rectangle.bmp";
// Create an instance of BmpOptions class.
BmpOptions saveOptions = new BmpOptions();
// Invoke the setBitsPerPixel method to set the bits per pixel.
saveOptions.setBitsPerPixel(32);
// Create an instance of Image class and initialize it with the object of PsdImage class.
try (PsdImage image = new PsdImage(100, 100)) {
// Instantiate an instance of the Graphics class with the object of the Image class.
Graphics graphic = new Graphics(image);
// Set the background color of the image by calling the getYellow method.
graphic.clear(Color.getYellow());
// The drawRectangle method will draw a rectangle.
graphic.drawRectangle(new Pen(Color.getRed()), new RectangleF(30, 10, 40, 80));
graphic.drawRectangle(new Pen(new SolidBrush(Color.getBlue())), new RectangleF(10, 30, 80, 40));
// Export image to bmp file format by calling the save method.
image.save(outpath, saveOptions);
}
}
}

输出:

如何绘制矩形

通过编程绘制圆弧 - 代码示例

类似地,您可以按照下面提到的代码片段在 Java 中绘制圆弧:

public class Main {
// Draw an Arc
public static void main(String[] args) throws Exception {
String outpath = "/file/arc.bmp";
// Create an instance of BmpOptions class and invoke the setBitsPerPixel method to set the bits per pixel. .
BmpOptions saveOption = new BmpOptions();
saveOption.setBitsPerPixel(32);
// Create an instance of Image class and initialize it with the object of PsdImage class.
try (PsdImage image = new PsdImage(100, 100)) {
// Create and initialize an instance of Graphics class and clear Graphics surface
Graphics graphic = new Graphics(image);
graphic.clear(Color.getYellow());
// Draw an arc shape by specifying the Pen object having red black color and coordinates, height, width, start & end angles
int width = 100;
int height = 200;
int startAngle = 45;
int sweepAngle = 270;
// Draw arc to screen and save all changes.
graphic.drawArc(new Pen(Color.getBlack()), 0, 0, width, height, startAngle, sweepAngle);
// Save the image to bmp file format.
image.save(outpath, saveOption);
}
}
}

输出:

画圆弧

结论

总而言之,除了在 Java 中绘制几何形状外,Aspose.PSD for Java还提供了许多功能。因此,我们已经介绍了如何使用此Java 绘图库创建矩形和圆弧。果您还有其他疑问,欢迎查阅本系列其他教程,或者私信我获取帮助~

  • 18
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值