gpt4 book ai didi

java - 在图像上附加文本时指定行距

转载 作者:行者123 更新时间:2023-12-02 00:06:05 25 4
gpt4 key购买 nike

我是Appending Text on images 。在图像上附加文本时如何指定两行之间的行间距?

编辑:-我想控制文本的行高。

final BufferedImage image = ImageIO.read(new File(Background));
Graphics g = image.getGraphics();
java.awt.Font a=new java.awt.Font("AndaleWTJ", java.awt.Font.PLAIN, 26);
g.setFont(a);
g.setColor(Color.RED);
FontMetrics fm = g.getFontMetrics();
drawString(g,Title,15, 84,402,199,171,1);
g.dispose();

public static void drawString(Graphics g, String s, int x, int y, int width,int red_val,int green_val, int blue_val)
{ FontMetrics fm = g.getFontMetrics();
java.awt.Color c= new java.awt.Color(red_val, green_val, blue_val);
g.setColor(c);
int lineHeight = fm.getHeight();
int curX = x;
int curY = y;
String[] words = s.split(" ");
for (String word : words)
{
// Find out thw width of the word.
int wordWidth = fm.stringWidth(word + " ");

// If text exceeds the width, then move to next line.
if (curX + wordWidth >= x + width)
{
curY += lineHeight;
curX = x;
}
g.drawString(word, curX, curY);
// Move over to the right for next word.
curX += wordWidth;
}
}

最佳答案

现在我传递 lineHeight 的值而不是 int lineHeight = fm.getHeight();

 public static void drawString(Graphics g, String s, int x, int y, int width,int red_val,int green_val, int blue_val, int lineHeight )
{ FontMetrics fm = g.getFontMetrics();
java.awt.Color c= new java.awt.Color(red_val, green_val, blue_val);
g.setColor(c);
int curX = x;
int curY = y;
String[] words = s.split(" ");
for (String word : words)
{
// Find out thw width of the word.
int wordWidth = fm.stringWidth(word + " ");

// If text exceeds the width, then move to next line.
if (curX + wordWidth >= x + width)
{
curY += lineHeight;
curX = x;
}
g.drawString(word, curX, curY);
// Move over to the right for next word.
curX += wordWidth;
}
}

关于java - 在图像上附加文本时指定行距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13861075/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com