aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/java/sip/communicator/impl/gui/customcontrols/TransparentBackground.java
blob: 116c395c9c5d7b18e9908ace51402af6f7e92e14 (plain)
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
 * SIP Communicator, the OpenSource Java VoIP and Instant Messaging client.
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */
package net.java.sip.communicator.impl.gui.customcontrols;

import java.awt.*;
import java.awt.image.*;

import javax.swing.*;

import net.java.sip.communicator.impl.gui.utils.*;
import net.java.sip.communicator.swing.*;

/**
 * The <tt>TransparentBackground</tt> is a <tt>JComponent</tt>, which is
 * added to a <tt>Window</tt> in order to make it transparent.
 * <p>
 * <b>How to use the <tt>TransparentBackground</tt>?</b>
 * The <tt>TransparentBackground</tt> is created and added to the content pane
 * of the <tt>Window</tt> that should be made transparent. All other components
 * then are added to the <tt>TransparentBackground</tt> component and not
 * directly to the window content pane.
 * <p>
 * <b>How it works?</b>
 * The <tt>TransparentBackground</tt> is a <tt>JComponent</tt> which is not
 * really transparent, but only looks like. It overrides the
 * <code>paintComponent</code> method of <tt>JComponent</tt> to paint its
 * own background image, which is an exact image of the screen at the position
 * where the window will apear and with the same size. The
 * <tt>java.awt.Robot</tt> class is used to make the screen capture.
 * <p>
 * Note that the effect of transparence is gone when behind there is an
 * application which shows dynamic images or something the moves, like
 * a movie for example. 
 * 
 * @author Yana Stamcheva
 */
public class TransparentBackground extends JComponent {
    private BufferedImage background;

    private Robot robot;

    private final Window window;

    /**
     * Creates an instance of <tt>TransparentBackground</tt> by specifying
     * the parent <tt>Window</tt> - this is the window that should be made
     * transparent.
     * 
     * @param window The parent <tt>Window</tt>
     */
    public TransparentBackground(Window window) {

        this.window = window;

        try {

            robot = new Robot();

        } catch (AWTException e) {

            e.printStackTrace();

            return;

        }
    }

    /**
     * Updates the background. Makes a new screen capture at the given
     * coordiantes.
     * 
     * @param x The x coordinate.
     * @param y The y coordinate.
     */
    public void updateBackground(int x, int y) {
        this.background = robot.createScreenCapture(new Rectangle(x, y, x
                + this.window.getWidth(), y + this.window.getHeight()));

    }

    /**
     * Overrides the <code>paintComponent</code> method in <tt>JComponent</tt>
     * to paint the screen capture image as a background of this component.
     */
    protected void paintComponent(Graphics g)
    {
        g = g.create();
        try
        {
            AntialiasingManager.activateAntialiasing(g);

            Graphics2D g2 = (Graphics2D) g;
            int width = getWidth();
            int height = getHeight();

            g2.drawImage(this.background, 0, 0, null);

            g2.setColor(new Color(255, 255, 255, 180));

            g2.fillRoundRect(0, 0, width, height, 10, 10);

            g2.setColor(Constants.BORDER_COLOR);

            g2.drawRoundRect(0, 0, width - 1, height - 1, 10, 10);
        }
        finally
        {
            g.dispose();
        }
    }
}