Java 图形2d不';我画不出正确的字体
我想画不同的字体,特别是Times New Roman(可在我使用的iMac上找到) 我设置了正确的字体并绘制了字符串,我使用的Java 图形2d不';我画不出正确的字体,java,graphics,fonts,awt,graphics2d,Java,Graphics,Fonts,Awt,Graphics2d,我想画不同的字体,特别是Times New Roman(可在我使用的iMac上找到) 我设置了正确的字体并绘制了字符串,我使用的FontMetrics-测量了正确的字体-但它没有绘制正确的字体!相反,我认为是Arial被画出来了 在下面,我使用了一个Graphics2D对象,但它也不适用于普通的图形对象 // FONTS Font fBank = new Font("Times New Roman", Font.PLAIN, 9); Font fPrice = new F
FontMetrics
-测量了正确的字体-但它没有绘制正确的字体!相反,我认为是Arial被画出来了
在下面,我使用了一个Graphics2D对象,但它也不适用于普通的图形对象
// FONTS
Font fBank = new Font("Times New Roman", Font.PLAIN, 9);
Font fPrice = new Font("Times New Roman", Font.PLAIN, 17);
Font fnormalText = new Font("Times New Roman", Font.PLAIN, 13);
Font fHeadlineText = new Font("Times New Roman", Font.PLAIN, 27);
Font fPayAndDiagnose = new Font("Times New Roman", Font.PLAIN, 12);
Font fHeadlineNumber = new Font("Times New Roman", Font.PLAIN, 17);
// FONTMETRIC
FontMetrics fMetric = _parent.getFontMetrics(fnormalText);
// LOGO
int imgPosX = (int) pageFormat.getImageableX() + 30;
int imgPosY = (int) pageFormat.getImageableY() + 30;
Image logo = new ImageIcon(getClass().getResource("/at/corgler/images/Print_Header_Plain.jpg")).getImage();
g.drawImage(logo, imgPosX, imgPosY, 184, 117, null);
// BILLDATE
String dateText = "XX, " + new SimpleDateFormat("dd. MMMM yyyy").format(_billDate);
int datePosY = imgPosY + 105;
int datePosX = (int) pageFormat.getImageableWidth() - fMetric.stringWidth(dateText);
g.setFont(fnormalText);
g.drawString(dateText, datePosX, datePosY);
// HEADLINE WITH NUMBER
String headlineText = "Honorarnote";
String numberText = "Nr. " + _payNumber + "/" + new SimpleDateFormat("yy").format(new Date());
fMetric = _parent.getFontMetrics(fHeadlineText);
int headlineWidth = fMetric.stringWidth(headlineText);
fMetric = _parent.getFontMetrics(fHeadlineNumber);
int numberWidth = fMetric.stringWidth(numberText);
int headlinePosY = datePosY + 65;
int headlineTextPosX = (int) ((pageFormat.getImageableWidth() / 2) - ((headlineWidth + numberWidth) / 2));
int headlineNumberPosX = headlineTextPosX + headlineWidth + 3;
g.setFont(fHeadlineText);
g.drawString(headlineText, headlineTextPosX, headlinePosY);
g.setFont(fHeadlineNumber);
g.drawString(numberText, headlineNumberPosX, headlinePosY);
g.drawLine(headlineTextPosX - 1, headlinePosY + 2, headlineTextPosX + headlineWidth, headlinePosY + 2);
g.setStroke(new BasicStroke(0.5f));
g.dispose();
我认为你没有向我们提供足够的信息来充分帮助你 一些可运行的代码或您的预期和实际结果的图片会很好 文本渲染有很多问题,首先,你需要补偿字体的上升,以确保它在基线上绘制 您可能想从2D图形教程中浏览一下,以获得复习
public class TestFontGraphics {
public static void main(String[] args) {
new TestFontGraphics();
}
public TestFontGraphics() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception ex) {
}
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int x = 10;
int y = 10;
y += drawFont("Arial", x, y, g);
y += drawFont("Times New Roman", x, y, g);
}
private int drawFont(String fontName, int x, int y, Graphics g) {
Font font = new Font(fontName, Font.PLAIN, 24);
g.setFont(font);
FontMetrics fm = g.getFontMetrics();
g.setColor(Color.RED);
g.drawLine(x, y, x + fm.stringWidth(fontName), y);
g.setColor(Color.GREEN);
g.drawLine(x, y + fm.getAscent(), x + fm.stringWidth(fontName), y + fm.getAscent());
g.setColor(Color.BLUE);
g.drawLine(x, y + (fm.getDescent() + fm.getAscent()), x + fm.stringWidth(fontName), y + (fm.getDescent() + fm.getAscent()));
g.setColor(Color.BLACK);
g.drawString(fontName, x, y + fm.getAscent());
return fm.getHeight();
}
}
}
1) 为了更快地获得更好的帮助,请发布一个。2) 发布一个小屏幕截图可能会有帮助。你怎么知道它不仅仅是测量Arial?因为我在fontMetrics的帮助下画了一条“下”线,在我的macbook pro上,字体画得很好,但在iMac上没有。1)添加@JasonSperske以通知他们新的评论。尤其是因为你似乎忽视了我。。2) 您评论中的信息应编辑到问题中。3) 检查
Times New Roman
是否使用类似seen的源代码注册为字体
(这也显示了字体的显示方式)。导入如图所示的Font
。@Christian'fuzi'Orgler很高兴看到您如何处理FontMetrics
并计算headlineextposx
&headlinePosY