Advertisement
Guest User

metric

a guest
Oct 30th, 2010
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.72 KB | None | 0 0
  1. package resources;
  2. import javax.swing.*;
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import java.awt.image.*;
  6.  
  7.  
  8.  
  9. public class TransparentBackground extends JComponent
  10.         implements ComponentListener, WindowFocusListener, Runnable {
  11.  
  12.     // constants ---------------------------------------------------------------
  13.     // instance ----------------------------------------------------------------
  14.     private JFrame _frame;
  15.     private BufferedImage _background;
  16.     private long _lastUpdate = 0;
  17.     private boolean _refreshRequested = true;
  18.     private Robot _robot;
  19.     private Rectangle _screenRect;
  20.     private ConvolveOp _blurOp;
  21.  
  22.     // constructor -------------------------------------------------------------
  23.  
  24.     public TransparentBackground(JFrame frame) {
  25.         _frame = frame;
  26.         try {
  27.             _robot = new Robot();
  28.         } catch (AWTException e) {
  29.             e.printStackTrace();
  30.             return;
  31.         }
  32.  
  33.         Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
  34.         _screenRect = new Rectangle(dim.width, dim.height);
  35.  
  36.         float[] my_kernel = {
  37.                 0.10f, 0.10f, 0.10f,
  38.                 0.10f, 0.20f, 0.10f,
  39.                 0.10f, 0.10f, 0.10f};
  40.         _blurOp = new ConvolveOp(new Kernel(2, 2, my_kernel));
  41.  
  42.         updateBackground();
  43.         _frame.addComponentListener(this);
  44.         _frame.addWindowFocusListener(this);
  45.         new Thread(this).start();
  46.     }
  47.  
  48.     // protected ---------------------------------------------------------------
  49.  
  50.     protected void updateBackground() {
  51.         _background = _robot.createScreenCapture(_screenRect);
  52.     }
  53.  
  54.  
  55.  
  56.     protected void refresh() {
  57.         if (_frame.isVisible() && this.isVisible()) {
  58.             repaint();
  59.             _refreshRequested = true;
  60.             _lastUpdate = System.currentTimeMillis();
  61.         }
  62.     }
  63.  
  64.  
  65.     // JComponent --------------------------------------------------------------
  66.  
  67.     protected void paintComponent(Graphics g) {
  68.         Graphics2D g2 = (Graphics2D) g;
  69.         Point pos = this.getLocationOnScreen();
  70.         BufferedImage buf = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
  71.         buf.getGraphics().drawImage(_background, -pos.x, -pos.y, null);
  72.  
  73.         Image img = _blurOp.filter(buf, null);
  74.         g2.drawImage(img, 0, 0, null);
  75.         g2.setColor(new Color(255, 255, 255, 0));
  76.         g2.fillRect(0, 0, getWidth(), getHeight());
  77.     }
  78.  
  79.     // ComponentListener -------------------------------------------------------
  80.     public void componentHidden(ComponentEvent e) {
  81.     }
  82.  
  83.     public void componentMoved(ComponentEvent e) {
  84.         repaint();
  85.     }
  86.  
  87.     public void componentResized(ComponentEvent e) {
  88.         repaint();
  89.  
  90.     }
  91.  
  92.     public void componentShown(ComponentEvent e) {
  93.         repaint();
  94.     }
  95.  
  96.     // WindowFocusListener -----------------------------------------------------
  97.     public void windowGainedFocus(WindowEvent e) {
  98.         refresh();
  99.     }
  100.  
  101.     public void windowLostFocus(WindowEvent e) {
  102.         refresh();
  103.     }
  104.  
  105.     // Runnable ----------------------------------------------------------------
  106.     public void run() {
  107.         try {
  108.             while (true) {
  109.                 Thread.sleep(100);
  110.                 long now = System.currentTimeMillis();
  111.                 if (_refreshRequested && ((now - _lastUpdate) > 1000)) {
  112.                     if (_frame.isVisible()) {
  113.                         Point location = _frame.getLocation();
  114.                         _frame.setLocation(-_frame.getWidth(), -_frame.getHeight());
  115.                         updateBackground();
  116.                         _frame.setLocation(location);
  117.                         refresh();
  118.                     }
  119.                     _lastUpdate = now;
  120.                     _refreshRequested = false;
  121.                 }
  122.             }
  123.         } catch (InterruptedException e) {
  124.             e.printStackTrace();
  125.         }
  126.     }
  127.  
  128.     public static void main(String[] args) {
  129.         JFrame frame = new JFrame("Transparent Window");
  130.         TransparentBackground bg = new TransparentBackground(frame);
  131.  
  132.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  133.         frame.getContentPane().add(bg);
  134.         frame.pack();
  135.         frame.setSize(200, 200);
  136.         frame.setLocation(500, 500);
  137.         frame.setVisible(true);
  138.     }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement