View Javadoc

1   /*
2   Wotonomy: OpenStep design patterns for pure Java applications.
3   Copyright (C) 2000 Intersect Software Corporation
4   
5   This library is free software; you can redistribute it and/or
6   modify it under the terms of the GNU Lesser General Public
7   License as published by the Free Software Foundation; either
8   version 2.1 of the License, or (at your option) any later version.
9   
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  Lesser General Public License for more details.
14  
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, see http://www.gnu.org
17  */
18  
19  package net.wotonomy.ui.swing.components;
20  
21  import java.awt.Dimension;
22  import java.awt.Graphics;
23  import java.awt.Image;
24  import java.awt.image.ImageObserver;
25  
26  import javax.swing.JPanel;
27  
28  /***
29  * A JPanel that renders an image, tiling as necessary to
30  * fill the panel.
31  * The preferred size of the panel is the size of the image
32  * and will change until the image is fully loaded, so using
33  * a media tracker is recommended.
34  *
35  * @author michael@mpowers.net
36  * @version $Revision: 904 $
37  */
38  public class ImagePanel extends JPanel implements ImageObserver
39  {    
40  	protected Image image;
41  	protected int imageWidth, imageHeight;
42  	
43      public ImagePanel()
44      {
45  		this( null );
46      }
47  	
48  	public ImagePanel( Image anImage )
49  	{
50  		image = anImage;	
51  		if ( anImage != null )
52  		{
53              prepareImage( image, this ); 
54  			// these may return -1
55  			imageWidth = image.getWidth( this ); 
56  			imageHeight = image.getHeight( this );
57  		}
58  		else
59  		{
60  			imageWidth = 0;
61  			imageHeight = 0;
62  		}
63  	}
64  	
65  	protected void paintComponent(Graphics g)
66  	{
67  		if ( ( image != null ) && ( imageWidth > 0 ) && ( imageHeight > 0 ) )
68  		{
69  			int width = getWidth();
70  			int height = getHeight();
71  			
72  			for ( int x = 0; x < width; x += imageWidth )
73  			{
74  				for ( int y = 0; y < height; y += imageHeight )
75  				{
76  					g.drawImage( image, x, y, 
77  						imageWidth, imageHeight, 
78  						getBackground(), this );		
79  				}
80  			}
81  		}
82  	}
83      
84  	public boolean imageUpdate(Image img,
85  	   int infoflags,
86  	   int x,
87  	   int y,
88  	   int width,
89  	   int height)
90  	{
91  		imageWidth = width;
92  		imageHeight = height;
93  		setPreferredSize( new Dimension( width, height ) );
94  		revalidate();	
95  		repaint();
96  	
97  		if ( ( infoflags & ImageObserver.ALLBITS ) == ImageObserver.ALLBITS )
98  		{
99  			return false;	
100 		}
101 		return true;
102 	}
103 	
104 }