GfxBuilder update 2: transforms & plugins - No Fluff Just Stuff

GfxBuilder update 2: transforms & plugins

Posted by: Andres Almiray on July 11, 2009

Here goes another update on GfxBuilder. If you recall the observations made at the end of "a Coffee Cup with Groovy", one thing that bothered me is that applying transforms to a node turned out to be quite messy, specially when scaling was involved, rectifying this situation was one of the goals when rebooting GfxBuilder. I'm happy to say the matter is solved given that you can now apply local transforms to a node now, these transforms are applied relative to the node's center always, also in a very specific order [rotate, translate, scale]. However there is also the possibility to apply arbitrary transforms, these are not tied to the node's center but rather to the current coordinate system, let's look at an example, the following picture demonstrates how local transforms affect some squares



The top left square is our control, no transforms are applied to it. The top right square is rotated 45 degrees around its center, you'll notice that the rotation angle needs to be defined only. The bottom left square has a scale transforms applied over the X axis, notice that it continues to be centered along the X axis at x=100, same as the control square. The last square has all 3 kinds of transforms applied to it, if you're careful you'll see that its center is displaced 20 units across the X axis when compared to the top right square, it is also aligned in the Y axis to the bottom left, meaning that neither rotation nor scaling affected it in an unexpected way. Here is the code that shows how the squares were setup
The good thing about local transforms is that they will work on images, groups and even custom nodes too, as shown by the following pic



I'm reusing the sphere custom node shown on a previous post with a new addition: a bullseye at its center. A few local transforms are applied to the red, green and yellow spheres, the blue serves as control, lastly a red square is drawn over small red circles that are pinned to the center of each sphere before they were affected by their respective transforms, showing that only the yellow one is off center because it is the one that has translation transforms applied, here is the code
While local transforms are much better that what it was previously available there are still a few kinks, the keen observer would have noticed that even though the squares in the first picture are rotated 45 degrees their respective paint is not! local transforms affect the node's shape but not how it is filled/painted while regular transforms affect how a node is painted, because those transforms affect the current graphics context not just the shape.

Another breakthrough in the current development version are plugins, previously known as extensions. Loading an extension in the old way was bothersome and rather inextensible (even though they were named extensions, silly? I know!), GfxBuilder now relies in Jdk6's ServiceLoader facility to load its plugins. In other words, GfxBuilder requires Jdk6 as its minimum Java version. Yup, you read that right, if you really want to have great Java2D/Swing performance with Java you need Jdk6, I'm sorry about those on OSX that can't upgrade, maybe SoyLatte or OpenJDK7 can help you. The currently supported plugins are:
  • swingx - adds the morph shape. Use it to morph any two shapes.
  • svg - adds the xpath shape, an extended version of path that supports arcTo as a path segment. It also adds an SVGRenderer, meaning you can save your nodes as SVG. An SVG to GFX converter will be provided as well.
GfxBuilder is almost on par in terms of features offered by its previous incarnation, besides a few missing nodes (draw, shape, clip, paint, composite) the other big feature missing right now are filters. Feedback is always appreciated.

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 »