Java使用Graphics2D實現(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)計字符串寬度 并與 預(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);知識補充
Java中使用Graphics2D實現(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);
// 計算每列的寬度
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);
// // 計算當(dāng)前位置
int cx = x - columnIndex * columnWidth;
int cy = y + rowIndex * fontHeight + rowIndex * verticalSpacing; // 加入垂直偏移量
// 計算當(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實現(xiàn)字符串文本自動換行的文章就介紹到這了,更多相關(guān)Java Graphics2D字符串換行內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Springboot關(guān)于自定義stater的yml無法提示問題解決方案
這篇文章主要介紹了Springboot關(guān)于自定義stater的yml無法提示問題及解決方案,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-06-06
詳解java中的PropertyChangeSupport與PropertyChangeListener
這篇文章主要介紹了詳解java中的PropertyChangeSupport與PropertyChangeListener的相關(guān)資料,需要的朋友可以參考下2017-09-09
java 在Jetty9中使用HttpSessionListener和Filter
這篇文章主要介紹了java 在Jetty9中使用HttpSessionListener和Filter的相關(guān)資料,需要的朋友可以參考下2017-06-06
Java中int類型轉(zhuǎn)換為String類型的多種方法及優(yōu)缺點總結(jié)
在Java編程中數(shù)據(jù)類型轉(zhuǎn)換是一項頻繁運用且至關(guān)重要的操作,它精細(xì)把控著數(shù)據(jù)在不同場景下的流轉(zhuǎn)與適配,這篇文章主要介紹了Java中int類型轉(zhuǎn)換為String類型的多種方法及優(yōu)缺點,需要的朋友可以參考下2025-12-12
Spring Boot 會員管理系統(tǒng)之處理文件上傳功能
Spring Boot會員管理系統(tǒng)的中,需要涉及到Spring框架,SpringMVC框架,Hibernate框架,thymeleaf模板引擎。這篇文章主要介紹了Spring Boot會員管理系統(tǒng)之處理文件上傳功能,需要的朋友可以參考下2018-03-03
Java讀取Oracle大字段數(shù)據(jù)(CLOB)的2種方法
這篇文章主要介紹了Java讀取Oracle大字段數(shù)據(jù)(CLOB)的2種方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-04-04

