Java Graphics2D.drawString 中的换行符问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4413132/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Problems with newline in Graphics2D.drawString
提问by pkinsky
g2
is an instance of the class Graphics2D
. I'd like to be able to draw multi-line text, but that requires a newline character. The following code renders in one line.
g2
是类的一个实例Graphics2D
。我希望能够绘制多行文本,但这需要一个换行符。以下代码在一行中呈现。
String newline = System.getProperty("line.separator");
g2.drawString("part1\r\n" + newline + "part2", x, y);
采纳答案by aioobe
The drawString
method does not handle new-lines.
该drawString
方法不处理换行符。
You'll have to split the string on new-line characters yourself and draw the lines one by one with a proper vertical offset:
您必须自己将字符串拆分为换行符,并使用适当的垂直偏移逐条绘制线条:
void drawString(Graphics g, String text, int x, int y) {
for (String line : text.split("\n"))
g.drawString(line, x, y += g.getFontMetrics().getHeight());
}
Here is a complete example to give you the idea:
这是一个完整的示例,可以让您了解这个想法:
import java.awt.*;
public class TestComponent extends JPanel {
private void drawString(Graphics g, String text, int x, int y) {
for (String line : text.split("\n"))
g.drawString(line, x, y += g.getFontMetrics().getHeight());
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
drawString(g, "hello\nworld", 20, 20);
g.setFont(g.getFont().deriveFont(20f));
drawString(g, "part1\npart2", 120, 120);
}
public static void main(String s[]) {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new TestComponent());
f.setSize(220, 220);
f.setVisible(true);
}
}
which gives the following result:
这给出了以下结果:
回答by Ivan De Sousa Paz
I just made a method to draw long text spliting automaticaly by giving the line width.
我刚刚做了一个方法,通过给出线宽来自动绘制长文本分割。
public static void drawStringMultiLine(Graphics2D g, String text, int lineWidth, int x, int y) {
FontMetrics m = g.getFontMetrics();
if(m.stringWidth(text) < lineWidth) {
g.drawString(text, x, y);
} else {
String[] words = text.split(" ");
String currentLine = words[0];
for(int i = 1; i < words.length; i++) {
if(m.stringWidth(currentLine+words[i]) < lineWidth) {
currentLine += " "+words[i];
} else {
g.drawString(currentLine, x, y);
y += m.getHeight();
currentLine = words[i];
}
}
if(currentLine.trim().length() > 0) {
g.drawString(currentLine, x, y);
}
}
}
回答by Don Kirkby
Here's a snippet I used to draw text in a JPanel
with tab expansion and multiple lines:
这是我用来在JPanel
标签扩展和多行中绘制文本的片段:
import javax.swing.*;
import java.awt.*;
import java.awt.geom.Rectangle2D;
public class Scratch {
public static void main(String argv[]) {
JFrame frame = new JFrame("FrameDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel() {
@Override
public void paint(Graphics graphics) {
graphics.drawRect(100, 100, 1, 1);
String message =
"abc\tdef\n" +
"abcx\tdef\tghi\n" +
"xxxxxxxxdef\n" +
"xxxxxxxxxxxxxxxxghi\n";
int x = 100;
int y = 100;
FontMetrics fontMetrics = graphics.getFontMetrics();
Rectangle2D tabBounds = fontMetrics.getStringBounds(
"xxxxxxxx",
graphics);
int tabWidth = (int)tabBounds.getWidth();
String[] lines = message.split("\n");
for (String line : lines) {
int xColumn = x;
String[] columns = line.split("\t");
for (String column : columns) {
if (xColumn != x) {
// Align to tab stop.
xColumn += tabWidth - (xColumn-x) % tabWidth;
}
Rectangle2D columnBounds = fontMetrics.getStringBounds(
column,
graphics);
graphics.drawString(
column,
xColumn,
y + fontMetrics.getAscent());
xColumn += columnBounds.getWidth();
}
y += fontMetrics.getHeight();
}
}
@Override
public Dimension getPreferredSize() {
return new Dimension(400, 200);
}
};
frame.getContentPane().add(panel, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true); }
}
It really seemed like Utilities.drawTabbedText()
was promising, but I couldn't figure out what it needed as input.
看起来真的Utilities.drawTabbedText()
很有希望,但我无法弄清楚它需要什么作为输入。