-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTransparentFrame.java
More file actions
executable file
·55 lines (49 loc) · 1.65 KB
/
TransparentFrame.java
File metadata and controls
executable file
·55 lines (49 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class TransparentFrame extends JFrame {
Image background;
Point latestPos;
public static void main() {
TransparentFrame t = new TransparentFrame();
t.setSize(640, 480);
t.setVisible(true);
}
public TransparentFrame() {
super();
updateBackground();
new Timer(5, new ActionListener() {
public void actionPerformed(ActionEvent a) {
System.out.println("Repainting");
repaint();
}
}).start();
addWindowListener(new WindowAdapter() {
public void windowDeactivated(WindowEvent w) {
//Update background intelligently
updateBackground();
}
});
latestPos = new Point(0,0);
}
public void updateBackground() {
try {
boolean visible = isVisible();
Robot r = new Robot();
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension dim = tk.getScreenSize();
setVisible(false);
background = r.createScreenCapture(
new Rectangle(0,0,(int)dim.getWidth(),(int)dim.getHeight()));
setVisible(visible);
} catch (Exception e) {}
}
public void paint(Graphics g) {
if(getLocationOnScreen().equals(latestPos)){return;}
Point pos = getLocationOnScreen();
Point offset= new Point(-pos.x,-pos.y);
System.out.println("Repainting to " + pos.x + ", " + pos.y);
g.drawImage(background,offset.x,offset.y,null);
latestPos = pos;
}
}