Skip to content

Instantly share code, notes, and snippets.

@aalmiray
Created July 9, 2009 23:45
Show Gist options
  • Save aalmiray/144104 to your computer and use it in GitHub Desktop.
Save aalmiray/144104 to your computer and use it in GitHub Desktop.
import groovy.swing.SwingBuilder
import griffon.builder.gfx.*
import griffon.builder.gfx.swing.GfxPanel
import java.awt.Color
class SphereNode extends CustomGfxNode {
@GfxAttribute(alias="r") double radius = 90
@GfxAttribute double cx = 100
@GfxAttribute double cy = 100
@GfxAttribute Color base = Colors.get("blue")
@GfxAttribute Color ambient = Colors.get(r: 6, g: 76, b: 160, a: 127)
@GfxAttribute Color specular = Colors.get(r: 64, g: 142, b: 203, a: 255)
@GfxAttribute Color shine = Colors.get("white")
SphereNode() {
super("sphere")
}
DrawableNode createNode(GfxBuilder builder) {
double height = radius * 2
builder.group(borderColor: 'none') {
circle(cx: cx, cy: cy, radius: radius, fill: base)
circle(cx: cx, cy: cy, radius: radius) {
multiPaint {
radialGradient(radius: radius) {
stop(offset: 0.0f, color: ambient)
stop(offset: 1.0f, color: rgba(alpha: 204))
}
radialGradient(cy: cy + (height*0.9),
fy: cy + (height*1.1)+20,
radius: radius) {
stop(offset: 0.0f, color: specular)
stop(offset: 0.8f, color: specular.derive(alpha: 0))
transforms{ scale(y: 0.5) }
}
radialGradient(fit: false, fx: 55, fy: 35, radius: height/1.4){
stop(offset: 0.0f, color: shine.derive(alpha:0.5))
stop(offset: 0.5f, color: shine.derive(alpha:0))
}
}
}
}
}
}
def gfx = new GfxBuilder()
def node = gfx.group() {
antialias true
background(color('white'))
(0..41).each{ i ->
def color = i % 5 == 0 ? color('red') : color('gray')
line(x1: 0, y1: i*10, x2: 410, y2: i*10, bc: color)
line(y1: 0, x1: i*10, y2: 410, x2: i*10, bc: color)
}
customNode(n: "1", SphereNode)
customNode(n: "2", SphereNode, cx: 300,
base: color('red'),
ambient: Colors.get(r: 160, g: 76, b: 6, a: 127),
specular: Colors.get(r: 203, g: 64, b: 64, a: 255))
customNode(n: "3", SphereNode, cy: 300,
base: color('green'),
ambient: Colors.get(r: 160, g: 76, b: 6, a: 127),
specular: Colors.get(r: 64, g: 250, b: 64, a: 255))
customNode(n: "4", SphereNode, cx: 300, cy: 300,
base: color('orange'),
ambient: Colors.get(r: 160, g: 76, b: 6, a: 127),
specular: Colors.get(r: 203, g: 203, b: 64, a: 255))
}
def swing = new SwingBuilder()
swing.edt {
frame(title: "Sphere", size: [410,430], visible: true) {
panel(new GfxPanel(), id: "canvas", node: node)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment