Skip to content

A Little Griffon Tip

I just want to ensure that I remember this tip. How better to do it than to post it here…

import java.awt.Graphics
import groovy.swing.SwingBuilder
import javax.swing.JPanel
import java.awt.Color

class CanvasPanel extends JPanel {
   Closure draw

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

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

Here’s the result:

I know it is extremely simple, but it’s the sort of thing that can be very useful and is also just the sort of thing that slips one’s mind!

Thanks to Andres Almiray!

Tags: ,

Java Enterprise Edition, JEE, JavaServer Pages, JSP, Tag Libraries, Servlets, Enterprise Java Beans, EJB, Java Messaging Service JMS, BEA Weblogic, JBoss, Application Servers, Spring Framework, Groovy, Grails, Griffon, Seam, Open Source, Service Oriented Architectures, SOA, Java 2 Standard Edition, J2SE