Question

Is there a way to align text from g.drawString() to the bottom center of a java applet?

I am also hoping for a way that is fluid between full-screen and small screen.

Was it helpful?

Solution

Yes, start by taking a look at Measuring Text

String text = "Happy at the bottom";
FontMetrics fm = g.getFontMetrics();
int x = (getWidth() - fm.stringWidth(text)) / 2;
int y = (getHeight() - fm.getHeight()) + fm.getAscent();
g.drawString(text, x, y);

The next question that needs to asked, is could what you want be achieved by using a JLabel and a BorderLayout (or other compound layout)....?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top