GraphicsBuilder: Runtime Behaviors - No Fluff Just Stuff

GraphicsBuilder: Runtime Behaviors

Posted by: Andres Almiray on March 12, 2008

If you have followed GraphicsBuilder's examples shown at this blog you have probably noticed that GraphicsBuilder has a lot of similarities with SwingBuilder, both create a hierarchical structure of nodes used to render artifacts to the screen, but GraphicsBuilder's operations are not rendered directly once built, it is the responsibility of a JComponent that understands these operations to call them in the context of a paint operation, as it is the case of GraphicsPanel. You can say that GraphicsBuilder has a design nature (the hierarchical structure of nodes) and the JComponent takes care of the runtime nature.

Because of these two natures you can't reach the graphics context while building (as it doesn't exist yet) and once you draw the artifacts on the screen is too late. This is the reason why every GraphicsOperation now has a pair of properties beforeRender/afterRender that take a closure as value, the runtime nature of the operations will assert that they are called at the proper time.

So now that we have access to the graphics context, what can we do with it? the following example shows a rectangle (presumably a background rectangle) that will always cover the full extent of the JComponent as it keeps in sync its width/height property with the JComponent's dimension

There is no ComponentListener registered to keep the sizes in sync (though you could register one inside beforeRender) so in order to see the effect, you can resize the painting panel and click on the rectangle, that will trigger a property change which in turn will trigger a repaint on the panel, drawing the rectangle with updated dimensions. It is worth noting that even though you can change an observable property that may affect the visuals of the operation using this new option, PropertyChangeEvents won't be triggered while the closures are executing, otherwise an infinite loop would happen.

Granted this is a cheesy way to have an operation react to its containing component, I'm looking at a better option to lazily bind this behavior in a simpler way and not using the existing bind() node (which is also an option but requires that the panel/component be created before the graphics operation).

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 »