4

Hello everyone im made a swing application in java, but problem is that how i can achive the transparent frame in java.

I want to make a user interface design as in attach image, but probem is that frames is not transparent, so it showing the default color if there is no components on that layer.

Like on the right side i want to show this frame as a transparent on the top free and bottom free portion,

I use AWTUtilities, but it not works for me.

how can i do it, plz reply,

also give me a hint to drag the window by title bar, in undecorated window

thanks in advanceenter image description here

DEVANG SHARMA
  • 2,662
  • 5
  • 33
  • 52
  • possible duplicate of [How to make JFrame transparent?](http://stackoverflow.com/questions/6660908/how-to-make-jframe-transparent) – Harry Joy Dec 05 '11 at 09:43
  • I would really really really like to know how you came about this design, I have been trying to work out something of this nature for a very long time now to no avail, I hope you still use stackoverflow, please let me know if you are okay with this. I would like to know how you designed the JFrame to look like that, and how you customised your buttons, Graphics is the reason I creack my head everyday on Java. – George Sep 23 '13 at 15:26

1 Answers1

3

Transparent background component

public class TransparentBackground extends Jcomponent { 
    private JFrame frame; 
    private Image background;

public TransparentBackground(JFrame frame) {
    this.frame = frame;
    updateBackground( );
}

public void updateBackground( ) {
    try {
        Robot rbt = new Robot( );
        Toolkit tk = Toolkit.getDefaultToolkit( );
        Dimension dim = tk.getScreenSize( );
        background = rbt.createScreenCapture(
        new Rectangle(0,0,(int)dim.getWidth( ),
                          (int)dim.getHeight( )));
    } catch (Exception ex) {
        p(ex.toString( ));
        ex.printStackTrace( );
    }
}
public void paintComponent(Graphics g) {
    Point pos = this.getLocationOnScreen( );
    Point offset = new Point(-pos.x,-pos.y);
    g.drawImage(background,offset.x,offset.y,null);
}
}

You can run this with a simple main( ) method, dropping a few components onto the panel and putting it into a frame:

public static void main(String[] args) {
    JFrame frame = new JFrame("Transparent Window");
    TransparentBackground bg = new TransparentBackground(frame);
    bg.setLayout(new BorderLayout( ));
    JButton button = new JButton("This is a button");
    bg.add("North",button);
    JLabel label = new JLabel("This is a label");
    bg.add("South",label);
    frame.getContentPane( ).add("Center",bg);
    frame.pack( );
    frame.setSize(150,100);
    frame.show( );
}
Veaceslav Gaidarji
  • 4,261
  • 3
  • 38
  • 61