alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Groovy example source code file (paintingByNumbers.groovy)

This example Groovy source code file (paintingByNumbers.groovy) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Groovy tags/keywords

gui, swing

The Groovy paintingByNumbers.groovy source code

/** 
 * Simple patchwork graphics demo
 * @author: Jeremy Rayner, changes by Dierk Koenig
 */
import javax.swing.WindowConstants as WC
import groovy.swing.SwingBuilder

def width = 500, height = 400, blockSize = 10
def g = createGraphics(width, height)

// main loop
while (true) {
    drawBlock(width, height, blockSize, g)
}

// random integer
def rnd(upperBound){
    (int)(Math.random() * upperBound)
}

// draw a random coloured square within bounds
def drawBlock(w, h, b, g) {
    def row    = rnd(h / b)
    def column = rnd(w / b)
    def colour = new java.awt.Color(rnd(255),rnd(255),rnd(255))
    g.color = colour
    g.fillRect(column * b, row * b, b, b)
}

// create a new frame and clear screen
def createGraphics(w, h) {
    def frame = new SwingBuilder().frame(
        title:'Painting by numbers',
        location:[20,20],
        size:[w, h],
        defaultCloseOperation:WC.EXIT_ON_CLOSE
    )
    frame.show()
              
    // obtain graphics context
    def gfx = frame.graphics
              
    // clear screen
    gfx.color = java.awt.Color.BLACK
    gfx.fillRect(0, 0, w, h)

    return gfx
}

Other Groovy examples (source code examples)

Here is a short list of links related to this Groovy paintingByNumbers.groovy source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.