JavaFX Clock update, now with transparency

This is an update to my previous entry “JavaFX Clock”.

jfxclock-transparent.jpg

I modified the code (JFXClock.fx) to use the JNA library so that the clock can be contained in a transparent window. I also added a right click menu (JFX is really cool for writing menus) so you can pause, resize and exit the application. I didn’t find a corresponding JFX wrapper class for JPopupMenu, but since you can call Java code from JFX it was doable. The syntax is a bit strange, you have to get a handle to the corresponding JComponent in order to add the menus to the popupmenu. I also miss isPopupTrigger(), but there’s an attribute to the mouse event class in jfx to get the mouse button “number”.

Transparent JFXClock

Opaque JFXClock

Updated! 

I was getting some strange exceptions with the example JNA jar file, but thanks to technomage it’s working much better now. I also added an “Alwas on Top” menu item.

Why are there two versions? Well, the Windows version pegs the CPU to almost 100% in my machine. There is some issue with some of the code that JNA uses to handle transparent windows, and every time the clock animates it seems to throw a very long stack trace. Have to investigate what is happening, here’s the stack trace … I was even thinking of not posting this version, but at least it doesn’t lock up my PC. If you get in trouble remember you can stop the animation and everything should be back to normal.

The “opaque” version will turn off the transparency so at least you can play around with the clock outside of JFXPad.

I’m also interested in finding out if the transparent version works in other platforms other than XP. It should work fine on most Windows versions, not sure about Vista. Included in the JNLP download are the binaries for Windows, Linux and Apple (although for this effect, you don’t seem to need JNA in the Mac).

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

