public Dimension stringSize(FontMetrics f){
  Dimension d=new Dimension();
  int old=0;
  String s;
  int noa=0;
  while ((s=getLine(noa)) != null) {
    noa++;
    old=f.stringWidth(s);
    if (old > d.width) {
      d.width=old;
    }
  }
  d.height=noa * f.getHeight();
  return d;
}

public Dimension stringSize ( FontMetrics f ) { Dimension d = new Dimension ( ) ; int old = 0 ; String s ; int noa = 0 ; while ( ( s = getLine ( noa ) ) != null ) { noa ++ ; old = f . stringWidth ( s ) ; if ( old > d . width ) { d . width = old ; } } d . height = noa * f . getHeight ( ) ; return d ; }
this will return the width and height of the rectangle that the text will fit into .
