Changings Graphics drawString Color

时间:2014-05-01 04:20:23

标签: java graphics fonts colors

我正在尝试从ttf文件创建一个字体,然后使用Graphics2D将其渲染为某种颜色。

字体加载正常并以正确的字体呈现到屏幕上,但文本的颜色仍为黑色。如何将文本颜色更改为白色或其他颜色?任何帮助表示赞赏!

获取字体:

    public Font getFont(String name) {
        Font font = null;
        if (name == null) {
            font = new Font("sans", Font.PLAIN, 24);
        }

        try {
            String fname = "assets/fonts/" + name;
            File fontFile = new File(fname);
            font = Font.createFont(Font.TRUETYPE_FONT, fontFile);
            GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
            ge.registerFont(font);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return font;
    }

绘制文字:

    Font title = getFont("PressStart2P.ttf");
    g2.setFont(title.deriveFont(30.0f));
    g2.setColor(Color.WHITE);
    g2.drawString("A Game of Squares", 50, 80);

2 个答案:

答案 0 :(得分:4)

问题在于:

    g2.setColor(new Color(Color.WHITE));

Java中的Color类不接受另一个Color对象作为其构造函数的输入,因为Color.WHITE(以及无数其他预定义颜色)是对Color对象的引用,该行应为:

    g2.setColor(Color.WHITE);

来源:http://docs.oracle.com/javase/7/docs/api/java/awt/Color.html

答案 1 :(得分:1)

对我来说很好......

一些注意事项:

  1. 我已经将字体用作嵌入式资源,但它不应该有所区别(ps-测试(作为外部资源),一切都很好)
  2. 避免在paint方法中加载资源,这会减慢您的绘制过程。油漆方法应尽快返回
  3. enter image description here

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.EventQueue;
    import java.awt.Font;
    import java.awt.FontMetrics;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.GraphicsEnvironment;
    import java.io.File;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    
    public class FontTest {
    
        public static void main(String[] args) {
            new FontTest();
        }
    
        public FontTest() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    }
    
                    JFrame frame = new JFrame("Testing");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLayout(new BorderLayout());
                    frame.add(new TestPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        public class TestPane extends JPanel {
    
            private Font font;
    
            public TestPane() {
                font = getFont("PressStart2P.ttf");
                font = font.deriveFont(Font.PLAIN, 30);
                setBackground(Color.BLACK);
            }
    
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(1000, 200);
            }
    
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2d = (Graphics2D) g.create();
                String text = "All your base are belong to us";
                g2d.setFont(font);
                FontMetrics fm = g2d.getFontMetrics();
                int x = (getWidth() - fm.stringWidth(text)) / 2;
                int y = ((getHeight() - fm.getHeight()) / 2) + fm.getAscent();
                g2d.setColor(Color.WHITE);
                g2d.drawString(text, x, y);
                g2d.dispose();
            }
    
            public Font getFont(String name) {
                Font font = null;
                if (name == null) {
                    font = new Font("sans", Font.PLAIN, 24);
                }
    
                try {
                    String fname = "/assets/fonts/" + name;
                    font = Font.createFont(Font.TRUETYPE_FONT, getClass().getResourceAsStream(fname));
                    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
                    ge.registerFont(font);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return font;
            }
        }
    
    }