/* * Jitsi, 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.plugin.desktoputil.*; /** * The TransparentBackground is a JComponent, which is * added to a Window in order to make it transparent. *
* How to use the TransparentBackground? * The TransparentBackground is created and added to the content pane * of the Window that should be made transparent. All other components * then are added to the TransparentBackground component and not * directly to the window content pane. *
* How it works?
* The TransparentBackground is a JComponent which is not
* really transparent, but only looks like. It overrides the
* paintComponent
method of JComponent 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
* java.awt.Robot class is used to make the screen capture.
*
* 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 static final long serialVersionUID = 1L;
private BufferedImage background;
private final Robot robot;
private final Window window;
/**
* Creates an instance of TransparentBackground by specifying
* the parent Window - this is the window that should be made
* transparent.
*
* @param window The parent Window
*/
public TransparentBackground(Window window) {
this.window = window;
Robot rbt;
try {
rbt = new Robot();
} catch (AWTException e) {
e.printStackTrace();
rbt = null;
}
this.robot = rbt;
}
/**
* 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 paintComponent
method in JComponent
* to paint the screen capture image as a background of this component.
*/
@Override
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();
}
}
}