transparent components

cusax-fix
Yana Stamcheva 20 years ago
parent 7df164739c
commit 98d0dd6c2d

@ -3,6 +3,7 @@
import javax.swing.*;
import net.java.sip.communicator.impl.gui.main.utils.AntialiasingManager;
import net.java.sip.communicator.impl.gui.main.utils.Constants;
import java.awt.*;
@ -12,37 +13,21 @@
public class TransparentBackground extends JComponent
implements ComponentListener, WindowFocusListener/*, Runnable*/ {
public class TransparentBackground extends JComponent {
private Window frame;
private BufferedImage background;
private long lastUpdate = 0;
private boolean refreshRequested = true;
private Robot robot;
private Rectangle screenRect;
private Window window;
// constructor -------------------------------------------------------------
public TransparentBackground(Window frame) {
this.frame = frame;
public TransparentBackground(Window window) {
this.window = window;
this.updateBackground();
this.frame.addComponentListener(this);
this.frame.addWindowFocusListener(this);
//new Thread(this).start();
}
@ -61,29 +46,14 @@ protected void updateBackground() {
return;
}
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
background = robot.createScreenCapture(
new Rectangle(0,0,(int)dim.getWidth( ),
(int)dim.getHeight( )));
}
protected void refresh() {
if (this.frame.isVisible() && this.isVisible()) {
repaint();
this.refreshRequested = true;
this.lastUpdate = System.currentTimeMillis();
}
background = robot.createScreenCapture(
new Rectangle(0, 0, (int)dim.getWidth( ),
(int)dim.getHeight( )));
}
// JComponent --------------------------------------------------------------
@ -94,130 +64,19 @@ protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
Point pos = this.getLocationOnScreen();
Point offset = new Point(-pos.x,-pos.y);
BufferedImage buf = new BufferedImage( this.getWidth(),
this.getHeight(),
BufferedImage.TYPE_INT_RGB);
buf.getGraphics().drawImage(this.background, offset.x, offset.y, null);
g2.drawImage(buf, 0, 0, null);
Point offset = new Point(-pos.x,-pos.y);
g2.drawImage(this.background, offset.x, offset.y, null);
g2.setColor(new Color(255, 255, 255, 180));
g2.fillRoundRect(0, 0, getWidth(), getHeight(), 10, 10);
g2.setColor(Color.GRAY);
g2.setColor(Constants.TOOLBAR_SEPARATOR_COLOR);
g2.drawRoundRect(0, 0, getWidth() - 1, getHeight() - 1, 10, 10);
}
// ComponentListener -------------------------------------------------------
public void componentHidden(ComponentEvent e) {
}
public void componentMoved(ComponentEvent e) {
repaint();
}
public void componentResized(ComponentEvent e) {
repaint();
}
public void componentShown(ComponentEvent e) {
repaint();
}
// WindowFocusListener -----------------------------------------------------
public void windowGainedFocus(WindowEvent e) {
refresh();
}
public void windowLostFocus(WindowEvent e) {
refresh();
}
// Runnable ----------------------------------------------------------------
/*
public void run() {
try {
while (true) {
Thread.sleep(100);
long now = System.currentTimeMillis();
if (this.refreshRequested && ((now - this.lastUpdate) > 1000)) {
if (this.frame.isVisible()) {
Point location = this.frame.getLocation();
this.frame.setLocation(-this.frame.getWidth(), -this.frame.getHeight());
//this.frame.hide();
updateBackground();
//this.frame.show();
this.frame.setLocation(location);
refresh();
}
this.lastUpdate = now;
this.refreshRequested = false;
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
*/
public static void main(String[] args) {
//JFrame frame = new JFrame("Transparent Window");
JWindow frame = new JWindow();
TransparentBackground bg = new TransparentBackground(frame);
//frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(bg);
frame.pack();
frame.setSize(200, 200);
frame.setLocation(300, 300);
frame.setVisible(true);
}
}

@ -3,6 +3,7 @@
import javax.swing.*;
import net.java.sip.communicator.impl.gui.main.utils.AntialiasingManager;
import net.java.sip.communicator.impl.gui.main.utils.Constants;
import java.awt.*;
@ -12,10 +13,13 @@
public class TransparentWindow extends JPopupMenu
public class TransparentFrameBackground extends JComponent
implements ComponentListener, WindowFocusListener, Runnable {
implements ComponentListener, Runnable {
private Window frame;
private BufferedImage background;
private long lastUpdate = 0;
@ -28,13 +32,18 @@ public class TransparentWindow extends JPopupMenu
// constructor -------------------------------------------------------------
public TransparentWindow() {
public TransparentFrameBackground(Window frame) {
this.frame = frame;
this.updateBackground();
this.addComponentListener(this);
this.frame.addComponentListener(this);
//this.frame.addWindowFocusListener(this);
//new Thread(this).start();
new Thread(this).start();
}
@ -56,15 +65,14 @@ protected void updateBackground() {
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
background = robot.createScreenCapture(
background = robot.createScreenCapture(
new Rectangle(0,0,(int)dim.getWidth( ),
(int)dim.getHeight( )));
}
protected void refresh() {
if (this.isVisible()) {
if (this.frame.isVisible() && this.isVisible()) {
repaint();
@ -80,7 +88,6 @@ protected void refresh() {
// JComponent --------------------------------------------------------------
protected void paintComponent(Graphics g) {
super.paintComponent(g);
AntialiasingManager.activateAntialiasing(g);
@ -98,13 +105,13 @@ protected void paintComponent(Graphics g) {
g2.drawImage(buf, 0, 0, null);
g2.setColor(new Color(255, 255, 255, 150));
g2.setColor(new Color(255, 255, 255, 180));
g2.fillRoundRect(0, 0, getWidth(), getHeight(), 20, 20);
g2.fillRoundRect(0, 0, getWidth(), getHeight(), 10, 10);
g2.setColor(Color.GRAY);
g2.setColor(Constants.TOOLBAR_SEPARATOR_COLOR);
g2.drawRoundRect(0, 0, getWidth() - 1, getHeight() - 1, 20, 20);
g2.drawRoundRect(0, 0, getWidth() - 1, getHeight() - 1, 10, 10);
}
@ -130,6 +137,18 @@ public void componentShown(ComponentEvent e) {
repaint();
}
// WindowFocusListener -----------------------------------------------------
public void windowGainedFocus(WindowEvent e) {
System.out.println("focus gained");
refresh();
}
public void windowLostFocus(WindowEvent e) {
System.out.println("focuslost");
refresh();
}
// Runnable ----------------------------------------------------------------
public void run() {
@ -144,11 +163,11 @@ public void run() {
if (this.refreshRequested && ((now - this.lastUpdate) > 1000)) {
if (this.isVisible()) {
if (this.frame.isVisible()) {
Point location = this.getLocation();
Point location = this.frame.getLocation();
this.setLocation(-this.getWidth(), -this.getHeight());
this.frame.setLocation(-this.frame.getWidth(), -this.frame.getHeight());
//this.frame.hide();
@ -156,7 +175,7 @@ public void run() {
//this.frame.show();
this.setLocation(location);
this.frame.setLocation(location);
refresh();
@ -178,14 +197,14 @@ public void run() {
}
/*
public static void main(String[] args) {
//JFrame frame = new JFrame("Transparent Window");
JWindow frame = new JWindow();
TransparentBackground bg = new TransparentBackground(frame);
TransparentFrameBackground bg = new TransparentFrameBackground(frame);
//frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
@ -200,5 +219,5 @@ public static void main(String[] args) {
frame.setVisible(true);
}
*/
}
Loading…
Cancel
Save