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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
|
import java.awt.AWTException;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JPanel;
import javax.swing.JWindow;
public class FrameTransparente extends JWindow {
private BufferedImage image;
private long updateTime; //CF: LIGNE 67
private Timer timer; //CF: LIGNE 67
private TimerTask tache; //CF: LIGNE 67
public FrameTransparente() {
pack();
setSize(400, 400);
setLocationRelativeTo(null);
setContentPane(new panCapture());
setVisible(true);
}
public void screenCapture() {
try{
setVisible(false);
Robot robot = new Robot();
image = robot.createScreenCapture(new Rectangle(java.awt.Toolkit.getDefaultToolkit().getScreenSize()) );
setVisible(true);
}catch(AWTException ex) {
System.out.println(ex);
}
}
public BufferedImage getImage() {
return image;
}
public static void main(String[] args) {
new FrameTransparente();
}
class panCapture extends JPanel {
public panCapture() {
addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
screenCapture();
}
});
/******************************************************************************
* J'AI ESSAYE DE CREER UN TIMER AFIN DE METTRE A JOUR REGULIEREMENT
* LE BUFFEREDIMAGE, MAIS CELA ENTRAINE UN CLIGNOTEMENT DUE AU FAIT QUE JE CACHE
* LA FENETRE AVANT DE PREND UNE CAPTURE, SANS ÇA JE N'EST PAS UNE BONNE
* TRANSPARENCE DE LA JWINDOWS!!!
******************************************************************************/
updateTime = 1000;
timer = new Timer();
tache = new TimerTask() {
@Override
public void run() {
setVisible(false); // On cache la fenetre
screenCapture(); // On fait une capture
setVisible(true); // la fenetre peut redevenir visible
}
};
timer.scheduleAtFixedRate(tache,0,updateTime);
}
@Override
public void paintComponent(Graphics g) {
g.drawImage(getImage(),
0,0,
getWidth(),getHeight(),
getLocationOnScreen().x,getLocationOnScreen().y,
getLocationOnScreen().x+getWidth(),getLocationOnScreen().y+getHeight(),
null);
}
}
} |
Partager