翻譯|行業(yè)資訊|編輯:胡濤|2024-09-02 11:16:40.863|閱讀 101 次
概述:這篇博文將更進一步,向您展示如何在 Java 中繪制幾何形狀
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
最近,我們使用Aspose.PSD for Java實現(xiàn)了繪制諸如日食和線條等形狀的功能。然而,這篇博文將更進一步,向您展示如何在 Java 中繪制幾何形狀。幸運的是,您可以使用這個Java 繪圖庫以編程方式執(zhí)行此操作,因為它是一個完整的包,可以在 Java 應(yīng)用程序中處理形狀。因此,沒有額外的要求,我們可以直接進入安裝和實施部分。
Aspose.PSD 是高級PSD文件格式操作API,沒有任何Adobe Photoshop依賴項。API允許創(chuàng)建或編輯Photoshop文件,并提供更新圖層屬性,添加水印,執(zhí)行圖形操作或?qū)⒁环N文件格式轉(zhuǎn)換為另一種文件的功能。支持流行文件格式處理,并允許將各類文檔導出或轉(zhuǎn)換為固定布局文件格式和最常用的圖像/多媒體格式。
現(xiàn)在,您可以轉(zhuǎn)到此 安裝 指南查看所有提到的詳細信息。但是,您可以 在此處下載 JAR 文件。
Maven 配置:
<repositories> <repository> <id>snapshots</id> <name>repo</name> <url>//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>
安裝完成后,您可以按照以下步驟操作:
以下代碼示例演示了如何使用 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還提供了許多功能。因此,我們已經(jīng)介紹了如何使用此Java 繪圖庫創(chuàng)建矩形和圓弧。果您還有其他疑問,歡迎查閱本系列其他教程,或者私信我們獲取幫助~
歡迎下載|體驗更多Aspose文檔管理產(chǎn)品
獲取更多信息請咨詢 或 加入Aspose技術(shù)交流群(761297826)
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn