Painting gradients with GraphicsBuilder - No Fluff Just Stuff

Painting gradients with GraphicsBuilder

Posted by: Andres Almiray on December 18, 2007

Gradients are cool, they will surely make your app look better, but drawing them may sometimes spell trouble. Good news is that gradient support in GraphicsBuilder is back, exploiting GradientPaint as a start. the jdk6 extension will be resurrected to provide support for LinearGradient & RadialGradient.

Let's see a simple example of gradients in action, the following image displays 4 gradients




Here is the snippet of graphics operation that produced that image

As with other graphics operations available in GraphicsBuilder gradientPaint() provides sensible defaults for its properties in case you were wondering where the colors come from. You may have also noticed that the gradient's bounds are smaller than the actual shape bounds, still the gradient fills the whole shape; also the starting point of the gradient (which is the color black) matches one corner though no transformations is visible. This happens because gradientPaint() has been adjusted to the shape's bounds with the default setting of fit=true meaning that the gradient will be scaled (retaining its original bounds ratio). There is another setting stretch=true that will scale the gradient to the shape's bound, discarding the original ratio. Gradients can also have cycles if cyclic=true is specified. As you may have guessed these 3 settings exclude one another, if one is true the others will be set to false. Let's see those settings at work, while at the same time taking advantage of the fact that any paint may be used as a parameter for the fill property on any shape operation.

This is the base code
These are the outputs with different settings for fit, stretch and cyclic
fit = true
fit = false
stretch = true
cyclic = true

I expect to have full gradient support in the next version of GraphicsBuilder (0.4.2), stay tunned for more GraphicsBuilder news.

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 »