1 47 48 package org.jfree.util; 49 50 import java.awt.Graphics ; 51 import java.awt.Image ; 52 import java.awt.image.BufferedImage ; 53 import java.awt.image.ImageObserver ; 54 import java.io.Serializable ; 55 56 66 public class WaitingImageObserver implements ImageObserver , Serializable , 67 Cloneable 68 { 69 70 static final long serialVersionUID = -807204410581383550L; 71 72 73 private boolean lock; 74 75 76 private Image image; 77 78 79 private boolean error; 80 81 87 public WaitingImageObserver(final Image image) { 88 if (image == null) { 89 throw new NullPointerException (); 90 } 91 this.image = image; 92 this.lock = true; 93 } 94 95 114 public synchronized boolean imageUpdate( 115 final Image img, 116 final int infoflags, 117 final int x, 118 final int y, 119 final int width, 120 final int height) { 121 if ((infoflags & ImageObserver.ALLBITS) == ImageObserver.ALLBITS) { 122 this.lock = false; 123 this.error = false; 124 notifyAll(); 125 return false; 126 } 127 else if ((infoflags & ImageObserver.ABORT) == ImageObserver.ABORT 128 || (infoflags & ImageObserver.ERROR) == ImageObserver.ERROR) { 129 this.lock = false; 130 this.error = true; 131 notifyAll(); 132 return false; 133 } 134 return true; 136 } 137 138 142 public synchronized void waitImageLoaded() { 143 144 if (this.lock == false) 145 { 146 return; 147 } 148 149 final BufferedImage img = new BufferedImage ( 150 1, 1, BufferedImage.TYPE_INT_RGB 151 ); 152 final Graphics g = img.getGraphics(); 153 154 while (this.lock) { 155 if (g.drawImage(this.image, 0, 0, img.getWidth(this), 156 img.getHeight(this), this)) { 157 return; 158 } 159 160 try { 161 wait(500); 162 } 163 catch (InterruptedException e) { 164 Log.info( 165 "WaitingImageObserver.waitImageLoaded(): InterruptedException thrown", 166 e 167 ); 168 } 169 } 170 } 171 172 180 public Object clone() throws CloneNotSupportedException { 181 return (WaitingImageObserver) super.clone(); 182 } 183 184 public boolean isLoadingComplete() { 185 return this.lock == false; 186 } 187 188 193 public boolean isError() { 194 return this.error; 195 } 196 } 197 | Popular Tags |