没有任何图形的文本数据会使商业文档变得枯燥无味。事实上,图形插图可用于吸引读者对整个文档的注意力,读者可以轻松了解数据的要点。本文面向以编程方式创建和操作文档/图像的 Java 程序员。因此,Aspose.PSD for Java是一个功能强大的Java 图形库,用于在 Java 中绘制形状,例如线条、日食和矩形。因此,在本指南中,我们将实现如何使用此 Java 绘图 API 绘制线条。此外,我们还将介绍如何通过编写几行源代码在 Java 中绘制 Eclipse 。
Aspose.PSD 是高级PSD文件格式操作API,没有任何Adobe Photoshop依赖项。API允许创建或编辑Photoshop文件,并提供更新图层属性,添加水印,执行图形操作或将一种文件格式转换为另一种文件的功能。Aspose API支持流行文件格式处理,并允许将各类文档导出或转换为固定布局文件格式和最常用的图像/多媒体格式。
在 Java 中画一条线 - 代码示例
以下步骤和代码片段展示了如何使用Aspose.PSD for Java在 Javs 中绘制线条:
- 创建BmpOptions类的实例。
- 调用setBitsPerPixel方法来设置每像素的位数。
- 使用 PsdImage 类的实例初始化Image类的对象。
- 创建并初始化Graphics类的对象,并设置图像背景颜色。
- 调用drawLine方法,通过指定蓝色 Pen 对象和坐标 Points 绘制两条虚线对角线。
- 保存方法将把文件保存在磁盘上。
以下代码示例展示了如何以编程方式在 Java 中绘制线条:
public class Main { public static void main(String[] args) throws java.io.IOException { String dir = "/Desktop/"; String outpath = dir+"line.bmp"; // Create an instance of the BmpOptions class. BmpOptions saveOptions = new BmpOptions(); // Invoke the setBitsPerPixel method to set the Bits per Pixel. saveOptions.setBitsPerPixel(32); // Initialize an object of Image class with an instance of the PsdImage class. try (Image image = new PsdImage(100, 100)) { // Create and initialize an object of the Graphics class and set the image background color. Graphics graphic = new Graphics(image); graphic.clear(Color.getYellow()); // Call the drawLine method to draw two dotted diagonal lines by specifying the Pen object having blue color and coordinate Points. graphic.drawLine(new Pen(Color.getBlue()), 19, 19, 90, 90); graphic.drawLine(new Pen(Color.getBlue()), 19, 90, 90, 9); // The save method will save the file on disk. image.save(outpath, saveOptions); } } }
上述代码示例的输出如下图所示:
通过编程绘制日食
要在 Java 中绘制日食,请按照以下步骤和下面给出的代码示例进行操作:
- 通过指定具有红色和周围矩形的 Pen 对象来调用drawEllipse方法绘制椭圆形。
public class Main { public static void main(String[] args) throws java.io.IOException { String dir = "/Desktop/"; String outpath = dir+"eclipse.bmp"; BmpOptions saveOptions = new BmpOptions(); saveOptions.setBitsPerPixel(32); // Create an instance of Image try (Image 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()); // Invoke the drawEllipse method to draw a dotted ellipse shape by specifying the Pen object having a red color and a surrounding Rectangle. graphic.drawEllipse(new Pen(Color.getRed()), new RectangleF(30, 10, 40, 80)); // Export image to bmp file format by calling the save method. image.save(outpath, saveOptions); } } }
您可以在下图中看到输出:
结论
感谢阅读!Java 版 Aspose.PSD不仅限于绘制形状,它还提供更多功能。因此,在这篇博文中,我们介绍了如何以编程方式在 Java 中绘制线条和日食。如果您还有其他疑问,欢迎查阅本系列其他教程,或者私信我们获取帮助~