どう書く? org の2次元ランダムウォークの課題回答

どう書く? orgの課題回答です。Groovyを使用しています。

2次元ランダムウォーク
の課題の回答です。どう書く? orgには画像が投稿できないので、画面イメージはこちらに置きます。

コードはこちら。

import groovy.swing.SwingBuilder
import java.awt.Color
import java.awt.BorderLayout as BL
import javax.swing.WindowConstants as WC
import javax.swing.BorderFactory as BF
import javax.swing.JOptionPane
swing = new SwingBuilder()
paint = swing.action(
	name: 'Run',
	closure: this.&paintGraph,
	mnemonic: 'R',
	accelerator: 'ctrl R'
)
about = swing.action(
	name: 'About',
	closure: this.&showAbout,
	mnemonic: 'A',
	accelerator: 'F1'
)
frame = swing.frame(title:'2次元ランダムウォーク',
location:[100,100], size:[300,300],
defaultCloseOperation:WC.EXIT_ON_CLOSE) {
	menuBar (){
		menu(mnemonic:'A','Action'){
		menuItem(action:paint)
		}
		glue()
		menu(mnemonic:'H','Help'){
			menuItem(action:about)
		}
	}
	panel (border:BF.createEmptyBorder(6,6,6,6)) {
		borderLayout()
	
		vbox(constraints: BL.CENTER,border:BF.createTitledBorder('Runボタンを押すたびに100回動きます')) {
			panel(id:'canvas')
		}

		hbox (constraints: BL.SOUTH){
			hstrut(width:10)
			button(action:paint)
		}
	}
}

frame.show()

def labeledSpinner(label, value){
	swing.label(label)
	swing.hstrut()
	swing.spinner(id:label, stateChanged:this.&paintGraph,
	model:swing.spinnerNumberModel(value:value))
}
	gfx = swing.canvas.graphics
	gfx.color = new Color(255, 255, 150)
	gfx.color = Color.red
	xpos = 100
	ypos = 100
def paintGraph(event) {

	int width = swing.canvas.size.width
	int height = swing.canvas.size.height
	
	1.upto(100) {
		xdir = Math.random()
		ydir = Math.random()
		if (xdir <0.3333 &&  xpos >5)         { xpos-=5 }
		if (xdir >0.6666 && (xpos < width-5)) { xpos+=5 }
		if (ydir <0.3333 &&  ypos >5)         { ypos-=5 }
		if (ydir >0.6666 && (ypos <height-5)) { ypos+=5 }
		gfx.fillRect(xpos,ypos,4,4)
	}
}
void showAbout(event) {
	JOptionPane.showMessageDialog(frame,
	'''2次元ランダムウォーク
赤い四角がランダムに歩きます。''')
}