/* * Copyright (c) 1996 Lysator Academic Computer Society, All Rights Reserved. */ package SE.liu.lysator.gfx; import java.awt.*; import java.awt.image.*; /** * A class that produces a component displaying an image * * @version 1.0 24 Jan 1995 * @author Marcus Comstedt */ public class Icon extends Canvas implements ImageObserver { /** * The topleft layout variable */ public static final int TOPLEFT = 0; /** * The center layout variable */ public static final int CENTER = 1; /** * The scale layout variable */ public static final int SCALE = 2; /** * The tile layout variable */ public static final int TILE = 4; /** * The image for this icon. */ Image image; /** * The width of this icons image. */ int width; /** * The height of this icons image. */ int height; /** * The image layout of this icon. */ int layout; /** * Constructs an Icon with no image. */ public Icon() { this(null); } /** * Constructs an Icon with an image, using CENTER layout. * @param image the icon image */ public Icon(Image image) { this.image = image; layout = CENTER; if(image == null) { width = 0; height = 0; } else { width = image.getWidth(this); height = image.getHeight(this); } } /** * Sets the image layout policy for the icon. (Default = CENTER) * @param layout the layout value */ public void setLayout(int layout) { this.layout = layout; } /** * Paints the icon. * @param g the specified Graphics window * @see #update */ public void paint(Graphics g) { if(image != null && width>0 && height>0) { Dimension dim = size(); if((layout&SCALE)!=0) g.drawImage(image, 0, 0, dim.width, dim.height, this); else { int x=0; int y=0; if((layout&CENTER)!=0) { x = (dim.width-width)>>1; y = (dim.height-height)>>1; } if((layout&TILE)!=0) { if(x>0) x-=((x+width-1)/width)*width; if(y>0) y-=((y+height-1)/height)*height; int x0=x; for(x0=x; y 0) { resize(this.width, this.height); repaint = true; } } if((infoflags&ImageObserver.HEIGHT) != 0) { this.height = height; if(this.width > 0) { resize(this.width, this.height); repaint = true; } } if((infoflags&ImageObserver.ALLBITS) != 0) { repaint=true; ret=false; } if((infoflags&ImageObserver.SOMEBITS) != 0) repaint=true; if(repaint) repaint(); return ret; } }