[ Back | Previous | Next ]

Centering a graphics String

Package:
java.awt.*
Product:
JDK
Release:
1.0.2
Related Links:
Color
Cursor
FileDialog
Font
FontMetrics
Frame
general
Image
LayoutManager
Menu
ScrollPane
Comment:
// Draw the loading message
  Font        font = new Font("Helvetica", Font.PLAIN, 12);
  FontMetrics fm = g.getFontMetrics(font);
  String      str = new String("Loading images...");
  g.setFont(font);
  g.drawString(str, (getSize().width - fm.stringWidth(str)) / 2,
  ((getSize().height - fm.getHeight()) / 2) + fm.getAscent());
1