1 26 27 package net.sourceforge.groboutils.uicapture.v1; 28 29 import java.awt.Color ; 30 import java.awt.Point ; 31 import java.awt.Window ; 32 import java.awt.Graphics ; 33 import java.awt.Rectangle ; 34 import java.awt.Image ; 35 import java.awt.event.FocusListener ; 36 import java.awt.event.FocusEvent ; 37 38 39 40 55 public class VirtualWindowUI extends Window implements Runnable , FocusListener 56 { 57 60 63 private boolean enableGlass = false; 64 65 private transient Thread runner = null; 68 private transient Object syncObj = new Object (); 69 private transient boolean doRun = true; 70 71 private transient Image background = null; 72 73 74 private static final Color INVISIBLE = new Color ( 0, 0, 0, 0 ); 75 private static final int SLEEP_LENGTH = 100; 76 77 78 79 82 83 86 public VirtualWindowUI( Window owner ) 87 { 88 super( owner ); 89 90 setBackground( INVISIBLE ); 93 setForeground( INVISIBLE ); 94 95 103 104 setSize( 0, 0 ); 106 super.show(); 107 setGlassEnabled( false ); 108 } 109 110 111 112 113 114 115 118 119 131 public void setGlassEnabled( boolean on ) 132 { 133 synchronized( this.syncObj ) 134 { 135 this.enableGlass = on; 136 137 if (on) 138 { 139 this.syncObj.notifyAll(); 140 maximize(); 141 } 142 } 143 } 144 145 146 152 public boolean isGlassEnabled() 153 { 154 return this.enableGlass; 155 } 156 157 158 161 public void maximize() 162 { 163 synchronized( this.syncObj ) 164 { 165 if (this.enableGlass) 166 { 167 Rectangle rect = getCoveredScreen(); 168 setLocation( rect.getLocation() ); 169 setSize( rect.getSize() ); 170 } 171 } 172 } 173 174 175 178 public Rectangle getCoveredScreen() 179 { 180 return new Rectangle ( 181 new Point ( 0, 0 ), 182 getToolkit().getScreenSize() ); 183 } 184 185 186 190 public void setBackground( Image img ) 191 { 192 synchronized( this.syncObj ) 193 { 194 this.background = img; 195 } 196 } 197 198 199 202 203 206 public void show() 207 { 208 setGlassEnabled( true ); 209 210 super.show(); 211 toFront(); 212 } 213 214 215 218 public void hide() 219 { 220 setGlassEnabled( false ); 221 toBack(); 222 super.hide(); 223 } 224 225 226 229 public void dispose() 230 { 231 if (this.runner != null) 232 { 233 this.doRun = false; 234 synchronized( this.syncObj ) 235 { 236 setGlassEnabled( false ); 237 this.syncObj.notifyAll(); 238 } 239 240 try 242 { 243 this.runner.join( 1000 ); 244 this.runner = null; 245 } 246 catch (InterruptedException ie) 247 { 248 } 251 this.background = null; 252 } 253 254 super.dispose(); 255 } 256 257 258 261 public void update( Graphics g ) 262 { 263 } 265 266 267 270 public void paint( Graphics g ) 271 { 272 synchronized( this.syncObj ) 274 { 275 if (this.background == null) 276 { 277 g.fillRect( 0, 0, getWidth(), getHeight() ); 279 } 280 else 281 { 282 g.drawImage( this.background, 0, 0, this ); 283 } 284 } 285 } 286 287 288 295 296 297 300 301 302 305 public void run() 306 { 307 while (this.doRun) 308 { 309 boolean doToFront = false; 310 try 311 { 312 synchronized( this.syncObj ) 313 { 314 if (this.enableGlass == false) 315 { 316 this.syncObj.wait(); 318 } 319 else 320 { 321 doToFront = true; 322 } 323 } 324 if (doToFront) 325 { 326 toFront(); 328 329 Thread.sleep( SLEEP_LENGTH ); 331 } 332 } 333 catch (InterruptedException ie) 334 { 335 break; 337 } 338 } 339 } 340 341 342 345 public void focusGained( FocusEvent fe ) 346 { 347 } 349 350 351 354 public void focusLost( FocusEvent fe ) 355 { 356 synchronized( this.syncObj ) 358 { 359 if (this.enableGlass) 360 { 361 toFront(); 362 } 363 } 364 } 365 366 367 368 371 372 373 376 protected void finalize() throws Throwable 377 { 378 if (this.runner != null) 379 { 380 dispose(); 381 } 382 383 super.finalize(); 384 } 385 386 } 387 388 | Popular Tags |