home *** CD-ROM | disk | FTP | other *** search
/ CD Ware Multimedia 1999 February / CDW0299.iso / Demos / Cafe / Source.bin / ImagePanel.java < prev    next >
Encoding:
Java Source  |  1998-03-18  |  9.2 KB  |  347 lines

  1. package symantec.itools.awt;
  2.  
  3. import java.awt.Panel;
  4. import java.net.URL;
  5. import java.awt.Image;
  6. import java.awt.MediaTracker;
  7. import java.awt.Graphics;
  8. import java.awt.Dimension;
  9. import java.beans.PropertyVetoException;
  10. import java.beans.PropertyChangeListener;
  11. import java.beans.VetoableChangeListener;
  12. import java.beans.PropertyChangeEvent;
  13.  
  14. //     03/02/97    RKM    Changed call to invalidate to repaint
  15. //    05/31/97    RKM    Updated to support Java 1.1
  16. //                    Made properties bound & constrained
  17. //  07/29/97    CAR marked fields transient as needed
  18. //                  implemented readObject
  19. //    05/31/97    RKM    Fixed bug in setImageURL, was not properly comparing objects
  20. //  08/19/97    CAR constructor now calls super.setLayout(null)
  21. //  08/25/97    LAB Changed all property name strings in bound and constrained messages to
  22. //                    conform to the BeanSpec naming guidelines.  Added IMAGE_NORMAL image style
  23. //                    (Addresses Mac Bug #7255).
  24. //  08/27/97    CAR updated setImageURL to "erase" image if URL is null
  25. //                  updated paint to draw images in itself before calling super.paint
  26. //  09/11/97    LAB Made imageStyle protected (Addresses Mac Bug #7692).
  27.  
  28. /**
  29.  * The ImagePanel component is similar to a regular panel except that it
  30.  * displays an image within the panel.
  31.  * The image to use is specified with a URL.
  32.  */
  33. public class ImagePanel extends java.awt.Panel
  34. {
  35.     /**
  36.      * A constant indicating the image is to be tiled in this panel.
  37.      */
  38.     public static final int IMAGE_TILED = 0;
  39.     /**
  40.      * A constant indicating the image is to be centered in this panel.
  41.      */
  42.     public static final int IMAGE_CENTERED = 1;
  43.     /**
  44.      * A constant indicating the image is to be scaled to fit this panel.
  45.      */
  46.     public static final int IMAGE_SCALED_TO_FIT = 2;
  47.     /**
  48.      * A constant indicating the image is to be drawn from the upper left corner of the panel.
  49.      */
  50.     public static final int IMAGE_NORMAL = 3;
  51.  
  52.     /**
  53.      * Constructs a default ImagePanel. By default the image will be tiled.
  54.      */
  55.     public ImagePanel()
  56.     {
  57.         super.setLayout(null);
  58.         imageURL = null;
  59.         image = null;
  60.         imageStyle = IMAGE_TILED;
  61.  
  62.         vetos = new symantec.itools.beans.VetoableChangeSupport(this);
  63.         changes = new symantec.itools.beans.PropertyChangeSupport(this);
  64.     }
  65.  
  66.     // Properties
  67.  
  68.     /**
  69.      * Paints this component using the given graphics context.
  70.      * This is a standard Java AWT method which typically gets called
  71.      * by the AWT to handle painting this component. It paints this component
  72.      * using the given graphics context. The graphics context clipping region
  73.      * is set to the bounding rectangle of this component and its [0,0]
  74.      * coordinate is this component's top-left corner.
  75.      *
  76.      * @param g the graphics context used for painting
  77.      * @see java.awt.Component#repaint
  78.      * @see java.awt.Component#update
  79.      */
  80.     public void paint(Graphics g)
  81.     {
  82.         Dimension dim = size();
  83.         if (image != null)
  84.         {
  85.  
  86.             int imageWidth = image.getWidth(this);
  87.             int imageHeight = image.getHeight(this);
  88.  
  89.             switch(imageStyle)
  90.             {
  91.                 default:
  92.                 case IMAGE_TILED:
  93.                 {
  94.                     //Calculate number of images that should be drawn horizontally
  95.                     int numHImages = dim.width / imageWidth;
  96.  
  97.                     //Don't forget remainders
  98.                     if (dim.width % imageWidth != 0)
  99.                         numHImages++;
  100.  
  101.                     //Calculate number of images that should be drawn vertically
  102.                     int numVImages = dim.height / imageHeight;
  103.  
  104.                     //Don't forget remainders
  105.                     if (dim.height % imageHeight != 0)
  106.                         numVImages++;
  107.  
  108.                     int h;
  109.                     int v = 0;
  110.                     for (int vCount = 0;vCount < numVImages;vCount++)
  111.                     {
  112.                         h = 0;
  113.                         for (int hCount = 0;hCount < numHImages;hCount++)
  114.                         {
  115.                             g.drawImage(image, h, v, imageWidth, imageHeight, this);
  116.  
  117.                             //Increment to next column
  118.                             h += imageWidth;
  119.                         }
  120.  
  121.                         //Increment to next row
  122.                         v += imageHeight;
  123.                     }
  124.  
  125.                     break;
  126.                 }
  127.  
  128.                 case IMAGE_CENTERED:
  129.                 {
  130.                     g.drawImage
  131.                         (image,
  132.                          (dim.width - imageWidth) / 2,
  133.                          (dim.height - imageHeight) / 2,
  134.                          imageWidth,
  135.                          imageHeight,
  136.                          this);
  137.  
  138.                     break;
  139.                 }
  140.  
  141.                 case IMAGE_SCALED_TO_FIT:
  142.                 {
  143.                     g.drawImage(image, 0, 0, dim.width, dim.height, this);
  144.  
  145.                     break;
  146.                 }
  147.  
  148.                 case IMAGE_NORMAL:
  149.                 {
  150.                     g.drawImage(image, 0, 0, this);
  151.  
  152.                     break;
  153.                 }
  154.             }//switch
  155.         }
  156.         else
  157.         {
  158.             g.clearRect(0, 0, dim.width, dim.height);
  159.         }
  160.         super.paint(g);
  161.     }
  162.  
  163.     /**
  164.      * Sets the URL of the image to display in this panel.
  165.      * @param url the URL of the image to display
  166.      * @see #getImageURL
  167.      * @exception PropertyVetoException
  168.      * if the specified property value is unacceptable
  169.      */
  170.     public void setImageURL(URL url)
  171.         throws PropertyVetoException
  172.     {
  173.         if (!symantec.itools.util.GeneralUtils.objectsEqual(imageURL,url))
  174.         {
  175.             vetos.fireVetoableChange("imageURL", imageURL, url);
  176.  
  177.             imageURL = url;
  178.             if (imageURL != null)
  179.             {
  180.                 image = getToolkit().getImage(imageURL);
  181.                 if (image != null)
  182.                 {
  183.                     MediaTracker mt = new MediaTracker(this);
  184.                     try
  185.                     {
  186.                         mt.addImage(image, 0);
  187.                         mt.waitForAll();
  188.                     }
  189.                     catch (InterruptedException ie)
  190.                     {
  191.                     }
  192.                 }
  193.             }
  194.             else
  195.             {
  196.                 if(image != null)
  197.                 {
  198.                     image.flush();
  199.                     image = null;
  200.                 }
  201.  
  202.             }
  203.  
  204.             changes.firePropertyChange("imageURL", imageURL, url);
  205.  
  206.             repaint();
  207.         }
  208.     }
  209.  
  210.     /**
  211.      * Returns the URL of the image being displayed in this panel.
  212.      * @see #setImageURL
  213.      */
  214.     public URL getImageURL()
  215.     {
  216.         return imageURL;
  217.     }
  218.  
  219.     /**
  220.      * Sets the new panel image style.
  221.      * @param newStyle the new panel image style, one of
  222.      * IMAGE_TILED, IMAGE_CENTERED, or IMAGE_SCALED_TO_FIT
  223.      * @exception PropertyVetoException
  224.      * if the specified property value is unacceptable
  225.      * @see #getStyle
  226.      * @see #IMAGE_TILED
  227.      * @see #IMAGE_CENTERED
  228.      * @see #IMAGE_SCALED_TO_FIT
  229.      * @see #IMAGE_NORMAL
  230.      */
  231.     public void setStyle(int newStyle) throws PropertyVetoException
  232.     {
  233.         if (newStyle != imageStyle)
  234.         {
  235.             Integer oldStyleInt = new Integer(imageStyle);
  236.             Integer newStyleInt = new Integer(newStyle);
  237.  
  238.             vetos.fireVetoableChange("style", oldStyleInt, newStyleInt);
  239.  
  240.             imageStyle = newStyle;
  241.  
  242.             changes.firePropertyChange("style", oldStyleInt, newStyleInt);
  243.  
  244.             repaint();
  245.         }
  246.     }
  247.  
  248.     /**
  249.      * Gets the current panel image style.
  250.      * @return the current panel image style, one of
  251.      * IMAGE_TILED, IMAGE_CENTERED, or IMAGE_SCALED_TO_FIT
  252.      * @see #setStyle
  253.      * @see #IMAGE_TILED
  254.      * @see #IMAGE_CENTERED
  255.      * @see #IMAGE_SCALED_TO_FIT
  256.      * @see #IMAGE_NORMAL
  257.      */
  258.     public int getStyle()
  259.     {
  260.         return imageStyle;
  261.     }
  262.  
  263.     // Methods
  264.  
  265.     //???RKM??? When beans come around we need to make certain that this is not returned as a property
  266.     /**
  267.      * Returns the image being displayed in this panel.
  268.      */
  269.     public Image getImage()
  270.     {
  271.         return image;
  272.     }
  273.  
  274.     /**
  275.      * Adds a listener for all event changes.
  276.      * @param listener the listener to add.
  277.      * @see #removePropertyChangeListener
  278.      */
  279.     public synchronized void addPropertyChangeListener(PropertyChangeListener listener)
  280.     {
  281.         //super.addPropertyChangeListener(listener);
  282.         changes.addPropertyChangeListener(listener);
  283.     }
  284.  
  285.     /**
  286.      * Removes a listener for all event changes.
  287.      * @param listener the listener to remove.
  288.      * @see #addPropertyChangeListener
  289.      */
  290.     public synchronized void removePropertyChangeListener(PropertyChangeListener listener)
  291.     {
  292.         //super.removePropertyChangeListener(listener);
  293.         changes.removePropertyChangeListener(listener);
  294.     }
  295.  
  296.     /**
  297.      * Adds a vetoable listener for all event changes.
  298.      * @param listener the listener to add.
  299.      * @see #removeVetoableChangeListener
  300.      */
  301.     public synchronized void addVetoableChangeListener(VetoableChangeListener listener)
  302.     {
  303.          //super.addVetoableChangeListener(listener);
  304.         vetos.addVetoableChangeListener(listener);
  305.     }
  306.  
  307.     /**
  308.      * Removes a vetoable listener for all event changes.
  309.      * @param listener the listener to remove.
  310.      * @see #addVetoableChangeListener
  311.      */
  312.     public synchronized void removeVetoableChangeListener(VetoableChangeListener listener)
  313.     {
  314.         //super.removeVetoableChangeListener(listener);
  315.         vetos.removeVetoableChangeListener(listener);
  316.     }
  317.  
  318.     private void readObject(java.io.ObjectInputStream in) throws java.io.IOException, ClassNotFoundException {
  319.         in.defaultReadObject();
  320.  
  321.         if (imageURL != null) {
  322.             image = getToolkit().getImage(imageURL);
  323.             if (image != null) {
  324.                 MediaTracker mt = new MediaTracker(this);
  325.                 try {
  326.                     mt.addImage(image, 0);
  327.                     mt.waitForAll();
  328.                 }
  329.                 catch (InterruptedException ie) { }
  330.             }
  331.         }
  332.     }
  333.  
  334.     /**
  335.      * The style that the image will be displayed in.
  336.      */
  337.     protected int imageStyle;
  338.     
  339.     // Private members
  340.  
  341.     transient private Image image;
  342.     private URL imageURL;
  343.     private symantec.itools.beans.VetoableChangeSupport vetos;
  344.     private symantec.itools.beans.PropertyChangeSupport changes;
  345. }
  346.  
  347.