| | |
transparent jframe
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jun 2008
Posts: 23
Reputation:
Solved Threads: 0
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..
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..
java Syntax (Toggle Plain Text)
import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; public class emptyframe extends JFrame{ private JLabel BG = new JLabel (new ImageIcon("Earth-06-june.gif")); public emptyframe() { watever(); BG.setBounds(50,50,100,100); add(BG); } private void watever() { setPreferredSize (new Dimension (300, 300)); setLayout (null); setVisible(true); pack(); setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); setLocation(400,250); setBackground(Color.black); } public static void main (String[] args) { new emptyframe().setVisible(true); } }
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.
Regards,
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.
java Syntax (Toggle Plain Text)
import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.awt.image.*; public class TransparentBackground extends JComponent implements ComponentListener, WindowFocusListener, Runnable { // constants --------------------------------------------------------------- // instance ---------------------------------------------------------------- private JFrame _frame; private BufferedImage _background; private long _lastUpdate = 0; private boolean _refreshRequested = true; private Robot _robot; private Rectangle _screenRect; private ConvolveOp _blurOp; // constructor ------------------------------------------------------------- public TransparentBackground(JFrame frame) { _frame = frame; try { _robot = new Robot(); } catch (AWTException e) { e.printStackTrace(); return; } Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); _screenRect = new Rectangle(dim.width, dim.height); float[] my_kernel = { 0.10f, 0.10f, 0.10f, 0.10f, 0.20f, 0.10f, 0.10f, 0.10f, 0.10f}; _blurOp = new ConvolveOp(new Kernel(3, 3, my_kernel)); updateBackground(); _frame.addComponentListener(this); _frame.addWindowFocusListener(this); new Thread(this).start(); } // protected --------------------------------------------------------------- protected void updateBackground() { _background = _robot.createScreenCapture(_screenRect); } protected void refresh() { if (_frame.isVisible() && this.isVisible()) { repaint(); _refreshRequested = true; _lastUpdate = System.currentTimeMillis(); } } // JComponent -------------------------------------------------------------- protected void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; Point pos = this.getLocationOnScreen(); BufferedImage buf = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB); buf.getGraphics().drawImage(_background, -pos.x, -pos.y, null); Image img = _blurOp.filter(buf, null); g2.drawImage(img, 0, 0, null); g2.setColor(new Color(255, 255, 255, 192)); g2.fillRect(0, 0, getWidth(), getHeight()); } // 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 (_refreshRequested && ((now - _lastUpdate) > 1000)) { if (_frame.isVisible()) { Point location = _frame.getLocation(); _frame.setLocation(-_frame.getWidth(), -_frame.getHeight()); updateBackground(); _frame.setLocation(location); refresh(); } _lastUpdate = now; _refreshRequested = false; } } } catch (InterruptedException e) { e.printStackTrace(); } } public static void main(String[] args) { JFrame frame = new JFrame("Transparent Window"); TransparentBackground bg = new TransparentBackground(frame); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(bg); frame.pack(); frame.setSize(200, 200); frame.setLocation(500, 500); frame.setVisible(true); } }
Regards,
Puneet Kalra
www.PuneetK.com
Sun Certified Java Programmer
Admin of Pikk - Object Relational Mapping Framework
www.PuneetK.com
Sun Certified Java Programmer
Admin of Pikk - Object Relational Mapping Framework
Puneet Kalra
www.PuneetK.com
Sun Certified Java Programmer
Admin of Pikk - Object Relational Mapping Framework
www.PuneetK.com
Sun Certified Java Programmer
Admin of Pikk - Object Relational Mapping Framework
![]() |
Similar Threads
- Questions about Applet and JFrame (Java)
- JFrame Issue (Java)
- Java, Jbuilder 2006, Swing Seperation (Java)
Other Threads in the Java Forum
- Previous Thread: Standard input
- Next Thread: adding events in iTemlistner
| Thread Tools | Search this Thread |
android api applet application apps array arrays automation awt bidirectional binary birt bluetooth busy_handler(null) chat class classes client code columns component constructor database designadrawingapplicationusingjavajslider draw eclipse editor error errors event eventlistener exception expand fractal game givemetehcodez graphics gui guidancer html ide image inetaddress input integer intellij j2me java javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia link linux list loop map method methods mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle parsing plazmic print problem program programming project recursion scanner screen server set sharepoint size smart sms smsspam sort sortedmaps sql string subclass support swing textfield threads time tree unlimited utility webservices windows





