Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures

Home

Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips

Books

Submit News
Search the site here...
Search...
 

Transparent Background Java code example - Click here to copy ->>>

   Can't find what you're looking for? Try our search:

Really working examples categorized by API, package, class. You can compile and run our examples right away! Not from source code for Java projects - only working examples! Copy, compile and run!
------------------

Code:

 
import java.awt.*;
import java.util.Date;
import javax.swing.*;
import java.awt.event.*;

/* left to do
   listen to window focused (gained and lost) events
   listen for window uniconified event
*/

public class TransparentBackground extends JComponent
        implements ComponentListener, WindowFocusListener,
        Runnable {
    private JFrame frame;
    protected Image background;
    private long lastupdate = 0;
    public boolean refreshRequested = true;

    public TransparentBackground(JFrame frame) {
        this.frame = frame;
        updateBackground();
        frame.addComponentListener(this);
        frame.addWindowFocusListener(this);
        new Thread(this).start();
    }

    public void updateBackground() {
        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 void paintComponent(Graphics g) {
        Point pos = this.getLocationOnScreen();
        Point offset = new Point(-pos.x,-pos.y);
        g.drawImage(background,offset.x,offset.y,null);
    }


    public  void componentShown(ComponentEvent evt) { repaint(); }
    public  void componentResized(ComponentEvent evt) { repaint(); }
    public  void componentMoved(ComponentEvent evt) { repaint(); }
    public  void componentHidden(ComponentEvent evt) { }

    public void windowGainedFocus(WindowEvent evt) { refresh(); }
    public void windowLostFocus(WindowEvent evt) { refresh(); }

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

/*
    private boolean recurse = false;
    public void quickRefresh() {
        p("quick refresh");
        long now = new Date().getTime();
        if(recurse ||
           ((now - lastupdate) < 1000)) { return; }

        recurse = true;
        Point location = frame.getLocation();
        frame.hide();
        updateBackground();
        frame.show();
        frame.setLocation(location);
        repaint();
        recurse = false;
        lastupdate = now;
    }
    */

    public void run() {
        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 static void p(String str) {
        System.out.println(str);
    }

}

/*   Swing Hacks
 *   Tips and Tools for Killer GUIs
 * By Joshua Marinacci, Chris Adamson
 *   First Edition June 2005
 *   Series: Hacks
 *   ISBN: 0-596-00907-0
 *   http://www.oreilly.com/catalog/swinghks/
 */

 
 



References.

The list of classes which were used on this page you can find below. The links to Java API contain official SUN documentation about all used classes.

  java.awt.Image

  javax.swing.JComponent

  java.awt.event.ComponentListener

  java.awt.event.WindowFocusListener

  java.lang.Runnable

  javax.swing.JFrame

  java.lang.Thread

  java.awt.Robot

  java.awt.Toolkit

  java.awt.Dimension

  java.awt.Rectangle

  java.lang.Exception

  java.awt.Graphics

  java.awt.Point

  java.awt.event.ComponentEvent

  java.awt.event.WindowEvent

  java.sql.Date




[ Go Back ]



Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy