home | 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',
        size:[w, h],
    // 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:

new blog posts


Copyright 1998-2016 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.