Skip to content

Instantly share code, notes, and snippets.

@aalmiray
Created May 22, 2009 05:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aalmiray/115941 to your computer and use it in GitHub Desktop.
Save aalmiray/115941 to your computer and use it in GitHub Desktop.
import java.awt.Color
import java.awt.Font
import matrix.Letter
import org.pushingpixels.trident.Timeline
import org.pushingpixels.trident.Timeline.RepeatBehavior
// warning!! reading font file on EDT
InputStream is = MatrixView.classLoader.getResourceAsStream("katakana.ttf")
Font kf = Font.createFont(Font.TRUETYPE_FONT, is)
font = kf.deriveFont(Font.BOLD, 14i)
def newDrop = { x ->
def drop = new Expando()
drop.letters = []
drop.paint = { g -> drop.letters.each{ it.paint(g) } }
drop.scenario = { ->
int totalLetterCount = 5 + (20 * Math.random())
int initialDelay = 1000 * Math.random()
int duration = 1000 + (100 * Math.random())
parallelScenario {
(0..<totalLetterCount).each { i ->
int y = font.size * i
// choose random katakana letter
// int letterIndex = (int) (0x30A0 + Math.random()
// * (0x30FF - 0x30A0));
int start = 33
int delta = 95
char c = (char) (start + Math.random() * delta)
Letter l = new Letter(x as int, y as int, c, font)
drop.letters << l
timeline(l, duration: duration, initialDelay: initialDelay + i * 120) {
interpolatedProperty("opacity", from: 1.0f, to: 0.0f)
interpolatedProperty("color", from: Color.WHITE, to: Color.GREEN)
}
}
}
}
return drop
}
def lock = new Object()
def drops = []
def addDrop = null
addDrop = { canvas ->
def x = canvas.width * Math.random()
def drop = newDrop(x)
def scenario = drop.scenario()
timelineScenario(scenario) {
timelineScenarioCallback {
onDone {
synchronized(lock) {
drops.remove(drop)
addDrop(canvas)
}
}
}
}
drops << drop
scenario.play()
}
tabbedPane(tabGroup, selectedIndex: tabGroup.tabCount) {
panel(title: "Matrix") {
borderLayout()
def addMoreDrops = { if(drops.size() < 30) 0.upto(10){ addDrop(canvas) } }
panel( new CanvasPanel(
componentShown: addMoreDrops, componentResized: addMoreDrops,
draw: { p, g ->
g.color = Color.BLACK
g.fillRect(0,0,p.width,p.height)
synchronized(lock) {
drops.each{ it.paint(g) }
}
}), id: "canvas")
Timeline repaint = new Timeline.Repaint(canvas)
repaint.playLoop(RepeatBehavior.LOOP)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment