Java 图形无法更改字体
我正在尝试使用 Graphics 对象进行自定义绘画。 我想以自动换行样式绘制文本,因此我使用了在本页某处找到的一些示例代码。
我的问题是我无法以任何方式更改字体大小。 请帮助我,我在下面附上了可执行源代码。
谢谢克里斯
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.font.LineBreakMeasurer;
import java.awt.font.TextLayout;
import java.text.AttributedCharacterIterator;
import java.text.AttributedString;
import javax.swing.JFrame;
public class JavaFontProblem extends JFrame {
public JavaFontProblem() {
setSize(new Dimension(1024, 768));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
String longtext = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.";
g2d.setColor(Color.BLACK);
g2d.setFont(new Font("Verdana", Font.PLAIN, 250));
AttributedString atString = new AttributedString(longtext);
AttributedCharacterIterator iterator = atString.getIterator();
LineBreakMeasurer measurer = new LineBreakMeasurer(iterator, g2d
.getFontRenderContext());
measurer.setPosition(iterator.getBeginIndex());
int m_width = 500;
float x = 50, y = 50;
while (measurer.getPosition() < iterator.getEndIndex()) {
TextLayout layout = measurer.nextLayout(m_width);
y += layout.getAscent();
float dx = layout.isLeftToRight() ? 0 : m_width
- layout.getAdvance();
layout.draw(g2d, x + dx, y);
y += layout.getDescent() + layout.getLeading();
}
}
public static void main(String[] args) {
new JavaFontProblem().setVisible(true);
}
}
I am trying to do a custom painting using the Graphics object.
I want to paint the text in word-wrap style, so i used some example code i found somewhere on this page.
My problem is that i cannot change the font-size in any way.
Please help me, I attached the executable source code below.
Thanks
Chris
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.font.LineBreakMeasurer;
import java.awt.font.TextLayout;
import java.text.AttributedCharacterIterator;
import java.text.AttributedString;
import javax.swing.JFrame;
public class JavaFontProblem extends JFrame {
public JavaFontProblem() {
setSize(new Dimension(1024, 768));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
String longtext = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.";
g2d.setColor(Color.BLACK);
g2d.setFont(new Font("Verdana", Font.PLAIN, 250));
AttributedString atString = new AttributedString(longtext);
AttributedCharacterIterator iterator = atString.getIterator();
LineBreakMeasurer measurer = new LineBreakMeasurer(iterator, g2d
.getFontRenderContext());
measurer.setPosition(iterator.getBeginIndex());
int m_width = 500;
float x = 50, y = 50;
while (measurer.getPosition() < iterator.getEndIndex()) {
TextLayout layout = measurer.nextLayout(m_width);
y += layout.getAscent();
float dx = layout.isLeftToRight() ? 0 : m_width
- layout.getAdvance();
layout.draw(g2d, x + dx, y);
y += layout.getDescent() + layout.getLeading();
}
}
public static void main(String[] args) {
new JavaFontProblem().setVisible(true);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要发送长文本的长度...而不是 atString 长度
另外,您必须声明自己的 Font 对象。 它还消除了 setFont 的需要。
You need to send the length of the longtext...not the atString length
Also, you will have to declare your own Font object. It also negates the need to have your setFont.