GfxBuilder update: custom nodes - No Fluff Just Stuff

GfxBuilder update: custom nodes

Posted by: Andres Almiray on July 5, 2009

A quick update of what is now available on GfxBuilder:ut I'm very excited about the availability of custom nodes because making them in the past was nigh impossible, thanks in part to AST transformations that no longer is true. Let's revisit the Sphere multi-paint example, if you recall the code from than sample (follow the paint tutorial for a quick memory refreshment) you will notice that a sphere can be drawn using a circle and 3 gradients, well here is the updated version using custom nodes, first an image is worth a thousand words



Now for the code itself
The SphereNode exposes 7 attributes to the outside world using @GfxAttribute. This particular annotation is @Bindable's long lost sibling, it not only performs the same task @Bindable does (making a property an observable one) but it also is able to write an aliased property, this means you can either set a value for the radius: property or r: property (shortcuts!), it doesn't matter as the backing field will be updated accordingly. The node sets some default values for those attributes, those values match the settings for the blue sphere. Moving on to the actual drawing code of the node, it is pretty much the same one found in the painting how-to plus a few tweaks to have all gradients be relative to the node's location. Notice that the radial gradients are now aware of its host's dimensions, this is why setting values for cx: and cy: is now optional.

Drawing 4 spheres is just a matter of setting the appropriate coordinates (cx,cy) and tweaking some of the colors, otherwise we will have a drawing of 4 blue spheres in the same place! There are two ways for adding a custom node to an scene, by defining the node's class as the value of customNode() or passing an instance of a subclass of CustomNode to customNode(), in the end there is not much difference, the builder makes sure your nodes have the proper attribute values set before they are rendered.

Keep on Groovying!
Andres Almiray

About Andres Almiray

Andres is a Java/Groovy developer and a Java Champion with more than 20 years of experience in software design and development. He has been involved in web and desktop application development since the early days of Java. Andres is a true believer in open source and has participated on popular projects like Groovy, Griffon, and DbUnit, as well as starting his own projects (Json-lib, EZMorph, GraphicsBuilder, JideBuilder). Founding member of the Griffon framework and Hackergarten community event. https://ch.linkedin.com/in/aalmiray

Why Attend the NFJS Tour?

  • » Cutting-Edge Technologies
  • » Agile Practices
  • » Peer Exchange

Current Topics:

  • Languages on the JVM: Scala, Groovy, Clojure
  • Enterprise Java
  • Core Java, Java 8
  • Agility
  • Testing: Geb, Spock, Easyb
  • REST
  • NoSQL: MongoDB, Cassandra
  • Hadoop
  • Spring 4
  • Cloud
  • Automation Tools: Gradle, Git, Jenkins, Sonar
  • HTML5, CSS3, AngularJS, jQuery, Usability
  • Mobile Apps - iPhone and Android
  • More...
Learn More »