9

Is it possible to make a JFrame that has a transparent background and draw an image on it, so only the image will be visible with no border or background?

user
  • 6,166
  • 18
  • 55
  • 83

6 Answers6

14

Yes, it's possible in many ways. This is one of them:

setUndecorated(true);
setBackground(new Color(1.0f,1.0f,1.0f,0.5f));

4th float (which I set to 0.5f) in Color's constructor is alpha channel. It can be 0.0f - 1.0f depend on transparency you want.

Punyapat
  • 339
  • 3
  • 6
11

See Translucent and Shaped Swing Windows by Kirill Grouchnikov.

Community
  • 1
  • 1
McDowell
  • 105,511
  • 29
  • 196
  • 262
  • 8
    Link is now dead. Google suggests this blog post: http://www.pushing-pixels.org/2008/02/27/translucent-and-shaped-windows-in-core-java.html – Mark Peschel Mar 04 '18 at 20:59
4

You should make content pane transparent too.

frame.setUndecorated(true);
frame.getContentPane().setBackground(new Color(1.0f,1.0f,1.0f,0.0f));
frame.setBackground(new Color(1.0f,1.0f,1.0f,0.0f));
ossobuko
  • 851
  • 8
  • 25
1

It is possible.

If your JFrame is a local variable or field:

myJFrame.setUndecorated(true);

If your class extends JFrame:

setUndecorated(true);
Michael
  • 1,239
  • 9
  • 14
Taylor Golden
  • 39
  • 1
  • 7
  • 2
    true that since jdk7, transparency is only supported for undecorated frames. How to call methods should be basic knowledge, though :-) – kleopatra Oct 21 '12 at 09:19
0

For a Mac OS X example, see Re-paint problem on translucent frame/panel/component.

Community
  • 1
  • 1
trashgod
  • 200,320
  • 28
  • 229
  • 974
0

setOpacity(0.50f);//50% opaque

Prakash D
  • 1
  • 1