|
|
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.
[ Go Back ]
|
|