KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > htmlparser > lexerapplications > thumbelina > Sequencer


1 // HTMLParser Library $Name: v1_5_20050313 $ - 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/Sequencer.java,v $
8
// $Author: derrickoswald $
9
// $Date: 2004/07/31 16:42:30 $
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.Component JavaDoc;
30 import java.awt.Dimension JavaDoc;
31 import java.awt.Image JavaDoc;
32 import java.awt.Insets JavaDoc;
33 import java.awt.Point JavaDoc;
34 import java.net.URL JavaDoc;
35 import java.util.ArrayList JavaDoc;
36 import java.util.Random JavaDoc;
37 import javax.swing.JViewport JavaDoc;
38
39
40 /**
41  * Display received images at a constant rate.
42  */

43 public class Sequencer
44     extends
45         Thread JavaDoc
46 {
47     /**
48      * The default delay time, {@value} milliseconds.
49      */

50     protected static final int DEFAULT_DELAY = 500;
51
52     /**
53      * The thumbelina object to drive.
54      */

55     protected Thumbelina mThumbelina;
56
57     /**
58      * Pictures awaiting display.
59      */

60     protected ArrayList JavaDoc mPending;
61
62     /**
63      * Activity state.
64      * <code>true</code> means fetching and displaying, <code>false</code> not.
65      */

66     protected boolean mActive;
67
68     /**
69      * Delay between picture displays.
70      */

71     protected int mDelay;
72
73     /**
74      * Random number generator for picture placement.
75      */

76     protected Random JavaDoc mRandom;
77
78     /**
79      * Creates a new instance of a Sequencer.
80      * @param thumbelina The object to push images to.
81      */

82     public Sequencer (final Thumbelina thumbelina)
83     {
84         mThumbelina = thumbelina;
85         mPending = new ArrayList JavaDoc ();
86         mActive = true;
87         setDelay (DEFAULT_DELAY);
88         mRandom = new Random JavaDoc ();
89         setName ("Sequencer"); // only good if there's just one of these
90
start ();
91     }
92
93     /**
94      * Clears the pending images list.
95      */

96     public void reset ()
97     {
98         synchronized (mPending)
99         {
100             mPending.clear ();
101             mThumbelina.mReadyProgress.setValue (0);
102             mPending.notify ();
103         }
104     }
105
106     /**
107      * Compute a random point to load the image.
108      * Generate a random point for one of the corners of the image and
109      * then condition the numbers so the image is on screen.
110      * @param url The url this picture was fetched from.
111      * Used in computing the random position, so the picture is always
112      * placed in the same location, even when refetched.
113      * @param width The width of the image.
114      * @param height The height of the image.
115      * @return The random point to use.
116      */

117     protected Point JavaDoc random (final String JavaDoc url, final int width, final int height)
118     {
119         Component JavaDoc parent;
120         Component JavaDoc grandparent;
121         Dimension JavaDoc dim;
122         Insets JavaDoc insets;
123         int minx;
124         int miny;
125         int maxx;
126         int maxy;
127         int rndx;
128         int rndy;
129         int corner;
130         Point JavaDoc ret;
131
132         parent = mThumbelina.getPicturePanel ().getParent ();
133         if (parent instanceof JViewport JavaDoc)
134         {
135             grandparent = parent.getParent (); // JScrollPane
136
dim = grandparent.getSize ();
137         }
138         else
139             dim = mThumbelina.getPicturePanel ().getSize ();
140         insets = mThumbelina.getPicturePanel ().getInsets ();
141         dim.width -= (insets.left + insets.right);
142         dim.height -= (insets.top + insets.bottom);
143         minx = insets.left;
144         miny = insets.top;
145         maxx = minx + dim.width;
146         maxy = miny + dim.height;
147         mRandom.setSeed ((((long)(width + height)) << 32) + url.hashCode ());
148         rndx = (int)(mRandom.nextDouble () * dim.width);
149         rndy = (int)(mRandom.nextDouble () * dim.height);
150         corner = (int)(mRandom.nextDouble () * 4); // the panel has four corners
151
ret = new Point JavaDoc (0, 0);
152         switch (corner)
153         {
154             case 0: // upper left
155
if (rndx + width >= maxx)
156                     ret.x = maxx - width;
157                 else
158                     ret.x = rndx;
159                 if (rndy + height >= maxy)
160                     ret.y = maxy - height;
161                 else
162                     ret.y = rndy;
163                 break;
164             case 1: // upper right
165
if (rndx - width < minx)
166                     ret.x = minx;
167                 else
168                     ret.x = rndx - width;
169                 if (rndy + height >= maxy)
170                     ret.y = maxy - height;
171                 else
172                     ret.y = rndy;
173                 break;
174             case 2: // lower right
175
if (rndx - width < minx)
176                     ret.x = minx;
177                 else
178                     ret.x = rndx - width;
179                 if (rndy - height < miny)
180                     ret.y = miny;
181                 else
182                     ret.y = rndy - height;
183                 break;
184             case 3: // lower left
185
if (rndx + width >= maxx)
186                     ret.x = maxx - width;
187                 else
188                     ret.x = rndx;
189                 if (rndy - height < miny)
190                     ret.y = miny;
191                 else
192                     ret.y = rndy - height;
193                 break;
194             default:
195                 throw new IllegalStateException JavaDoc ("random corner = " + corner);
196         }
197
198         // if it's really large stuff it in the upper left hand corner
199
if (ret.x < 0)
200             ret.x = 0;
201         if (ret.y < 0)
202             ret.y = 0;
203
204
205         return (ret);
206     }
207
208     /**
209      * Add an image to the pending list.
210      * @param image The image to add.
211      * @param url The url the image came from.
212      */

213     public void add (final Image JavaDoc image, final URL JavaDoc url)
214     {
215         add (image, url, true);
216     }
217
218     /**
219      * Add an image to the panel.
220      * @param image The image to add.
221      * @param url The url the image came from.
222      * @param background If <code>true</code>, just add to pending list.
223      */

224     public void add (final Image JavaDoc image, final URL JavaDoc url, final boolean background)
225     {
226         Picture picture;
227         int size;
228
229         picture = new Picture ();
230         picture.setImage (image);
231         picture.setURL (url);
232         if (background)
233             synchronized (mPending)
234             {
235                 mPending.add (picture);
236                 size = mPending.size ();
237                 if (mThumbelina.mReadyProgress.getMaximum () < size)
238                     mThumbelina.mReadyProgress.setMaximum (size);
239                 mThumbelina.mReadyProgress.setValue (size);
240                 mPending.notify ();
241             }
242         else
243             place (picture, false);
244     }
245
246     /**
247      * Place a picture in the display area.
248      * Places the picture at a random location on screen.
249      * @param picture The picture to place on screen.
250      * @param add If <code>true</code>, the picture is added to the history.
251      */

252     protected void place (final Picture picture, final boolean add)
253     {
254         Point JavaDoc p;
255
256         if (Picture.ORIGIN == picture.getOrigin ())
257         {
258             // never been placed before
259
p = random (
260                 picture.getURL ().toExternalForm (),
261                 picture.width,
262                 picture.height);
263             picture.x = p.x;
264             picture.y = p.y;
265             picture.setOrigin (p);
266         }
267         mThumbelina.getPicturePanel ().draw (picture, add);
268     }
269
270     //
271
// Runnable interface
272
//
273

274     /**
275      * Display pictures from pending list with delay between.
276      * If the list is empty it waits on the pending list for new pictures.
277      */

278     public void run ()
279     {
280         Picture picture;
281         int size;
282
283         while (true)
284         {
285             try
286             {
287                 picture = null;
288                 synchronized (mPending)
289                 {
290                     if (mActive && !mPending.isEmpty ())
291                         picture = (Picture)mPending.remove (0);
292                     else
293                         try
294                         {
295                             mPending.wait ();
296                         }
297                         catch (InterruptedException JavaDoc ie)
298                         {
299                             ie.printStackTrace ();
300                         }
301                     size = mPending.size ();
302                     if (mThumbelina.mReadyProgress.getMaximum () < size)
303                         mThumbelina.mReadyProgress.setMaximum (size);
304                     mThumbelina.mReadyProgress.setValue (size);
305                 }
306                 if (null != picture)
307                 {
308                     place (picture, true);
309                     if (0 != getDelay ())
310                         try
311                         {
312                             sleep (getDelay ());
313                         }
314                         catch (InterruptedException JavaDoc ie)
315                         {
316                             ie.printStackTrace ();
317                         }
318                 }
319             }
320             catch (Throwable JavaDoc t)
321             {
322                 t.printStackTrace ();
323             }
324         }
325     }
326
327     /**
328      * Getter for property delay.
329      * @return Value of property delay.
330      */

331     public int getDelay ()
332     {
333         return (mDelay);
334     }
335
336     /**
337      * Setter for property delay.
338      * @param delay New value of property delay.
339      */

340     public void setDelay (final int delay)
341     {
342         mDelay = delay;
343     }
344
345 }
346
347 /*
348  * Revision Control Modification History
349  *
350  * $Log: Sequencer.java,v $
351  * Revision 1.2 2004/07/31 16:42:30 derrickoswald
352  * Remove unused variables and other fixes exposed by turning on compiler warnings.
353  *
354  * Revision 1.1 2003/09/21 18:20:56 derrickoswald
355  * Thumbelina
356  * Created a lexer GUI application to extract images behind thumbnails.
357  * Added a task in the ant build script - thumbelina - to create the jar file.
358  * You need JDK 1.4.x to build it. It can be run on JDK 1.3.x in crippled mode.
359  * Usage: java -Xmx256M thumbelina.jar [URL]
360  *
361  *
362  */

363
Popular Tags