Friday, August 28, 2009

Groovy and Processing.org!


Processing.org is a cool little thing; you can't look at two or three of the demos on their site before you realize it's really neat.  Not to mention it's designed as a compliment to Wiring, the language used to program the Arduino and a number of other DIY hardware platforms. 

So suffice to say, Processing is a compelling alternative to Flash for at least a handful of usage scenarios.  So what if you're a Java/ Groovy programmer who wants access to Processing's coolness, without using their cute IDE?  We're Java programmers after all; if I can't use Eclipse and integrate it with Hibernate and Spring -- well, that would be overkill.  But let's just get to the point: you can use Processing.org straight from your Java environment.  In fact, here's their JavaDoc.


But, the Processing language is actually kinda nice!  And if you look at all of those functions that Processing provides, they're all in the PApplet class.  Well that's a pain in the ass.  Never fear -- that's where Groovy steps in.  You can use Groovy's language features to Make Processing's API at least as easy to use, and you get all of the cool features of the Groovy language as well!

Enough rambling -- here's an example:

import processing.core.*

/**
 * A group of dots appear and then shrink in size.
 * This sketch uses the Groovy List and uses a second class SpriteEllipse.
 * This main extends PApplet so it can create the window.
 */
class CirclesDemo extends PApplet {

    // setup vars
    def sprites = []
    def renderer = JAVA2D // P2D

    // state
    int nCount = -1
    def animating = true
    int clickX, clickY = 0
    
    void setup() {
        ellipseMode CENTER
        size 400, 400, renderer
        frameRate 20
        smooth()
        textFont loadFont("TrebuchetMS-20.vlw"), 14
    }
    
    void draw() {
        background 120
        
        nCount++
        nCount %= 90  // add additional sprites every so often:
        if ( ! nCount ) (1..20).each {
            sprites << new SpriteEllipse( this ) 
        }

        def clicked
        sprites.each { s ->
            s.update()
            if ( s.dead ) s.init()
            else if ( clickX && s.isOver( clickX, clickY ) ) clicked = s
            s.render()
        }
        if ( clicked ) sprites.remove clicked
        clickX = 0
        
        fill 255 // set text color
        text sprites.size(), 5, height-5 // update sprite count display
    }
    
    void keyPressed() {
        if ( key == ' ' && animating ) { noLoop(); animating = false }
        else { loop(); animating = true }
    }
    
    void mouseClicked() { // get mouse click pos for next draw() call.
        this.clickX = mouseX
        this.clickY = mouseY
//        println " $clickX $clickY"
    }
    
    static void main(args) {
        PApplet.main( [ "CirclesDemo" ] as String[] );
    }    
}


Now the 'SpriteEllipse' class really could just be rolled up into the above
class -- since its functionality all comes from the PApplet anyway.  But in Groovy
you can easily separate the functionality into a separate class and use Groovy's @Delegate to make it appear that SpriteEllipse extends PApplet.


import processing.core.PApplet

class SpriteEllipse {
    private Float x = 0
    private Float y = 0
    private Float rad = 75
    private Integer color = 20
    private @Delegate PApplet pApplet
    
    SpriteEllipse(PApplet pApplet) {
        this.pApplet = pApplet // must be set first since it is delegate
        this.init()
    }
    
    void init() {    /* Initialize fields to random vals */
        this.x = random( 0, width )
        this.y = random( 0, height )
        this.rad = random( (int)( height*0.05 ), (int)( height*0.225 ) )
        this.color = random( 0, 255 )        
    }
    
    void update() { if ( this.rad ) rad-- }
    
    void render() {
        fill color
        ellipse x, y, rad, rad
    }

    boolean isOver(int mx, int my) {
        (mx-x)*(mx-x) + (my-y)*(my-y) < rad*rad;
    }
    
    boolean isDead() { return rad < 1 }
}


The full code is up on GitHub.  This is a modified version of this guy's work, so he deserves most of the credit. Actually now that I look back at his example, about the only thing I did was make use of the @Delegate feature :)


No comments:

Post a Comment