Picture.java
  1  // HTMLParser Library $Name: v1_6_20060319 $ - A java-based parser for HTML
  2  // http://sourceforge.org/projects/htmlparser
  3  // Copyright (C) 2003 Derrick Oswald
  4  //
  5  // Revision Control Information
  6  //
  7  // $Source: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/lexerapplications/thumbelina/Picture.java,v $
  8  // $Author: derrickoswald $
  9  // $Date: 2003/12/16 02:29:56 $
 10  // $Revision: 1.2 $
 11  //
 12  // This library is free software; you can redistribute it and/or
 13  // modify it under the terms of the GNU Lesser General Public
 14  // License as published by the Free Software Foundation; either
 15  // version 2.1 of the License, or (at your option) any later version.
 16  //
 17  // This library is distributed in the hope that it will be useful,
 18  // but WITHOUT ANY WARRANTY; without even the implied warranty of
 19  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 20  // Lesser General Public License for more details.
 21  //
 22  // You should have received a copy of the GNU Lesser General Public
 23  // License along with this library; if not, write to the Free Software
 24  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 25  //
 26  
 27  package org.htmlparser.lexerapplications.thumbelina;
 28  
 29  import java.awt.Image;
 30  import java.awt.Point;
 31  import java.awt.Rectangle;
 32  import java.net.URL;
 33  
 34  /**
 35   * Class to track pictures within the frame.
 36   * Maintains an image, an area and the URL for it.
 37   */
 38  public class Picture extends Rectangle
 39  {
 40      /**
 41       * The origin for new points from the zero args constructor.
 42       */
 43      public static final Point ORIGIN = new Point (0, 0);
 44  
 45      /**
 46       * The URL for the picture.
 47       */
 48      protected URL mURL;
 49  
 50      /**
 51       * The image for the picture.
 52       */
 53      protected Image mImage;
 54  
 55      /**
 56       * The upper left hand corner of the image.
 57       * This doesn't change, even if the image is cropped.
 58       * For example, if the left half of the image is obscured by another,
 59       * the <code>Rectangle</code> fields <code>x</code>, <code>y</code>,
 60       * <code>width</code> and <code>height</code> will change, but the
 61       * origin remains the same.
 62       */
 63      protected Point mOrigin;
 64  
 65      /**
 66       * Construct a Picture.
 67       */
 68      public Picture ()
 69      {
 70          setURL (null);
 71          setImage (null);
 72          setOrigin (ORIGIN);
 73      }
 74  
 75      /**
 76       * Construct a Picture over the area given.
 77       * @param x The x coordinate.
 78       * @param y The y coordinate.
 79       * @param width The width of the picture.
 80       * @param height The height of the picture.
 81       */
 82      public Picture (final int x, final int y, final int width, final int height)
 83      {
 84          super (x, y, width, height);
 85          setURL (null);
 86          setImage (null);
 87          setOrigin (new Point (x, y));
 88      }
 89  
 90      /**
 91       * Construct a picture over the rectangle given.
 92       * @param r The coordinates of the area.
 93       */
 94      public Picture (final Rectangle r)
 95      {
 96          super (r);
 97          setURL (null);
 98          setImage (null);
 99          setOrigin (new Point (r.x, r.y));
100      }
101  
102      /**
103       * Construct a picture from the one given.
104       * @param picture The picture to copy.
105       */
106      public Picture (final Picture picture)
107      {
108          super (picture);
109          setURL (picture.getURL ());
110          setImage (picture.getImage ());
111          setOrigin (picture.getOrigin ());
112      }
113  
114      /**
115       * Getter for property URL.
116       * @return Value of property URL.
117       */
118      public URL getURL ()
119      {
120          return (mURL);
121      }
122  
123      /**
124       * Setter for property URL.
125       * @param url New value of property URL.
126       */
127      public void setURL (final URL url)
128      {
129          mURL = url;
130      }
131  
132      /** Getter for property image.
133       * @return Value of property image.
134       */
135      public Image getImage ()
136      {
137          return (mImage);
138      }
139  
140      /** Setter for property image.
141       * @param image New value of property image.
142       */
143      public void setImage (final Image image)
144      {
145          mImage = image;
146          if (null != image)
147          {
148              width = image.getWidth (null);
149              height = image.getHeight (null);
150          }
151      }
152  
153      /** Getter for property origin.
154       * @return Value of property origin.
155       */
156      public Point getOrigin ()
157      {
158          return (mOrigin);
159      }
160  
161      /** Setter for property origin.
162       * @param origin New value of property origin.
163       */
164      public void setOrigin (final Point origin)
165      {
166          mOrigin = origin;
167      }
168  
169      /**
170       * Return <code>true</code> if that picture is the same as this one.
171       * @param picture The picture to check.
172       * @return <code>true</code> if the images match.
173       */
174      public boolean same (final Picture picture)
175      {
176          return (mImage == picture.mImage);
177      }
178  
179      /**
180       * Reset the picture to uncropped size.
181       */
182      public void reset ()
183      {
184          setBounds (mOrigin.x, mOrigin.y,
185              mImage.getWidth (null), mImage.getHeight (null));
186      }
187  
188      /**
189       * Create a string representation of the picture.
190       * @return A string that shows this picture URL and size.
191       */
192      public String toString ()
193      {
194          StringBuffer ret;
195  
196          ret = new StringBuffer ();
197          ret.append (getURL ().toString ());
198          ret.append ("[x=");
199          ret.append (Integer.toString (x));
200          ret.append (",y=");
201          ret.append (Integer.toString (y));
202          ret.append (",width=");
203          ret.append (Integer.toString (width));
204          ret.append (",height=");
205          ret.append (Integer.toString (height));
206          ret.append ("]");
207  
208          return (ret.toString ());
209      }
210  }
211  
212  /*
213   * Revision Control Modification History
214   *
215   * $Log: Picture.java,v $
216   * Revision 1.2  2003/12/16 02:29:56  derrickoswald
217   * Javadoc changes and additions. Stylesheet, overview, build instructions and todo list.
218   * Added HTMLTaglet, an inline Javadoc taglet for embedding HTML into javadocs.
219   *
220   * Revision 1.1  2003/09/21 18:20:56  derrickoswald
221   * Thumbelina
222   * Created a lexer GUI application to extract images behind thumbnails.
223   * Added a task in the ant build script - thumbelina - to create the jar file.
224   * You need JDK 1.4.x to build it.  It can be run on JDK 1.3.x in crippled mode.
225   * Usage: java -Xmx256M thumbelina.jar [URL]
226   *
227   *
228   */