FileDocCategorySizeDatePackage
TransparentBackground.javaAPI DocExample3356Mon Jan 09 11:01:58 GMT 2006None

TransparentBackground

public class TransparentBackground extends JComponent implements ComponentListener, Runnable, WindowFocusListener

Fields Summary
private JFrame
frame
protected Image
background
private long
lastupdate
public boolean
refreshRequested
Constructors Summary
public TransparentBackground(JFrame frame)

    
       
        this.frame = frame;
        updateBackground();
        frame.addComponentListener(this);
        frame.addWindowFocusListener(this);
        new Thread(this).start();
    
Methods Summary
public voidcomponentHidden(java.awt.event.ComponentEvent evt)

 
public voidcomponentMoved(java.awt.event.ComponentEvent evt)

 repaint(); 
public voidcomponentResized(java.awt.event.ComponentEvent evt)

 repaint(); 
public voidcomponentShown(java.awt.event.ComponentEvent evt)

 repaint(); 
public static voidp(java.lang.String str)

        System.out.println(str);
    
public voidpaintComponent(java.awt.Graphics g)

        Point pos = this.getLocationOnScreen();
        Point offset = new Point(-pos.x,-pos.y);
        g.drawImage(background,offset.x,offset.y,null);
    
public voidrefresh()

       if(this.isVisible() && frame.isVisible()) {
            repaint();
            refreshRequested = true;
            lastupdate = new Date().getTime();
        }
    
public voidrun()

        try {
            while(true) {
                Thread.sleep(250);
                long now = new Date().getTime();
                if(refreshRequested && 
                   ((now - lastupdate) > 1000)) {
                    if(frame.isVisible()) {
                        Point location = frame.getLocation();
                        frame.hide();
                        updateBackground();
                        frame.show();
                        frame.setLocation(location);
                        refresh();
                    }
                    lastupdate = now;
                    refreshRequested = false;
                }
            }
        } catch (Exception ex) {
            p(ex.toString());
            ex.printStackTrace();
        }
    
public voidupdateBackground()

        try {
            Robot rbt = new Robot();
            Toolkit tk = Toolkit.getDefaultToolkit();
            Dimension dim = tk.getScreenSize();
            background = rbt.createScreenCapture(
                new Rectangle(0,0,(int)dim.getWidth(),(int)dim.getHeight()));
        } catch (Exception ex) {
            p(ex.toString());
            ex.printStackTrace();
        }
    
public voidwindowGainedFocus(java.awt.event.WindowEvent evt)

 refresh(); 
public voidwindowLostFocus(java.awt.event.WindowEvent evt)

 refresh();