string - How to center a drawString in Java? -


how can center text of drawstring in java? want it can centered along screen dynamically, whether change height , width of box or not. found this codebut don't know how use it. can explain?

horizontally...

string text = "..."; graphics2d g2d = (graphics2d)g.create(); fontmetrics fm = g2d.getfontmetrics(); int x = (getwidth() - fm.stringwidth(text)) / 2; 

vertically...

string text = "..."; graphics2d g2d = (graphics2d)g.create(); fontmetrics fm = g2d.getfontmetrics(); int y = ((getheight() - fm.getheight()) / 2) + fm.getascent(); 

also demonstrated here

also have @ 2d graphics , working text apis


Comments

Popular posts from this blog

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

html - Outlook 2010 Anchor (url/address/link) -

android - How to create dynamically Fragment pager adapter -