Mar 7, 2010
Processing in Scala!
One of the great things about scala is that it inter-operate with any existing Java library. Both ways, use scala classes from java and java from scala. Here’s an quick example of using Processing in Scala.
I’m using IntelliJ IDEA 9.0.1 with the Scala plugin.
To get Processing working in IntelliJ:
- Download the processing.org jars
- Create a Scala project.
- Add them to your libraries list in the modules setting.
- Create a new scala object example below.
How is this useful? Scala’s syntax and its core data structures allow processing of data in a much more concise and understandable manner. Imagine you want to visualize xml or csv files with the Processing/Scala combo. Scala makes this easy with its xml and string manipulation libraries.
This example code is from Ben Fry’s Visualizing Data Book
import processing.core._
object Draw extends processing.core.PApplet {
override def setup() {
size(200, 200)
noStroke( )
fill(0, 102, 153, 204)
}
override def draw() {
background(255)
rect(width-mouseX, height-mouseY, 50, 50)
rect(mouseX, mouseY, 50, 50)
}
def main(args: Array[String]) {
val frame = new javax.swing.JFrame("Draw")
frame.getContentPane().add(Draw)
Draw.init
frame.pack
frame.setVisible(true)
}
}
