GraphicsBuilder: drawing custom shapes - No Fluff Just Stuff

GraphicsBuilder: drawing custom shapes

Posted by: Andres Almiray on December 25, 2007

GraphicsBuilder supports the standard Java2D shapes plus two more from Swingx but what if you wanted to draw a custom shape? say for example a donut as shown on chapter 3 of FRC



You can definitely take advantage of the area operations GraphicsBuilder already provides but you can also create a shape operation and register it with a GraphicsBuilder instance, as a matter of fact, that is how the previous image was created, here is the relevant code for setting the builder

Notice that donuts are registered through a factory, thanks to the facilities provided by FactoryBuilderSupport and extended by GraphicsBuilder itself. So how is the Donut shape made then? a shape operation must implement ShapeProvider, Transformable, take care of filling and drawing the shape, fire events whenever any of its properties changes value, etc. Fortunately you don't have to start from scratch, there is a convenient AbstractShapeGraphicsOperation class that does the heavy lifting for you, leaving the job of actually creating the shape to your class. Because the donut is computed as an area operation and we do not want to compute that area again and again we are going to cache the resulting shape, but allow for it to be recomputed if any of the donut's properties changes. Here is the Donut class
Alright, lets step into the code
  • Donut extends AbstractShapeGraphicsOperation, already explained
  • the static required property signals the superclass for special treatment of any property that matches those names, specially when firing events.
  • the shape field will hold the cached donut.
  • now we see the actual properties, defined with def as to make it easier for binding, and with some default values in case some of them are not defined at runtime.
  • the constructor defines the name of the new operation, useful for debugging.
  • getShape is the single method Donut must implement to provide the shape, here you can see that the cached shape is used if not null, computed otherwise and returned.
  • here is an interesting bit, each shape registers itself as PropertyChangeListener, so if it receives a PropertyChangeEvent the cached shape is set to null.
  • what its left is to create the actual donut shape, done with a subtract area operation on two circles.
And that's it! now donuts can be used anywhere a shape operation applies, for example as a basis for textures or as a clip, or as an input for any area operation.

Groovy Holidays!
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 »