-1

我想绘制不同的字体,特别是 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();
4

1 回答 1

1

我认为您没有向我们提供足够的信息来充分帮助您。

一些可运行的代码或您预期和实际结果的图片会很好。

文本渲染有很多陷阱,第一个是,你需要补偿字体的上升,以确保它在基线上绘制......

您可能想查看2D 图形教程中的使用文本 API进行复习...

在此处输入图像描述

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();

        }

    }

}
于 2013-01-30T00:12:49.483 回答