Graphics2D没有绘制正确的字体

时间:2013-01-29 23:49:38

标签: java graphics fonts awt graphics2d

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

1 个答案:

答案 0 :(得分:1)

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

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

文本渲染有很多陷阱,首先,你需要补偿字体的上升,以确保它在基线上绘画......

您可能希望从2D图形教程中查看Working with Text APIs以了解更新...

enter image description here

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

        }

    }

}