Transparent windows on Linux


JavaFX makes it easy to create non-rectangular windows -- just set a StageStyle.TRANSPARENT on your stage.style.
We use rounded corners on our popup menus, and we also have a non-rectangular and alpha-blended splash screen.



The other day I was checking how the cursors were looking on different platforms, and imagine my horror when I discovered how our new splash screen and rounded menus looked on Linux! Here they are -- as you can see you end up with white rectangles around the supposed-to-be-blended areas:











That looks.... craptastic!



Historically, the JDK didn't support alpha blended windows on Linux. However, that was added a while ago (I'm not sure exactly which version, but I think it was JDK 6 update 14). It turns out that the FX code which initializes the native frame for the stage does not do conditional checking for this; it simply turns off transparency on Linux. Fortunately, there's a System property you can set to force it to respect the transparency flag. You would obviously only do this if you know you are running on a JDK which supports transparency. And that's easy!



In your startup code, do something like this:


if (Utils.IS_LINUX and Utils.jdkAtLeast(1, 6, 0, 14)) {
java.lang.System.setProperty("javafx.allowTransparentStage", "true");
}

There are a couple of utility methods here that are simple - just looking at some system properties to determine whether we're on Linux and whether we're on a particular version of the JDK or higher (if you run on non-Sun/Oracle JDKs you may want to check for that as well).




public def IS_LINUX = osName.contains("linux");
public def IS_MAC = osName.contains("mac");
public def IS_WINDOWS = osName.contains("windows");
public def IS_SOLARIS = osName.contains("solaris");

/\*\*
\* Is the version of the running JDK at least major.minor.micro_update?
\* In 1.6.0_18 macro=1,minor=6,micro=0,update=18
\*/
public function jdkAtLeast(macro: Integer, minor: Integer, micro: Integer, update: Integer): Boolean {
def runtimeVersion = java.lang.System.getProperty("java.runtime.version");
def pattern = java.util.regex.Pattern.compile("\^(\\\\d)\\\\.(\\\\d)\\\\.(\\\\d)_(\\\\d+)-");
def matcher = pattern.matcher(runtimeVersion);
if (matcher.find()) {
def currentMacro = Integer.valueOf(matcher.group(1));
def currentMinor = Integer.valueOf(matcher.group(2));
def currentMicro = Integer.valueOf(matcher.group(3));
def currentUpdate = Integer.valueOf(matcher.group(4));
if (currentMacro < macro or currentMinor < minor or
currentMicro < micro or currentUpdate < update) {
return false;
}
}

true
}


With that modification at startup, we get much nicer results:











P.S. Yes, there's a JIRA issue tracking this to be automatically done by the platform, RT-4797.

P.S.2. This is for desktop; I'm not sure this code is mobile-safe.

Comments:

I'm glad that JavaFX is alive Authoring Tool vaporware serious thought, I pray that everything goes well and this wonderful tool due out soon and thank you very much MR. Tor Norbye'sy his team also hope that the Top Secret comes out soon and we can all enjoy doing great things Luis greetings to all ................................................

Spain

Me alegra saber que Javafx Authoring Tool esta vivo pense que seria vaporware, yo rezo por que todo salga bien y esta hermosa herramienta salga pronto gracias y muchas gracias SR. Tor Norbye´s y tambien a su equipo espero que salga pronto del Top Secret y todos podamos disfrutar haciendo grandes cosas saludos a todos Luis...

Posted by Luis on March 26, 2010 at 06:54 PM PDT #

I'm glad that JavaFX is alive Authoring Tool vaporware serious thought, I pray that everything goes well and this wonderful tool due out soon and thank you very much MR. Tor Norbye'sy his team also hope that the Top Secret comes out soon and we can all enjoy doing great things Luis greetings to all ...

Spain

Me alegra saber que Javafx Authoring Tool esta vivo pense que seria vaporware, yo rezo por que todo salga bien y esta hermosa herramienta salga pronto gracias y muchas gracias SR. Tor Norbye´s y tambien a su equipo espero que salga pronto del Top Secret y todos podamos disfrutar haciendo grandes cosas saludos a todos Luis...

Posted by Luis on March 26, 2010 at 06:56 PM PDT #

Some more comments on the transparency issue on Linux @ http://pathlesspath.blogspot.com/2010/06/how-to-get-java-fx-menus-to-work-on.html

Posted by Dhruva Ray on June 19, 2010 at 06:00 PM PDT #

Dear Tor,

Thank you for the tip, but it doesn't work on my machine. I have looked around on a few forums at it looks as though if you use Gnome with Compiz, there's still a transparency problem.

Posted by Kasper Sørensen on September 18, 2010 at 12:54 AM PDT #

Post a Comment:
Comments are closed for this entry.
About

Tor Norbye

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today