问题
I am making a program in which I need to set the Font inside a FontMetrics object. I have already created the fontmetrics before, but now I don't have access to the Graphics object anymore. Yet, I want to change the font inside the FontMetrics, that is, create a new FontMetrics, with a new Font, but using the same Graphic context.
I could make the Graphics avaliable, but it would take a lot of time and it wouldn't make sense at all to do so. Would anyone know an easier way to it? It seens so obvius it should be a way, maybe I am just missing something really stupid (I looked at the documentation and found nothing).
回答1:
Probably the easiest way would be to use JComponent.getFontMetrics(Font). This method respects the JComponent
's current FontRenderContext
(Note:Component.getFontMetrics
will not respect the current FontRenderContext
).
来源:https://stackoverflow.com/questions/10674035/fontmetrics-setfont