30 Comments »

 
  • Andrew says:

    i tried the transparent version with Ubuntu Linux (dapper drake) with GTK2 etc. It doesn't work :-( the opaque version does work, however.

    Cheers,

    Andrew

  • Joe says:

    The transparent one doesnt work for me – windows xp!?

  • Augusto says:

    Andrew, do you get something to appear at all?

    Joe, that's weird. I've tested it on 2 of my own XP systems …

  • Derek says:

    Transparent clock works great on Mac OS X!

  • Adrian says:

    It works! and looks pretty hot! on my XP system. Excellent stuff

  • [...] Update to this entry in this post : JavaFX Clock Update, now with Transparency [...]

  • This "small change" improves your example drastically:

    http://www.saager.org/2007/05/22/java-fx-clock-go...

  • Andrew says:

    Augusto: no, i don't see anything with transparent mode on. n o window, no clock — no recongnition that anything has happened.

    After being very sceptical initially, JavaFX is sort of growing on me. the JavaFXPad application is very nice, isn't it. It's almost a full IDE in its own right. I really like how it runs the application without needing a restart when you make changes.

    Andrew

  • Augusto says:

    @Andrew Collins;

    Thanks for that link, yes it would look much nicer as vector graphics. It's easier for me to do Photoshop, but I just downloaded Inkscape and I'm planning on doing a full SVG version next.

    The one thing I'm trying to avoid is putting a lot of code in the script to draw the elements. I like having those as separate files, so you can change the look of the clock very easily.

    For resized images BTW, if you blur them a bit they do look good. But again, vector graphics would be ideal here.

    @Andrew

    If it has problems loading a library it just quits, are you running a 64 bit version of XP by any chance? I need to find a way to popup an error in case the JNA stuff fails to load …

  • Andrew says:

    @augusto,

    no, i'm running ubuntu linux (dapper drake) with Gnome. I presume it isn't loading the library, but I get no output.

    Andrew

  • Augusto says:

    @Andrew

    Do you mind trying this example on your Linux machine?
    http://rabbit-hole.blogspot.com/2007/05/easier-al...

    It's using the same library, if it works, there might be something wrong with my JNLP setup.

  • Andrew says:

    @augusto,

    tried the easier-alpha-masks demo — doesn't work either…

    wonder if something is wrong on my machine… haven't had any troubles before…

    Andrew

  • Stephan says:

    Nice job, keep up the good work !

  • Henrik says:

    Nice demo! – but the transparent clock uses 20% CPU and 70Mb Ram .. I think that's a wee bit heavy… :-/

  • rvdkuil says:

    @augusto,

    I am running dapper as well and no luck running the demo either. I tried the demo of the jna website and there it complains about a missing glibc component. If someone knows how to fix this, it is appreciated if a fix can be posted.

    Thanks,

    Roy

  • Joe says:

    Still no luck with xp – no error messages or anything. Do you know how I could diagnose the errors?

  • Augusto says:

    @Roy/rvdkuil and others running Linux.

    I installed Ubuntu yesterday, and there is a problem linking with the X libs. Seems Ubuntu doesn't create a symlink to the latest Xlibs and the JNA implementation can't find the libraries. I got word that there has been some work done to fix this, I'll try it this weekend.

    @Joe, what you can do is download the jars (look at the JNLP) and run it in standalone mode. The JNLP has all the arguments you need. The only thing I can think of in your case, is that maybe you are using an older VM ? (try it with 1.6 if you can).

    @Henrik

    Yes, performance is an issue. I did some work last night to reduce CPU usage, the little animation to "spring" back the seconds hand seems to be a bit too CPU intensive for my tastes. One thing you can do is in JFXPad, try changing some of those parameters.

    Keep in mind though that JFX is really in it's early stages. The scripts are not compiled they're interpreted. So it's an interpreter on top of a VM (another form of interpreter). The plan seems to be to have compiled JFX scripts. Also, I haven't tried optimize graphics performance.

    I'm planning on doing a version with mixed bitmap and vector graphics, or a full vector graphics version in the future. So I'm interested in seeing how the performance scales.

  • Jeff says:

    I tweaked the code a bit, and reduced the CPU usage down to almost nothing. I made a post about it, which you can see at http://www.coffeejolts.com/2007/05/improved_javaf...

  • Augusto says:

    Cool.

    Keep in mind though that you are missing the animation that is eating up the CPU, which is the seconds hand springing back a bit. If you remove that one, from the original code, it also reduces the CPU significantly.

  • Sergio says:

    I tried on Mac OS X and it works very well, including transparency. Only the CPU usage is an issue 9as noted before).

    Congratulations for the demo!

  • knappyhead says:

    So I noticed that the performance is a little slow. Also I noted that the second hand sweep goes backwards after switching to the next second. But pretty cool prototype.

  • Kishore says:

    Great Work, It works great on my XP.

    Java Seriously needs Transparency support at base Container level.

  • Augusto says:

    I think an AWT based API for transparency is slated for Java 7, at least I remember hearing about that.

  • Not worked here, with Ubuntu 7.10, Gnome.

  • Marcos de Sousa says:

    Could you help me call "GetUserNameA" from "Advapi32.dll" using JNA?

    Function getUserName Lib "advapi32.dll" Alias "GetUserNameA" _

    (ByVal lpBuffer As String, ByRef nSize As Integer) As Integer

    More info: http://forums.microsoft.com/MSDN/ShowPost.aspx?Po...

    Long time ago I called it using ruby with success.

    ———————-RUBY CODE———————-

    require 'Win32API'

    def getUserName()

    begin

    nome = "" * 1024

    obj = Win32API.new("advapi32", "GetUserName", ["P", "P"], "L")

    obj.Call(nome, "")

    nome = nome.strip

    end

    end

    ——————————————————————

    You could mail me direct to sousa1981 AT yahoo DOT com DOT br

    Thanks in advance,

    Marcos de Sousa

  • [...] I showed some simple demos from Sun, and a few I created myself. At the end, we went over my JavaFX clock implementation and discussed a bit what place JFX occupies in the RIA technology space. The slides [...]

  • Bernard says:

    In Windows 2000, the opaque version works. But the transparent version does not:

    Java Web Start 1.6.0_06

    Using JRE version 1.6.0_06 Java HotSpot(TM) Client VM

    User home directory = D:Documents and SettingsAdministrator

    —————————————————-

    c: clear console window

    f: finalize objects on finalization queue

    g: garbage collect

    h: display this help message

    m: print memory usage

    o: trigger logging

    p: reload proxy configuration

    q: hide console

    r: reload policy configuration

    s: dump system and deployment properties

    t: dump thread list

    v: dump thread stack

    0-5: set trace level to

    —————————————————-

    compile thread: Thread[AWT-EventQueue-0,6,main]

    compile 3.156

    init: 2.109

    Exception in thread "AWT-EventQueue-0" java.lang.UnsatisfiedLinkError: no such function 'GetLayeredWindowAttributes' in library 'D:WINNTsystem32user32.dll'

    at com.sun.jna.Function.find(Native Method)

    at com.sun.jna.Function.(Function.java:85)

    at com.sun.jna.Library$Handler.invoke(Library.java:188)

    at $Proxy13.GetLayeredWindowAttributes(Unknown Source)

    at com.sun.jna.examples.WindowUtils$W32WindowUtils$W32RepaintTrigger.update(WindowUtils.java:597)

    at com.sun.jna.examples.WindowUtils$RepaintTrigger.paintComponent(WindowUtils.java:183)

    at javax.swing.JComponent.paint(Unknown Source)

    at javax.swing.JComponent.paintChildren(Unknown Source)

    at javax.swing.JComponent.paint(Unknown Source)

    at javax.swing.JLayeredPane.paint(Unknown Source)

    at javax.swing.JComponent.paintChildren(Unknown Source)

    at javax.swing.JComponent.paintToOffscreen(Unknown Source)

    at javax.swing.BufferStrategyPaintManager.paint(Unknown Source)

    at javax.swing.RepaintManager.paint(Unknown Source)

    at javax.swing.JComponent.paint(Unknown Source)

    at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source)

    at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source)

    at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)

    at java.awt.Container.paint(Unknown Source)

    at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)

    at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)

    at javax.swing.RepaintManager.seqPaintDirtyRegions(Unknown Source)

    at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(Unknown Source)

    at java.awt.event.InvocationEvent.dispatch(Unknown Source)

    at java.awt.EventQueue.dispatchEvent(Unknown Source)

    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)

    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)

    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)

    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)

    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)

    at java.awt.EventDispatchThread.run(Unknown Source)

  • Augusto says:

    Hum … this JFX project is pretty old I should upgrade it … however, I never tested it on Win2K. It uses a JNA wrapper, now that JDK 1.6 has official support for transparent windows I should probably update it to use that. Or you can try the JDK 1.6 support.

  • Fastmidia says:

    I really enjoyed your site and post. Congratulations

 

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

 

WP SlimStat