GraphicsBuilder: Dragging revisited - No Fluff Just Stuff

GraphicsBuilder: Dragging revisited

Posted by: Andres Almiray on March 14, 2008

Joshy has posted Doodle #3, a basic yet interesting make-your-character demo using JavaFX. The interesting part being that the demo displays draggable images, I'm so happy I revamped event handling recently, but before getting to work with Groodle #3 I'd like to present a couple of new things you can do with GraphicsBuilder. Suppose you have the following Gfx demo running



there are 3 "shapes", actually the green square pair is a group, here is the relevant code

All "shapes" have the save event handler, you are probably wondering if it is the same as the one showed previously here, which looks like

But sadly it will only work for the rect node as the other two do not have a set of [x,y] properties (as a matter of fact an exception will be thrown), so what can we do to solve this? Luckily these operations support transformations so we may attach a translate transformation to them. Ok, but setting [x,y] is much simpler no? and surely it would be the same case if we could access the centre's coordinates of the star, right? Lastly, there is a gotcha using translate, how do we find out the correct offsets to set the relative [x,y] ?

Fear not because this is exactly what this post is about, all of these problems can be solved one at a time. All "displayable" operations (shapes, outlines, images and groups) share a common ancestor that provides a new set of methods useful in this situation
  • hasCenter() - returns true if the operation has [cx,cy] properties
  • hasXY() - returns true if the operation has [x,y] properties
  • bounds - returns the bounding rectangle, this value is calculated after the operation has been executed, so you can't access it with a beforeRender handler (it will be null).
Ok, those methods will help with the first two shapes (square and star), what about the group? let's look at the code and find out, shall we?

The trick is storing the [x,y] coordinates of the shape's bounds the first time the event is processed, as they will be updated during the next repaint, making a mess if you use them as is. In order to solve this problem operations (and filters) now have a built-in extra set of properties (the props property in shape) that work pretty much as client properties do for Swing components, they are even observable too thanks to ObservableMap, so don't forget to register a PropertyChangeListener with props if you want to receive notifications when a key is updated. Note how named nodes are also used to query for a translation node, either to create a new one or update its settings.

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 »