我想绘制不同的字体,特别是 Times New Roman(在我使用的 iMac 上可用)
我设置了正确的字体并绘制了字符串,FontMetrics
我使用的 - 测量了正确的字体 - 但它没有绘制正确的字体!相反,我认为被绘制的是 Arial。
下面我使用了 Graphics2D 对象,但它也不适用于普通的 Graphics 对象。
// 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();