atom feed3 messages in org.codehaus.groovy.userRe: [groovy-user] SwingBuilder backgr...
FromSent OnAttachments
Gregg BolingerMay 20, 2009 11:02 am 
Danno FerrinMay 20, 2009 11:14 am 
Andres AlmirayMay 21, 2009 11:54 pm 
Subject:Re: [groovy-user] SwingBuilder background image
From:Andres Almiray (aalm@yahoo.com)
Date:May 21, 2009 11:54:50 pm
List:org.codehaus.groovy.user

Here is a sample JPanel that accepts a closure to define how it should be painted

-------------- import java.awt.Graphics import javax.swing.JPanel

class CanvasPanel extends JPanel { Closure draw

protected void paintComponent(Graphics g) { super.paintComponent(g) if(draw) draw(this,g) } }

------

its usage is as simple as

new SwingBuilder().edt { frame( title: "RED", size: [200,200], visible: true ) { panel( new CanvasPanel(draw: { p, g -> g.color = Color.RED g.fillRect 0, 0, p.width, p.height })) } }

as Danno says you can use container() or widget() withe the caveat that widget() does not accept nesting children.

Theoretically you could also use GraphicsBuilder to achieve the same goal, but as I haven't actually tested I would recommend you stick with groovyfied Java2D code for the time being.

Gregg Bolinger-8 wrote:

In plain jane swing, to apply a background image to a panel I'd override the paintComponent method and render my image. Is there a shortcut way of doing this with SwingBuilder? Something like: panel(backgroundImage:imageIcon('image.png').image) ?? Or something similar?

Thanks