Java使用Graphics2D實(shí)現(xiàn)字符串文本自動換行
效果
代碼
/** * @return void * @Author xia * @Description //TODO 寫字換行算法 * @Date 18:08 2021/4/1 * @Param [] **/ private static void drawWordAndLineFeed(Graphics2D g2d, Font font, String words, int wordsX, int wordsY, int wordsWidth) { FontDesignMetrics metrics = FontDesignMetrics.getMetrics(font); // 獲取字符的最高的高度 int height = metrics.getHeight(); int width = 0; int count = 0; int total = words.length(); String subWords = words; int b = 0; for (int i = 0; i < total; i++) { // 統(tǒng)計(jì)字符串寬度 并與 預(yù)設(shè)好的寬度 作比較 if (width <= wordsWidth) { width += metrics.charWidth(words.charAt(i)); // 獲取每個字符的寬度 count++; } else { // 畫 除了最后一行的前幾行 String substring = subWords.substring(0, count); g2d.drawString(substring, wordsX, wordsY + (b * height)); subWords = subWords.substring(count); b++; width = 0; count = 0; } // 畫 最后一行字符串 if (i == total - 1) { g2d.drawString(subWords, wordsX, wordsY + (b * height)); } } }
調(diào)用
// 添加第二行文字 Font fontTwo = new Font("MIMO天線及其空口測試技術(shù)", Font.BOLD, 140 ); graphics.setFont(fontTwo); graphics.setColor(Color.WHITE); String textTwo = "MIMO天線及其空口測試技術(shù)"; int twoX = 50; // 第二行文字起始x坐標(biāo) int twoY = 480; // 第二行文字起始y坐標(biāo) drawWordAndLineFeed(graphics, fontTwo, textTwo, twoX, twoY, 1200);
知識補(bǔ)充
Java中使用Graphics2D實(shí)現(xiàn)字符串- 豎直并居中排序顯示算法
效果:
代碼:
public static void drawMyString(Graphics textGraphics, String text) { // 每列顯示的漢字?jǐn)?shù)量 int columnSize = 7; // 文字之間的垂直間距 int verticalSpacing = 75; // 獲取字體渲染上下文 FontMetrics fm = textGraphics.getFontMetrics(); // 獲取字體的高度 int fontHeight = fm.getHeight(); System.out.println(fontHeight); // 計(jì)算每列的寬度 int columnWidth = fontHeight + verticalSpacing; // 設(shè)置初始位置 int x = 260; int y = 450; Font fontFour = new Font(" Source Han Sans CN", Font.BOLD, 100); textGraphics.setFont(fontFour); Color color = new Color(0, 88, 38); textGraphics.setColor(color); // // 繪制文字 int charCount = 0; int totalColumns = (int)Math.ceil((double)text.length() / columnSize); // 總列數(shù) int totalRows = Math.min(columnSize, text.length()); // 總行數(shù) int remainingChars = text.length() % columnSize; // 最后一列剩余字符數(shù) for (int columnIndex = 0; columnIndex < totalColumns; columnIndex++) { for (int rowIndex = 0; rowIndex < totalRows; rowIndex++) { if (charCount >= text.length()) break; char ch = text.charAt(charCount); // // 計(jì)算當(dāng)前位置 int cx = x - columnIndex * columnWidth; int cy = y + rowIndex * fontHeight + rowIndex * verticalSpacing; // 加入垂直偏移量 // 計(jì)算當(dāng)前位置 // int cx = x - columnIndex * columnWidth; // int cy = y + rowIndex * fontHeight + rowIndex * verticalSpacing + columnIndex ; // 如果是最后一列并且不滿 7 個字符,則需要將剩余字符居中 if (columnIndex == totalColumns - 1 && remainingChars > 0) { int extraVerticalSpace = (columnSize - remainingChars) * (fontHeight + verticalSpacing) / 2; cy += extraVerticalSpace; } // 繪制文字 textGraphics.drawString(String.valueOf(ch), cx, cy); charCount++; } } }
調(diào)用:
// TODO 講座名稱 String lectureName = "時空相分離調(diào)控的職務(wù)細(xì)胞信號轉(zhuǎn)導(dǎo)"; drawMyString(graphics, lectureName);
到此這篇關(guān)于Java使用Graphics2D實(shí)現(xiàn)字符串文本自動換行的文章就介紹到這了,更多相關(guān)Java Graphics2D字符串換行內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring boot實(shí)現(xiàn)數(shù)據(jù)庫讀寫分離的方法
本篇文章主要介紹了Spring boot實(shí)現(xiàn)數(shù)據(jù)庫讀寫分離的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-01-01Java中do-while循環(huán)的使用方法及注意事項(xiàng)詳解
這篇文章主要介紹了Java中do-while循環(huán)的使用方法及注意事項(xiàng)的相關(guān)資料,在Java編程中,do-while循環(huán)是一種基本的循環(huán)控制結(jié)構(gòu),它至少執(zhí)行一次循環(huán)體,然后根據(jù)條件判斷是否繼續(xù),文中將用法介紹的非常詳細(xì),需要的朋友可以參考下2024-10-10Java遞歸實(shí)現(xiàn)評論多級回復(fù)功能
這篇文章主要介紹了Java遞歸實(shí)現(xiàn)評論多級回復(fù)功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-06-06java Executors工具類的相關(guān)方法使用創(chuàng)建
這篇文章主要為大家介紹了java Executors工具類的相關(guān)方法使用創(chuàng)建,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11解析XML文件時的嵌套異常SAXParseException問題
這篇文章主要介紹了解析XML文件時的嵌套異常SAXParseException問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-04-04