home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-06-19 | 3.4 KB | 167 lines |
- package symantec.itools.awt;
-
- import java.awt.Panel;
- import java.net.URL;
- import java.awt.Image;
- import java.awt.MediaTracker;
- import java.awt.Graphics;
- import java.awt.Dimension;
-
- // 03/02/97 RKM Changed call to invalidate to repaint
-
- public class ImagePanel extends java.awt.Panel
- {
- public static final int IMAGE_TILED = 0;
- public static final int IMAGE_CENTERED = 1;
- public static final int IMAGE_SCALED_TO_FIT = 2;
-
- public ImagePanel()
- {
- imageURL = null;
- image = null;
- imageStyle = IMAGE_TILED;
- }
-
- // Properties
-
- /**
- * Paints this component using the given graphics context.
- * This is a standard Java AWT method which typically gets called
- * by the AWT to handle painting this component. It paints this component
- * using the given graphics context. The graphics context clipping region
- * is set to the bounding rectangle of this component and its <0,0>
- * coordinate is this component's top-left corner.
- *
- * @param g the graphics context used for painting
- * @see java.awt.Component#repaint
- * @see java.awt.Component#update
- */
- public void paint(Graphics g)
- {
- super.paint(g);
-
- if (image != null)
- {
- Dimension dim = size();
-
- int imageWidth = image.getWidth(this);
- int imageHeight = image.getHeight(this);
-
- switch(imageStyle)
- {
- default:
- case IMAGE_TILED:
- {
- //Calculate number of images that should be drawn horizontally
- int numHImages = dim.width / imageWidth;
-
- //Don't forget remainders
- if (dim.width % imageWidth != 0)
- numHImages++;
-
- //Calculate number of images that should be drawn vertically
- int numVImages = dim.height / imageHeight;
-
- //Don't forget remainders
- if (dim.height % imageHeight != 0)
- numVImages++;
-
- int h;
- int v = 0;
- for (int vCount = 0;vCount < numVImages;vCount++)
- {
- h = 0;
- for (int hCount = 0;hCount < numHImages;hCount++)
- {
- g.drawImage(image, h, v, imageWidth, imageHeight, this);
-
- //Increment to next column
- h += imageWidth;
- }
-
- //Increment to next row
- v += imageHeight;
- }
-
- break;
- }
-
- case IMAGE_CENTERED:
- {
- g.drawImage
- (image,
- (dim.width - imageWidth) / 2,
- (dim.height - imageHeight) / 2,
- imageWidth,
- imageHeight,
- this);
-
- break;
- }
-
- case IMAGE_SCALED_TO_FIT:
- {
- g.drawImage(image, 0, 0, dim.width, dim.height, this);
-
- break;
- }
- }//switch
- }
- }
-
- public void setImageURL(URL url)
- {
- imageURL = url;
- if (imageURL != null)
- {
- image = getToolkit().getImage(imageURL);
- if (image != null)
- {
- MediaTracker mt = new MediaTracker(this);
- try
- {
- mt.addImage(image, 0);
- mt.waitForAll();
- }
- catch (InterruptedException ie)
- {
- }
- }
- }
-
- repaint();
- }
-
- public URL getImageURL()
- {
- return imageURL;
- }
-
- public void setStyle(int newStyle)
- {
- if (newStyle != imageStyle)
- {
- imageStyle = newStyle;
- repaint();
- }
- }
-
- public int getStyle()
- {
- return imageStyle;
- }
-
- // Methods
-
- public Image getImage()
- {
- return image;
- }
-
- // Private members
-
- private Image image;
- private URL imageURL;
- private int imageStyle;
- }
-