Java Reference
In-Depth Information
add ( new
new Label ( "Domain:" , Label . CENTER ));
add ( domainTF = new
new TextField ( 10 ));
im = getToolkit (). getImage ( DEFAULT_IMAGE_NAME );
}
/** paint() - just tile the background. */
@Override
public
public void
void paintComponent ( Graphics g ) {
iif ( im == null
null )
return
return ;
int
int iw = im . getWidth ( this
this ), ih = im . getHeight ( this
this );
iif ( iw < 0 || ih < 0 )
// image not ready
return
return ;
// live to try again later.
int
int w = getSize (). width , h = getSize (). height ;
for
for ( int
int i = 0 ; i <= w ; i += iw ) {
for
for ( int
int j = 0 ; j <= h ; j += ih ) {
Debug . println ( "draw" , "drawImage(im," + i + "," + j + ")" );
g . drawImage ( im , i , j , this
this );
}
}
}
public
public static
void main ( String [] av ) {
JFrame f = new
static void
new JFrame ( "TiledImageComponent Demo" );
f . getContentPane (). add ( new
new TiledImageComponent ());
f . setSize ( 200 , 200 );
f . setVisible ( true
true );
f . setDefaultCloseOperation ( JFrame . EXIT_ON_CLOSE );
}
}
In the paint() method, we must check that the image is not null and has a nonnegative
width and height—we are more careful than we were in the previous, somewhat cavalier, ex-
ample. The image is null only if something went very wrong in the constructor, but it can
have a negative size. How? In certain creation myths, time ran backward before the begin-
ning of time; therefore, before an image is fully created, its size is backward (i.e., it has a
width and height of -1). The getImage() method doesn't actually get the image, you see. It
creates the Image object, true, but it doesn't necessarily load all the bits: it starts a back-
ground thread to do the reading and returns. This dates from the days when the Web was
slower and took a long time to fully load an image. In particular, with some image file
formats (some kinds of TIFF files, perhaps), you don't know the actual image size until
Search WWH ::




Custom Search