Transparant JFrame in java swing -


i need create transparant jframe in java swing have opacity of 0.05f. tried code below doesn't work. work in windows.

what need make work?

     import java.awt.alphacomposite;      import java.awt.eventqueue;      import java.awt.graphics;      import java.awt.graphics2d;      import javax.swing.jframe;      import java.awt.color;   public class backgroundnull {     private jframe frame;      public static void main(string[] args) {     eventqueue.invokelater(new runnable() {         public void run() {             try {                 backgroundnull window = new backgroundnull();                 window.frame.setvisible(true);             } catch (exception e) {                 e.printstacktrace();             }         }     }); }  public backgroundnull() {     initialize();  private void initialize() {     frame = new jframe();     frame.setbackground(new color(0, 0, 0, 0));     frame.setbounds(100, 100, 450, 300);     frame.setdefaultcloseoperation(jframe.exit_on_close);     frame.setopacity(0.5f); }      public void paint(graphics g) {          graphics2d g2 = (graphics2d) g.create();          g2.setcomposite(alphacomposite.getinstance(alphacomposite.src_over, 0.0f));           frame.getcontentpane().paint(g2);          g2.dispose();      }   } 

depending on platform, window transparency may work default (metal) @ feel.

i tried using system , feel on mac osx , windows 7 , either work.

the part missing in code this...

jframe.setdefaultlookandfeeldecorated(true);

import java.awt.*; import javax.swing.*; import static java.awt.graphicsdevice.windowtranslucency.*;  public class translucentwindowdemo extends jframe {      public translucentwindowdemo() {         super("translucentwindow");         setlayout(new gridbaglayout());          setsize(300, 200);         setlocationrelativeto(null);         setdefaultcloseoperation(jframe.exit_on_close);          //add sample button.         add(new jbutton("i button"));     }      public static void main(string[] args) {         // determine if graphicsdevice supports translucency.         graphicsenvironment ge =                         graphicsenvironment.getlocalgraphicsenvironment();         graphicsdevice gd = ge.getdefaultscreendevice();          //if translucent windows aren't supported, exit.         if (!gd.iswindowtranslucencysupported(translucent)) {             system.err.println(                             "translucency not supported");             system.exit(0);         }          jframe.setdefaultlookandfeeldecorated(true);          // create gui on event-dispatching thread         swingutilities.invokelater(new runnable() {             @override             public void run() {                 try {                     uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname());                 } catch (classnotfoundexception | instantiationexception | illegalaccessexception | unsupportedlookandfeelexception ex) {                 }                 translucentwindowdemo tw = new translucentwindowdemo();                  // set window 55% opaque (45% translucent).                 tw.setopacity(0.55f);                  // display window.                 tw.setvisible(true);             }         });     } } 

now, here's sad news. under java 6, can make work.

the following code (under java 6) make native window transparent...

public static void setopacity(window window, float opacity) {     try {         class<?> awtutilsclass = class.forname("com.sun.awt.awtutilities");         if (awtutilsclass != null) {             method method = awtutilsclass.getmethod("setwindowopacity", window.class, float.class);             method.invoke(null, window, opacity);         }     } catch (exception exp) {         exp.printstacktrace();     } } 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -