BufferedImage.java

/**
 *  Apple Worldwide Developer Technical Support
 *
 *  Sample demonstrating offscreen drawing in Java.
 *
 *  by Levi Brown, Apple Developer Technical Support
 *
 *  File:   BufferedImage.java
 *
 *  Copyright ©1999 Apple Computer, Inc.
 *  All rights reserved.
 *
 *  4/99    v. 1.0  Shipped as 'Magic Oracle AppleScript for Java' sample.
 *
 *  You may incorporate this sample code into your applications without
 *  restriction, though the sample code has been provided "AS IS" and the
 *  responsibility for its operation is 100% yours.  However, what you are
 *  not permitted to do is to redistribute the source as "Apple Sample
 *  Code" after having made changes. If you're going to re-distribute the
 *  source, we require that you make it clear in the source that the code
 *  was descended from Apple Sample Code, but that you've made changes.
**/
 
import java.awt.*;
 
/**
 * @author Levi Brown
 * @author Apple Worldwide Developer Technical Support
 * @author Apple Computer Inc.
 */
public class BufferedImage extends BufferedDrawer
{
    public BufferedImage()
    {
        image = null;
        imageLoc = new Point(0, 0);
        imageSize = new Dimension(0, 0);
    }
    
    public void setImage(Image image)
    {
        if (image != null)
        {
            this.image = image;
            imageSize.width = image.getWidth(this);
            imageSize.height = image.getHeight(this);
        }
    }
 
    public Image getImage()
    {
        return image;
    }
    
    public void setImageLocation(Point loc)
    {
        if (!imageLoc.equals(loc))
        {
            imageLoc = loc;
            draw();
        }
    }
    
    public Point getImageLocation()
    {
        return imageLoc;
    }
 
    public Dimension getImageSize()
    {
        return imageSize;
    }
    
    /** 
     * Returns the preferred size of this component.
     * @see #getMinimumSize
     * @see LayoutManager
     */
    public Dimension getPreferredSize()
    {
        if (image == null)
        {
            return super.getPreferredSize();
        }
        else
        {
            return getImageSize();
        }
    }
 
    /**
     * Handles drawing the desired content into the offscreen buffer.
     * Override this to do your own drawing (be sure to call super.draw()).
     * @see #paint
     */
    protected void draw()
    {
        super.draw();
        if (image != null)
        {
            Dimension s = getSize();
            workingGraphics.setColor(getBackground());
            workingGraphics.fillRect(0, 0, s.width, s.height);
            workingGraphics.drawImage(image, imageLoc.x, imageLoc.y, this);
        }
    }
    
    protected Image image;
    protected Point imageLoc;
    protected Dimension imageSize;
 
    /**
     * Notifies the Component that it has been added to a container
     * and if a peer is required, it should be created.
     * This method should be called by Container.add, and not by user
     * code directly.
     * @see #removeNotify
     */
    public void addNotify()
    {
        super.addNotify();
        if (image != null)
        {
            Dimension size = getSize();
            setImageLocation(new Point((size.width - imageSize.width) >> 1, (size.height - imageSize.height) >> 1));
        }
    }
}