Java中使用Graphics2D繪圖方法小結(jié)
一、簡介
在開發(fā)中可能會遇到這樣一類場景,業(yè)務(wù)復(fù)雜度不算太高,技術(shù)難度不算太深,但是做起來就很容易把人整破防,傷害很高侮辱性很強的:繪圖。
繪圖最怕有人挑刺:這里變形,那里不對,全圖失真。
最近在處理這樣一個場景,使用Java的Graphics2D類,繪制業(yè)務(wù)需要的圖形模板,然后在具體流程中填充數(shù)據(jù),并且將圖形存儲起來,邏輯并不復(fù)雜,由于涉及ToC和ToB兩端交互,必須用點雕花的態(tài)度。
二、字體安裝
在繪制具體圖形時,需要先處理好本地字體,使用設(shè)計師提供的字體,才可能在圖片上復(fù)制出想要的效果;安裝完相關(guān)的字體包,使用Java讀取驗證后再直接使用。
public class Typeface { public static void main(String[] args) { List<String> fontNames = new java.util.ArrayList<>(); Font[] fonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts(); for (Font font : fonts){ fontNames.add(font.getName()); } fontNames.forEach(System.out::println); } }
三、繪制圖形
在制圖中,會涉及一些簡單的圖形樣式,比如線條、矩形、圓弧線等,這些都可以使用Graphics2D的語法直接生成,下面的程序創(chuàng)建一張500x500的圖片,然后在其中繪制一些簡單的圖形樣式,最后保存到本地。
public class DrawDraft { public static void main(String[] args) throws Exception { // 1、創(chuàng)建圖片繪圖 BufferedImage image = new BufferedImage(500, 500, BufferedImage.TYPE_4BYTE_ABGR); Graphics2D graphics = image.createGraphics(); graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); // 2、填充背景色 graphics.setColor(Color.white); graphics.fillRect(0, 0, 500, 500); // 3、繪制線條 graphics.setStroke(new BasicStroke(3)); graphics.setColor(Color.red); graphics.drawLine(50, 50, 280, 50); graphics.setColor(Color.blue); graphics.drawLine(50, 50, 165, 200); graphics.setColor(Color.green); graphics.drawLine(280, 50, 165, 200); // 4、繪制圖形 graphics.setStroke(new BasicStroke(2)); graphics.setColor(Color.pink); graphics.drawRect(200, 200, 80, 50);// 矩形 graphics.setColor(Color.green); graphics.drawArc(280, 280, 100, 100, 0, 180);//圓弧線 graphics.drawArc(300, 300, 100, 50, 0, -270);//圓弧線弧度 graphics.setColor(Color.orange); graphics.drawArc(350, 350, 100, 100, 0, 180);//圓弧線 graphics.fillArc(350, 350, 100, 100, 0, -270);//填充四分之三的圓形 // 5、寫到圖片 ImageIO.write(image, "png", new File("src/main/draw-draft.png")); image.flush(); graphics.dispose(); } }
四、繪制文本
在常規(guī)的業(yè)務(wù)場景中,一般是先繪制模版圖形,然后在模板的圖形上填充數(shù)據(jù),也可以直接使用設(shè)計師提供的模板文件,這樣可以避免數(shù)據(jù)填充時出現(xiàn)排版問題,如果有大量的動態(tài)數(shù)據(jù)內(nèi)容,可以使用模板引擎,這在以前的內(nèi)容中有寫個類似的案例。
下面這個案例,使用上面的模板,在此模版上進行文本添加,繪制文本主要就是一些動態(tài)對齊和排版等問題,最后制圖生效時添加簽章即可。
import java.awt.Color; import java.awt.Font; import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.image.BufferedImage; import java.io.File; import javax.imageio.ImageIO; public class DrawImage { public static void main(String[] args) throws Exception { // 1、基礎(chǔ)樣式 Font yhFont = new Font("Microsoft Yahei UI", Font.PLAIN, 15); Font yhBoldFont = new Font("Microsoft Yahei UI Bold", Font.BOLD, 25); Font tailFont = new Font("Microsoft Yahei UI Bold", Font.PLAIN, 12); // 2、基于底圖繪制 BufferedImage backImg = ImageIO.read(new File("src/main/draw-draft.png")); int canvasWidth = backImg.getWidth(); int canvasHeight = backImg.getHeight(); // 3、創(chuàng)建畫筆 Graphics2D graphics = backImg.createGraphics(); graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); // 4、繪制居中標(biāo)題 graphics.setFont(yhBoldFont); graphics.setColor(Color.BLACK); String title = "2D繪圖"; int titleWidth = graphics.getFontMetrics().stringWidth(title); int titleX = canvasWidth/2-titleWidth/2; int titleY = 50; graphics.drawString(title, titleX, titleY); // 5、繪制長文本,左對齊和換行 graphics.setFont(yhFont); graphics.setColor(Color.BLACK); String blackText = "\u3000組織需要重新審視項目的核心價值主張,以便更好地與利益相關(guān)者對齊目標(biāo),協(xié)同共創(chuàng)。"; String[] textWord = blackText.split(""); // 文本最大寬度和行高 int textMaxWidth = 200; int textLineHeight = 18; // 文本字符輸出起始坐標(biāo) int textWordX = 20; int textWordY = 350; // 通過計算控制單行文本長度 StringBuilder textLine = new StringBuilder(); for (String word : textWord){ graphics.drawString(word, textWordX, textWordY); if (graphics.getFontMetrics().stringWidth(textLine + word) <= textMaxWidth) { // 不需要換行,記錄單行內(nèi)容,移動X坐標(biāo) textLine.append(word); textWordX = textWordX + graphics.getFontMetrics().stringWidth(word); } else { // 需要換行,重置當(dāng)行文本內(nèi)容,移動X坐標(biāo)和Y坐標(biāo) textLine.setLength(0); textWordX = 20 ; textWordY = textWordY+textLineHeight; } } // 6、繪制短文本,右對齊 graphics.setFont(tailFont); graphics.setColor(Color.BLUE); String author = "制圖方:白天睡不著"; int authorWidth = canvasWidth-30-graphics.getFontMetrics().stringWidth(author); graphics.drawString(author, authorWidth, 180); String drawDate = "時間:2024年8月28日"; int drawDateWidth = canvasWidth-30-graphics.getFontMetrics().stringWidth(drawDate); graphics.drawString(drawDate, drawDateWidth, 200); // 7、添加水印圖片 BufferedImage watermarkImg = ImageIO.read(new File("src/main/watermark.png")); graphics.drawImage(watermarkImg, 350, 120,120,120, null); // 8、寫到圖片 ImageIO.write(backImg, "png", new File("src/main/draw-img.png")); backImg.flush(); watermarkImg.flush(); graphics.dispose(); } }
彩蛋:這里blackText文本是讓大模型隨機寫的,就沖這個輸出和味道,大家猜猜出自哪個國產(chǎn)大模型,(放水提示詞:國產(chǎn))。最后關(guān)于文件管理就不贅述了,哪個文件服務(wù)器方便,就隨地存著。
到此這篇關(guān)于Java中使用Graphics2D繪圖方法小結(jié)的文章就介紹到這了,更多相關(guān)Graphics2D繪圖方法總結(jié)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
從0到1學(xué)SpringCloud之SpringCloud?gateway網(wǎng)關(guān)路由配置示例詳解
Spring?Cloud?Gateway的目標(biāo)提供統(tǒng)一的路由方式且基于Filter?鏈的方式提供了網(wǎng)關(guān)基本的功能,?例如:安全、監(jiān)控、指標(biāo)和限流?,這篇文章主要介紹了從0到1學(xué)SpringCloud之SpringCloud?gateway網(wǎng)關(guān)路由配置示例詳解,需要的朋友可以參考下2023-04-04SSM如何實現(xiàn)在Controller中添加事務(wù)管理
這篇文章主要介紹了SSM如何實現(xiàn)在Controller中添加事務(wù)管理,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02PageHelper插件實現(xiàn)服務(wù)器端分頁功能
這篇文章主要為大家詳細(xì)介紹了PageHelper插件實現(xiàn)服務(wù)器端分頁功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-07-07Java如何使用poi導(dǎo)入導(dǎo)出excel工具類
這篇文章主要介紹了Java如何使用poi導(dǎo)入導(dǎo)出excel工具類問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-06-06