Java使用Graphics2D实现字符串文本自动换行
作者:凌晨的集市人很少
这篇文章主要为大家详细介绍了Java如何使用Graphics2D实现字符串文本自动换行,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下
效果

代码
/**
* @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++) {
// 统计字符串宽度 并与 预设好的宽度 作比较
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));
}
}
}调用
// 添加第二行文字
Font fontTwo = new Font("MIMO天线及其空口测试技术", Font.BOLD, 140 );
graphics.setFont(fontTwo);
graphics.setColor(Color.WHITE);
String textTwo = "MIMO天线及其空口测试技术";
int twoX = 50; // 第二行文字起始x坐标
int twoY = 480; // 第二行文字起始y坐标
drawWordAndLineFeed(graphics, fontTwo, textTwo, twoX, twoY, 1200);知识补充
Java中使用Graphics2D实现字符串- 竖直并居中排序显示算法
效果:

代码:
public static void drawMyString(Graphics textGraphics, String text) {
// 每列显示的汉字数量
int columnSize = 7;
// 文字之间的垂直间距
int verticalSpacing = 75;
// 获取字体渲染上下文
FontMetrics fm = textGraphics.getFontMetrics();
// 获取字体的高度
int fontHeight = fm.getHeight();
System.out.println(fontHeight);
// 计算每列的宽度
int columnWidth = fontHeight + verticalSpacing;
// 设置初始位置
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); // 总列数
int totalRows = Math.min(columnSize, text.length()); // 总行数
int remainingChars = text.length() % columnSize; // 最后一列剩余字符数
for (int columnIndex = 0; columnIndex < totalColumns; columnIndex++) {
for (int rowIndex = 0; rowIndex < totalRows; rowIndex++) {
if (charCount >= text.length()) break;
char ch = text.charAt(charCount);
// // 计算当前位置
int cx = x - columnIndex * columnWidth;
int cy = y + rowIndex * fontHeight + rowIndex * verticalSpacing; // 加入垂直偏移量
// 计算当前位置
// 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++;
}
}
}调用:
// TODO 讲座名称
String lectureName = "时空相分离调控的职务细胞信号转导";
drawMyString(graphics, lectureName);到此这篇关于Java使用Graphics2D实现字符串文本自动换行的文章就介绍到这了,更多相关Java Graphics2D字符串换行内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
