我希望使用 Graphics2D 对象将字符串逐个字符绘制到屏幕上,让它们淡入。
我最初将字符串拆分为字符,并绘制了每个单独的字符,但遇到了每个字母之间适当间距的问题(因为每个字母的像素数量不同。)有没有一种简单的方法可以实现此目的?
g.drawString(currentLine(j), 250, 770+j*20);
对比
for(int i = 0; i < currentLine(j).length(); i++) {
String chr = new StringBuilder().append("").append(currentLine(j).charAt(i)).toString();
g.drawString(chr, 250+i*10, 770+j*20);
}
最佳答案
使用“GlyphVector”获取文本的宽度和高度。
String testString = "A";
GlyphVector gv = g.getFont().createGlyphVector(g.getFontRenderContext(), testString);
Rectangle textBound = gv.getOutline().getBounds();
double textHeight = textBound.getHeight();
double textWidth = textBound.getWidth();
关于java - 有没有一种简单的方法可以使用 java Graphics 2D 对象淡入字符串字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55934900/