transparent jframe

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jun 2008
Posts: 23
Reputation: rude04 is an unknown quantity at this point 
Solved Threads: 0
rude04 rude04 is offline Offline
Newbie Poster

transparent jframe

 
0
  #1
Jan 9th, 2009
how do you make a jframe transparent?...i need to add a png or gif image in the frame..so when i run the program it will only show the image..and the image would float in the desktop..

i also need to add a few components like JButton.. i want the frame to be invisible so the button would also be floating...

i tried setOpaque(false); but in only shows an error..


  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. import javax.swing.event.*;
  5.  
  6. public class emptyframe extends JFrame{
  7.  
  8. private JLabel BG = new JLabel (new ImageIcon("Earth-06-june.gif"));
  9.  
  10.  
  11. public emptyframe()
  12. {
  13. watever();
  14.  
  15. BG.setBounds(50,50,100,100);
  16. add(BG);
  17.  
  18.  
  19. }
  20.  
  21.  
  22. private void watever()
  23. {
  24. setPreferredSize (new Dimension (300, 300));
  25. setLayout (null);
  26. setVisible(true);
  27. pack();
  28. setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
  29. setLocation(400,250);
  30. setBackground(Color.black);
  31.  
  32. }
  33.  
  34. public static void main (String[] args) {
  35.  
  36. new emptyframe().setVisible(true);
  37.  
  38.  
  39. }
  40. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 121
Reputation: puneetkay is on a distinguished road 
Solved Threads: 23
puneetkay's Avatar
puneetkay puneetkay is offline Offline
Junior Poster

Re: transparent jframe

 
1
  #2
Jan 9th, 2009
Hello,

I dont think it possible to make Transparent JFrame (according to my knowledge). You can use java.awt.Window class instead of JFrame.

Or you can use this trick to make it work like transparent.

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

Regards,
Puneet Kalra
www.PuneetK.com
Sun Certified Java Programmer


Admin of Pikk - Object Relational Mapping Framework
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 23
Reputation: rude04 is an unknown quantity at this point 
Solved Threads: 0
rude04 rude04 is offline Offline
Newbie Poster

Re: transparent jframe

 
0
  #3
Jan 9th, 2009
,.,thanks for the reply, the code was good but it only makes it translucent..can i add components like jbutton and jlabel in window?
can you give me a sample code uses "java.awt.Window" that is simple like the code i posted...THANKS again!!
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 121
Reputation: puneetkay is on a distinguished road 
Solved Threads: 23
puneetkay's Avatar
puneetkay puneetkay is offline Offline
Junior Poster

Re: transparent jframe

 
0
  #4
Jan 9th, 2009
This article is for you
http://java.sun.com/developer/techni...haped_windows/

best of luck!
Puneet Kalra
www.PuneetK.com
Sun Certified Java Programmer


Admin of Pikk - Object Relational Mapping Framework
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 23
Reputation: rude04 is an unknown quantity at this point 
Solved Threads: 0
rude04 rude04 is offline Offline
Newbie Poster

Re: transparent jframe

 
0
  #5
Jan 10th, 2009
ill check it out..thanks for the link
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC