Getting string size in java (without having a Graphics object available)

前端 未结 6 608
南方客
南方客 2021-01-11 12:37

I\'m trying to write application which need to draw many strings using Graphics2D class in Java. I need to get sizes of each String object (to calculate exact p

6条回答
  •  长发绾君心
    2021-01-11 13:22

    for historical sake,here's how I think he did it originally (jruby java pseucodoe)

    font = UIManager.getFont("Label.font")
    frc = java.awt.font.FontRenderContext.new(font.transform, true, true)
    textLayout = java.awt.font.TextLayout.new(text, font, frc)
    textLayout.bounds.width
    

提交回复
热议问题