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
Post a Comment