Java - FontMetrics without Graphics

后端 未结 2 1605
醉酒成梦
醉酒成梦 2020-12-30 19:49

How to get FontMetrics without use Graphics ? I want to get FontMetrics in constructor, now I do this way:

BufferedImage bi = new BufferedImage(5, 5, Buffer         


        
相关标签:
2条回答
  • 2020-12-30 20:28

    Hmm... It is quite logical that you need graphics to get FontMetrics. Font height, width etc. can differ on various displays.

    If you have some Component, you can use it for getting FontMetrics:

    component.getFontMetrics(font);
    
    0 讨论(0)
  • 2020-12-30 20:38

    No you do not necessarily need to get/use the graphics object:

    Font font = new Font("Helvetica",Font.PLAIN,12);
    Canvas c = new Canvas();
    FontMetrics fm = c.getFontMetrics(font);
    

    If you now call c.getGraphics() it will return null. Instead canvas will also work in headless mode.

    0 讨论(0)
提交回复
热议问题