/* Wotonomy: OpenStep design patterns for pure Java applications. Copyright (C) 2000 Intersect Software Corporation This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, see http://www.gnu.org */ package net.wotonomy.ui.swing.components; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Image; import java.awt.image.ImageObserver; import javax.swing.JPanel; /** * A JPanel that renders an image, tiling as necessary to fill the panel. The * preferred size of the panel is the size of the image and will change until * the image is fully loaded, so using a media tracker is recommended. * * @author michael@mpowers.net * @version $Revision: 904 $ */ public class ImagePanel extends JPanel implements ImageObserver { protected Image image; protected int imageWidth, imageHeight; public ImagePanel() { this(null); } public ImagePanel(Image anImage) { image = anImage; if (anImage != null) { prepareImage(image, this); // these may return -1 imageWidth = image.getWidth(this); imageHeight = image.getHeight(this); } else { imageWidth = 0; imageHeight = 0; } } protected void paintComponent(Graphics g) { if ((image != null) && (imageWidth > 0) && (imageHeight > 0)) { int width = getWidth(); int height = getHeight(); for (int x = 0; x < width; x += imageWidth) { for (int y = 0; y < height; y += imageHeight) { g.drawImage(image, x, y, imageWidth, imageHeight, getBackground(), this); } } } } public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) { imageWidth = width; imageHeight = height; setPreferredSize(new Dimension(width, height)); revalidate(); repaint(); if ((infoflags & ImageObserver.ALLBITS) == ImageObserver.ALLBITS) { return false; } return true; } }