Upload
andres-almiray
View
1.359
Download
3
Embed Size (px)
DESCRIPTION
Java2D and Groovy, a Perfect Match
Citation preview
@ Andres Almiray 2008 Silicon Valley Code Camp 2008
Java2D and Groovy, a Perfect Match
Andres Almiray
@ Andres Almiray 2008 Silicon Valley Code Camp 2008
Agenda
What is Groovy? SwingBuilder GraphicsBuilder Putting it all together What’s next?
@ Andres Almiray 2008 Silicon Valley Code Camp 2008
What is Groovy?
@ Andres Almiray 2008 Silicon Valley Code Camp 2008
SwingBuilder
@ Andres Almiray 2008 Silicon Valley Code Camp 2008
Builder Basics
Builders are a form of a DSL Focused on Hierarchical Structures SwingBuilder is not the only Builder in Groovy
XMLBuilder AntBuilder ObjectGraphBuilder Still Others…
Swing applications follow a hierarchical model: Window Panel Button
Builders are a perfect fit for making Swing UIs
@ Andres Almiray 2008 Silicon Valley Code Camp 2008
A Quick HelloWorld Example
groovy.swing.SwingBuilder.build { frame( id: 'root', title:'Hello World', show:true, pack:true) { flowLayout() label('Enter a message:') textField(id:'name', columns:20) button('Click', actionPerformed: { ActionEvent e -> if (name.text) optionPane().showMessageDialog( root, name.text) }) }}
@ Andres Almiray 2008 Silicon Valley Code Camp 2008
What's going on?
Each node is syntactically a method call All of these method calls are dynamically
dispatched Most don’t actually exist in bytecode
Child closures create hierarchical relations Child widgets are added to parent containers
SwingBuilder node names are derived from Swing classes Remove the leading ‘J’ from a Swing class when present
SwingBuilder also supports some AWT classes like layouts
@ Andres Almiray 2008 Silicon Valley Code Camp 2008
GraphicsBuilder
@ Andres Almiray 2008 Silicon Valley Code Camp 2008
GraphicsBuilder - Basics
GraphicsBuilder is to Java2D what SwingBuilder is to Swing
It produces a hierarchy of GraphicsOperations (or nodes)
A specialized component is required to render graphicsOperations and handle input events: GraphicsPanel
@ Andres Almiray 2008 Silicon Valley Code Camp 2008
GraphicsBuilder - Features
shapes - shape, outline, areas regular Java2D shapes from java.wt.geom additional shapes from jSilhouette
paints - solid, gradients, composite strokes - from jhlabs filters - from swingx, jhlabs grouping, transfortmations , images svg - import/export via Batik swf - partial export (still experimental)
@ Andres Almiray 2008 Silicon Valley Code Camp 2008
GraphicsPad
@ Andres Almiray 2008 Silicon Valley Code Camp 2008
Putting it all together
@ Andres Almiray 2008 Silicon Valley Code Camp 2008
Mixing Swing & Graphics
Use SwingBuilder to build the UI Use GraphicsBuilder to draw anywhere you need
in a GraphicsPanel in JLayeredPane in SwingX layered components in Jide’s overlay support
@ Andres Almiray 2008 Silicon Valley Code Camp 2008
Tips & Hints
Use SwingBuilder.build() Use Expando for quick prototyping of beans Use ObservableMap for quick binding tests Reuse shapes as must as you can Group operations that may have the same
settings applied (color, borderWidth, fill, transformations)
@ Andres Almiray 2008 Silicon Valley Code Camp 2008
What’s next?
@ Andres Almiray 2008 Silicon Valley Code Camp 2008
Tips & Hints
Custom nodes Custom painting of Swing components Animation support
@ Andres Almiray 2008 Silicon Valley Code Camp 2008
Resources
http://groovy.codehaus.org
http://groovy.codehaus.org/Swing+Builder http://groovy.codehaus.org/SwingXBuilder http://groovy.codehaus.org/JideBuilder http://groovy.codehaus.org/GraphicsBuilder http://code.google.com/p/jsilhouette
http://jroller.com/aalmiray http://twitter.com/aalmiray
@ Andres Almiray 2008 Silicon Valley Code Camp 2008
Q & A
@ Andres Almiray 2008 Silicon Valley Code Camp 2008
Thank you!