Dean Iverson and I have been working on an open source project called GroovyFX that provides a Groovy binding that sits on the new JavaFX 2.0 platform.  Dean has written a good blog on how to get started with GroovyFX here. It is already a little dated, but if you ignore the JavaFX build numbers and just download the latest JavaFX bits from javafx.com you should be good to go.

What I would like to do today is start a series talking about the features of GroovyFX.  Let's start with the first application.

 

The general pattern for the app code is to define the JavaFX components within a Groovy closure that is passed to the supporting class GroovyFX.

 

import groovyx.javafx.GroovyFX

import groovyx.javafx.SceneGraphBuilder

 
GroovyFX.start { primaryStage ->   

    def sg = new SceneGraphBuilder(primaryStage)
    sg.stage(title: "GroovyFX @ JavaONE", show: true) {

         scene(fill: groovyblue, width: 420, height:420) {

         }

 

 

     }   

}

For starters, the GroovyFX application is enclosed in a closure block passed to the GroovyFX start method. A Groovy Closure is like a "code block". It is a piece of code that is defined and then executed at a later point. The closure passed to the GroovyFX start method is then executed on the main JavaFX thread. The only argument passed to this closure is the primary Stage automatically created by the JavaFX platform.

One of the core principles behind JavaFX is the creation of a scene graph to depict what needs to be displayed. A scene graph is comprised of a tree graph that begins with a stage that has a scene, and the scene is comprised of all the nodes, buttons, labels, etc, that make up the scene. 

 Groovy supports a concept know as a builder design pattern for recognizing and processing arbitrary nested tree structures and already has builders for Swing, XML and Ant, to name a few. Obviously, adding scene graph support to Groovy is a no-brainer.

 The next main step is to create a SceneGraphBuilder object to support the Groovy Builder design pattern. An optional argument to the SceneGraphBuilder constructor is the primary stage passed into the GroovyFX start closure. This stores the primary stage for retrieval later when the stage node is invoked. 

The top node in JavaFX is the Stage and this is akin to a Window in the Window manager (similar to JFrame in Swing). A stage may be decorated, having the familiar exit, minimize, and full screen buttons that are common to all windows on the desktop. Or it may be undecorated with out the surrounding frame, or transparent where only the visible components on the scene are shown.You can set the stage style by passing the attribute style on the stage node.

sg.stage(title: "GroovyFX @ JavaONE", visible: true, style: "transparent") {

 

If you wish to create a new stage instead of reusing the primary stage, just pass the attribute value, "primary: false"  in the stage definition. 

sg.stage(title: "GroovyFX @ JavaONE", show: true, primary: false) {

There is another pseudo attribute, centerOnScreen, that when set causes the stage to be shown at the center of the desktop.

sg.stage(title: "GroovyFX @ JavaONE", show: true, centerOnScreen: true) {

 

The other attributes, like title and fullScreen  are common to the properties defined on the javafx.stage.Stage class.

 

The next action is to create a scene on the stage. 

 

      sg.stage(title: "GroovyFX @ JavaONE", show: true) {

         scene(fill: groovyblue, width: 420, height:420) {

         } 

     } 

Notice that the scene is a nested node to the stage. Thusly, the scene will be installed on the Stage object. Also, you may notice the fill attribute takes an identifier argument groovyblue, that represents a JavaFX Color object. GroovyFX supports the common names for the colors as defined in the javafx.scene.paint.Color object (as well as the groovyblue we added). Other options for color are the web hex format within quotes, '#3e3e3e" and an rgb() and hsb() function like calls.There is also support for describing linear and radial gradients, and I will cover those in more detail in another posting.

 

The other attributes to the scene node in SceneGraphBuilder are common to the properties defined in the javafx.scene.Scene class.

 

So far we have created an empty window with the title "GroovyFX @ JavaONE". Let's add something to it.

 

import groovyx.javafx.GroovyFX

import groovyx.javafx.SceneGraphBuilder

 
GroovyFX.start { primaryStage ->   

    def sg = new SceneGraphBuilder(primaryStage)
    sg.stage(title: "GroovyFX @ JavaONE", show: true) {

         scene(fill: groovyblue, width: 420, height:420) {

label("GroovyFX: JavaFX is my Bag, Baby, Yeah!",

layoutX: 10, layoutY: 180, font: 24, textFill: navy,

                        prefWidth: 350, wrapText: true)

         }

     }   

}

This creates a label that is positioned at coordinate 10x180 in the window.

While this is a simple example, I hope you now understand the basics of the GroovyFX scene graph.

If you are attending JavaONE come and see Dean's and my session on GroovyFX Thursday, 03:30 PM, Hotel Nikko - Monterey I/II. Also stay tuned to this blog for more on GroovyFX.




Views: 2378

Comment

You need to be a member of Codetown to add comments!

Join Codetown

Happy 10th year, JCertif!

Notes

Welcome to Codetown!

Codetown is a social network. It's got blogs, forums, groups, personal pages and more! You might think of Codetown as a funky camper van with lots of compartments for your stuff and a great multimedia system, too! Best of all, Codetown has room for all of your friends.

When you create a profile for yourself you get a personal page automatically. That's where you can be creative and do your own thing. People who want to get to know you will click on your name or picture and…
Continue

Created by Michael Levin Dec 18, 2008 at 6:56pm. Last updated by Michael Levin May 4, 2018.

Looking for Jobs or Staff?

Check out the Codetown Jobs group.

 

Enjoy the site? Support Codetown with your donation.



InfoQ Reading List

Presentation: Adaptable Innovation: How Microsoft Leverages React Native for Strategic Advantage

Lorenzo Sciandra unveils the use of React Native across Microsoft's diverse ecosystem, emphasizing the strategic and technological reasons for its continued selection.

By Lorenzo Sciandra

How to Delight Your Developers with User-Centric Platforms and Practices

By focusing on the users, platform development teams can ensure that they build a platform that tackles the true needs of developers, Ana Petkovska said at QCon London. In her talk, Delight Your Developers with User-Centric Platforms & Practices, she shared what their Developer Experience (DevEx) group looks like and what products and services they provide.

By Ben Linders

Article: Reactive Real-Time Notifications with SSE, Spring Boot, and Redis Pub/Sub

Explore the power of reactive programming for building scalable real-time notification systems. Using Spring Boot Reactive and Spring WebFlux, leverage non-blocking operations to handle high-volume, asynchronous data flows efficiently. Discover how Redis Pub/Sub enables event-driven messaging and how the SSE protocol provides persistent connections for instant client updates without polling.

By Matteo Rossi

ASP.NET Core 9: Enhancements in Static Asset Handling, Blazor, SignalR, and OpenAPI Support

Microsoft has released .NET 9, which contains features regarding ASP.NET Core 9. This latest release focuses on optimizing static asset handling, refining Blazor's component interaction, enhancing SignalR's observability and performance, and streamlining API documentation through built-in OpenAPI support.

By Robert Krzaczyński

QCon SF: Mandy Gu on Using Generative AI for Productivity at Wealthsimple

Mandy Gu spoke at QCon SF 2024 about how Wealthsimple, a Canadian fintech company, uses Generative AI to improve productivity. Her talk focused on the development and evolution of their GenAI tool suite and how Wealthsimple crossed the "Trough of Disillusionment" to achieve productivity.

By Anthony Alford

© 2024   Created by Michael Levin.   Powered by

Badges  |  Report an Issue  |  Terms of Service