I wrote a sample application with Scala, thought you might like to see
how it looks (inspired by the earlier Piccolo2D/Clojure post). I added
a few library functions to simplify client code, For example, I added a
higher-order-function runInSwingThread{} so that you can do this:
//scala
runInSwingThread{
new JFrame().setVisible(true)
}
instead of this:
//java
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new JFrame().setVisible(true);
}
});
Also, Scala makes it easy to describe objects using the primary constructor:
//scala
val myTextNode = new PText("hello"){
setFont(new Font(...))
setTextPaint(...)
}
I also added a new function addDragListener, that takes a function of
type (Double,Double)=>Unit, and I added some layout code like this:
//scala, centers the text below the circle
text centered_below circle
Here's the example, and a screenshot is attached.
//scala
object TestScalaPiccolo {
def main(args: Array[String]) = {
runInSwingThread {
new MyJFrame {
contentPane = new SCanvas {
val circle = this add new PhetPPath(new Ellipse2D.Double(0, 0,
100, 100), blue, new BasicStroke(6), yellow) {
addInputEventListener(new CursorHandler)
this.addDragListener((dx: Double, dy: Double) =>
translate(dx, dy))
setOffset(200,200)
}
val text = this add new PText("Hello"){
var numTimes = 0
this.addMousePressListener( ()=>{numTimes = numTimes + 1;
setText("Mouse Pressed "+numTimes+" times")})
}
text centered_below circle
this add new PText("Another text")
}
setSize(new Dimension(800, 600))
setDefaultCloseOperation(EXIT_ON_CLOSE)
setVisible(true)
}
}
}
}
> I wrote a sample application with Scala, thought you might like to see
> how it looks (inspired by the earlier Piccolo2D/Clojure post). I added
> a few library functions to simplify client code, For example, I added a
> higher-order-function runInSwingThread{} so that you can do this:
> ...
These are cool. How about we create a page on the wiki to host these?
Perhaps we'll find volunteers for examples in other JVM scripting
languages, Groovy, JRuby, Jython, etc.
I imagine we might end up with a bunch of static methods for making
scripting easier, we should add those to a ScriptUtil class or some
such.
michael