19
@ Andres Almiray 2008 Silicon Valley Code Camp 2008 Java2D and Groovy, a Perfect Match Andres Almiray

Svcc Java2D And Groovy

Embed Size (px)

DESCRIPTION

Java2D and Groovy, a Perfect Match

Citation preview

Page 1: Svcc Java2D And Groovy

@ Andres Almiray 2008 Silicon Valley Code Camp 2008

Java2D and Groovy, a Perfect Match

Andres Almiray

Page 2: Svcc Java2D And Groovy

@ Andres Almiray 2008 Silicon Valley Code Camp 2008

Agenda

What is Groovy? SwingBuilder GraphicsBuilder Putting it all together What’s next?

Page 3: Svcc Java2D And Groovy

@ Andres Almiray 2008 Silicon Valley Code Camp 2008

What is Groovy?

Page 4: Svcc Java2D And Groovy

@ Andres Almiray 2008 Silicon Valley Code Camp 2008

SwingBuilder

Page 5: Svcc Java2D And Groovy

@ 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

Page 6: Svcc Java2D And Groovy

@ 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) }) }}

Page 7: Svcc Java2D And Groovy

@ 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

Page 8: Svcc Java2D And Groovy

@ Andres Almiray 2008 Silicon Valley Code Camp 2008

GraphicsBuilder

Page 9: Svcc Java2D And Groovy

@ 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

Page 10: Svcc Java2D And Groovy

@ 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)

Page 11: Svcc Java2D And Groovy

@ Andres Almiray 2008 Silicon Valley Code Camp 2008

GraphicsPad

Page 12: Svcc Java2D And Groovy

@ Andres Almiray 2008 Silicon Valley Code Camp 2008

Putting it all together

Page 13: Svcc Java2D And Groovy

@ 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

Page 14: Svcc Java2D And Groovy

@ 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)

Page 15: Svcc Java2D And Groovy

@ Andres Almiray 2008 Silicon Valley Code Camp 2008

What’s next?

Page 16: Svcc Java2D And Groovy

@ Andres Almiray 2008 Silicon Valley Code Camp 2008

Tips & Hints

Custom nodes Custom painting of Swing components Animation support

Page 17: Svcc Java2D And Groovy

@ 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

Page 18: Svcc Java2D And Groovy

@ Andres Almiray 2008 Silicon Valley Code Camp 2008

Q & A

Page 19: Svcc Java2D And Groovy

@ Andres Almiray 2008 Silicon Valley Code Camp 2008

Thank you!