Interactive Processing Window from REPL

[Edit 08/09/2011 I’m keeping an updated version here.]

Here’s a little thing I concocted:

import processing.core._; import PApplet._; import PConstants._;
object Test extends PApplet {
	override def setup() { size(500, 500); }
	override def draw() {}
}
PApplet.runSketch(Array("Test"), Test)
import Test._

Assuming you have the processing library in the classpath, you can copy and paste that code into the Scala REPL and you suddenly have an interactive graphical environment:

Welcome to Scala version 2.8.0.final (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_23).
Type in expressions to have them evaluated.
Type :help for more information.

scala> import processing.core._; import PApplet._; import PConstants._; import org.zhang.lib.misc._;
object Test extends PApplet {
	override def setup() { size(500, 500); }
	override def draw() {}
}
PApplet.runSketch(Array("Test"), Test)
import Test._
import processing.core._
import PApplet._
import PConstants._
import org.zhang.lib.misc._

scala>      |      |      | defined module Test

scala>
scala> import Test._

scala> background(255)

scala> var (x, y) = (width/2, height/2):(Float, Float)
x: Float = 250.0
y: Float = 250.0

scala> def newDir = random(4).asInstanceOf[Int] match { case 0 => (x, y+5); case 1 => (x, y-5); case 2 => (x+5, y); case 3 => (x-5, y); }
newDir: (Float, Float)

scala> for(i <- 0 until 1000) {val (nx, ny) = newDir; line(x, y, nx, ny); x = nx; y = ny; }

scala> save("frame1.png")

frame1.png

scala> filter(BLUR, 3)

scala> def randomWalk() {
     | var (x, y) = (width/2, height/2):(Float, Float)
     | def newDir = random(4).asInstanceOf[Int] match { case 0 => (x, y+5); case 1 => (x, y-5); case 2 => (x+5, y); case 3 => (x-5, y); }
     | for(i <- 0 until 1000) {val (nx, ny) = newDir; line(x, y, nx, ny); x = nx; y = ny; }
     | }
randomWalk: ()Unit

scala> randomWalk()

scala> filter(BLUR, 3)

scala> save("frame2.png")

frame2.png

scala> for(i <- 0 until 10) {randomWalk(); filter(BLUR, 3)}

scala> save("frame3.png")

frame3.png

scala> colorMode(HSB)

scala> background(255)

scala> for(i <- 0 until 100) {stroke(i*2.5f, 255, 255); randomWalk(); filter(BLUR, 1); }

scala> save("frame4.png")

frame4.png

Immediate feedback ftw!

Advertisements


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s