1 27 package org.htmlparser.lexerapplications.thumbelina; 28 29 import java.awt.Component ; 30 import java.awt.Dimension ; 31 import java.awt.Image ; 32 import java.awt.Insets ; 33 import java.awt.Point ; 34 import java.net.URL ; 35 import java.util.ArrayList ; 36 import java.util.Random ; 37 import javax.swing.JViewport ; 38 39 40 43 public class Sequencer 44 extends 45 Thread 46 { 47 50 protected static final int DEFAULT_DELAY = 500; 51 52 55 protected Thumbelina mThumbelina; 56 57 60 protected ArrayList mPending; 61 62 66 protected boolean mActive; 67 68 71 protected int mDelay; 72 73 76 protected Random mRandom; 77 78 82 public Sequencer (final Thumbelina thumbelina) 83 { 84 mThumbelina = thumbelina; 85 mPending = new ArrayList (); 86 mActive = true; 87 setDelay (DEFAULT_DELAY); 88 mRandom = new Random (); 89 setName ("Sequencer"); start (); 91 } 92 93 96 public void reset () 97 { 98 synchronized (mPending) 99 { 100 mPending.clear (); 101 mThumbelina.mReadyProgress.setValue (0); 102 mPending.notify (); 103 } 104 } 105 106 117 protected Point random (final String url, final int width, final int height) 118 { 119 Component parent; 120 Component grandparent; 121 Dimension dim; 122 Insets insets; 123 int minx; 124 int miny; 125 int maxx; 126 int maxy; 127 int rndx; 128 int rndy; 129 int corner; 130 Point ret; 131 132 parent = mThumbelina.getPicturePanel ().getParent (); 133 if (parent instanceof JViewport ) 134 { 135 grandparent = parent.getParent (); 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); ret = new Point (0, 0); 152 switch (corner) 153 { 154 case 0: 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: 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: 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: 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 ("random corner = " + corner); 196 } 197 198 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 213 public void add (final Image image, final URL url) 214 { 215 add (image, url, true); 216 } 217 218 224 public void add (final Image image, final URL 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 252 protected void place (final Picture picture, final boolean add) 253 { 254 Point p; 255 256 if (Picture.ORIGIN == picture.getOrigin ()) 257 { 258 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 274 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 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 ie) 315 { 316 ie.printStackTrace (); 317 } 318 } 319 } 320 catch (Throwable t) 321 { 322 t.printStackTrace (); 323 } 324 } 325 } 326 327 331 public int getDelay () 332 { 333 return (mDelay); 334 } 335 336 340 public void setDelay (final int delay) 341 { 342 mDelay = delay; 343 } 344 345 } 346 347 363 | Popular Tags |