1|package com.infocom.print;
  2|
  3|/**
  4| * Class: PFImage <p>
  5| *
  6| * Render an image on a <code>PFPage</code> object.
  7| * This class will render an image of type GIF or JPEG.
  8| * The image will be rendered using the specified height
  9| * and width
 10| *
 11| * @author Jean-Pierre Dube <jpdube@videotron.ca>
 12| * @version 1.0
 13| * @since 1.0
 14| * @see PFPrintObject
 15| */
 16|
 17|import java.awt.MediaTracker;
 18|import java.net.URL;
 19|import java.awt.Image;
 20|import java.awt.Graphics2D;
 21|import java.awt.Canvas;
 22|import java.awt.Toolkit;
 23|import java.net.MalformedURLException;
 24|import java.awt.image.BufferedImage;
 25|
 26|
 27|public class PFImage extends PFPrintObject {
 28|
 29|   //--- Private instances declarations
 30|   private Canvas canvas = new Canvas ();
 31|   private MediaTracker mt = new MediaTracker (canvas);
 32|   private URL imageURL = null;
 33|   private Image image;
 34|
 35|
 36|   /**
 37|    * Constructor: PFImage <p>
 38|    *
 39|    * Default constructor
 40|    *
 41|    */
 42|   public PFImage () {
 43|
 44|   }
 45|
 46|
 47|   /**
 48|    * Method: setURL <p>
 49|    *
 50|    * Set the <code>URL</code> and load the image
 51|    * in an <code>Image</code> object
 52|    *
 53|    * @param parURL a value of type URL
 54|    */
 55|   public void setURL (String parURL) {
 56|
 57|      try {
 58|
 59|         imageURL = new URL (parURL);
 60|      }
 61|      catch (MalformedURLException me) {
 62|         me.printStackTrace ();
 63|      }
 64|
 65|      //--- Load the image and wait for it to load
 66|      image = Toolkit.getDefaultToolkit().getImage (imageURL);
 67|      mt.addImage (image, 0);
 68|      try {
 69|         mt.waitForID (0);
 70|      }
 71|      catch (InterruptedException e) {
 72|      }
 73|   }
 74|
 75|
 76|   /**
 77|    * Method: print <p>
 78|    *
 79|    * Render the image.
 80|    *
 81|    * @param parG a value of type Graphics2D
 82|    */
 83|   public void print (Graphics2D parG) {
 84|
 85|      //--- Compute the size and position of this object
 86|      computePositionAndSize ();
 87|
 88|      //--- Render the image on the sheet
 89|      parG.drawImage (image,
 90|                     (int) getDrawingOrigin ().getX ().getPoints (),
 91|                     (int) getDrawingOrigin ().getY ().getPoints (),
 92|                     (int) getDrawingSize ().getWidth ().getPoints (),
 93|                     (int) getDrawingSize ().getHeight ().getPoints (),
 94|                     canvas);
 95|
 96|      //--- Draw childs
 97|      printChilds (parG);
 98|   }
 99|
100|
101|   /**
102|    * Method: getBufferedImage <p>
103|    *
104|    * Return the loaded image as a <code>BufferedImage</code>.
105|    * Use this method when you need to process the image before
106|    * it is rendered.
107|    *
108|    * @return a value of type BufferedImage
109|    */
110|   public BufferedImage getBufferedImage () {
111|
112|      return ((BufferedImage) image);
113|
114|   }
115|
116|
117|
118|}// PFImage