001/*
002$Source: v:/cvsroot/open/projects/WebARTS/ca/bc/webarts/widgets/ImageCanvas.java,v $
003$Name:  $
004
005$Revision: 567 $
006$Date: 2012-11-03 20:36:02 -0700 (Sat, 03 Nov 2012) $
007$Locker:  $
008*/
009
010/* This program is free software; you can redistribute it and/or modify
011 * it under the terms of the GNU General Public License as published by
012 * the Free Software Foundation; either version 2 of the License, or
013 * (at your option) any later version.
014 *
015 * This program is distributed in the hope that it will be useful,
016 * but WITHOUT ANY WARRANTY; without even the implied warranty of
017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
018 * GNU General Public License for more details.
019 *
020 * You should have received a copy of the GNU General Public License
021 * along with this program; if not, write to the Free Software
022 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
023 */
024package ca.bc.webarts.widgets;
025
026import java.awt.Canvas;
027import java.awt.Dimension;
028import java.awt.Graphics;
029import java.awt.Image;
030import java.awt.MediaTracker;
031
032/**
033 *  A nice canvas that holds an Image in its center.
034 *
035 * @author     tgutwin
036 * @created    October 27, 2001
037 */
038public class ImageCanvas extends Canvas
039{
040  /**
041   *  Description of the Field
042   *
043   * @since
044   */
045  private Image          image;
046  /**
047   *  Description of the Field
048   *
049   * @since
050   */
051  private int            imageHeight = 0;
052  /**
053   *  Description of the Field
054   *
055   * @since
056   */
057  private int            imageWidth = 0;
058
059
060  /**
061   *  Constructor for the ImageCanvas object
062   *
063   * @param  image  The Image that will get displayed
064   * @since
065   */
066  public ImageCanvas(Image image)
067  {
068    MediaTracker mt = new MediaTracker(this);
069    mt.addImage(image, 0);
070
071    try
072    {
073      mt.waitForID(0);
074    }
075    catch (Exception e)
076    {
077      e.printStackTrace();
078    }
079
080    this.image = image;
081    imageHeight = image.getHeight(this);
082    imageWidth = image.getWidth(this);
083  }
084
085
086  /**
087   *  Gets the PreferredSize attribute of the ImageCanvas object
088   *
089   * @return    The PreferredSize value
090   * @since
091   */
092  public Dimension getPreferredSize()
093  {
094    return new Dimension(imageWidth, imageHeight);
095  }
096
097
098  /**
099   *  Description of the Method
100   *
101   * @param  g  Description of Parameter
102   * @since
103   */
104  public void paint(Graphics g)
105  {
106    g.drawImage(image,
107        (getWidth() - imageWidth) / 2,
108        (getHeight() - imageHeight) / 2,
109        this);
110  }
111
112
113  /**
114   *  Description of the Method
115   *
116   * @param  g  Description of Parameter
117   * @since
118   */
119  public void update(Graphics g)
120  {
121    paint(g);
122  }
123}
124