heys all i'm having some problems making this code run without exceptions.
boolean text_centered;
String text;
public void CenterText() {
text_centered = true;
text = getText();
java.awt.Graphics graphics = getGraphics();
System.out.println(graphics.toString());
java.awt.FontMetrics metrics = graphics.getFontMetrics(getFont());
java.awt.geom.Rectangle2D rectangle = metrics.getStringBounds(text, graphics);
text_left = (int) ((getWidth() - rectangle.getWidth()) / 2);
text_top = (int) ((getHeight() - rectangle.getHeight()) / 2) + metrics.getAscent();
graphics.dispose();
setText("");
}
"public Graphics getGraphics(): Creates a graphics context for this component. This method will return null if this component is currently not displayable. "
how do we make a component displayable?
Edited by: Pacerier on May 23, 2010 3:09 PM