Piccolo2D with Scala

113 views
Skip to first unread message

Samuel Robert Reid

unread,
Oct 12, 2009, 12:59:13 PM10/12/09
to piccolo...@googlegroups.com
Piccolo team,

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)
}
}
}
}

piccolo-scala.png

Michael Heuer

unread,
Oct 12, 2009, 1:17:47 PM10/12/09
to piccolo...@googlegroups.com
Samuel Robert Reid wrote:

> 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

Travis

unread,
Oct 12, 2009, 10:23:46 PM10/12/09
to Piccolo2D Users
It may not have been apparent from my post
http://groups.google.com/group/piccolo2d-users/t/d5eaa301510b682
that Clojure is equally functional.

(def run-in-swing-thread [r] (SwingUtilities/invokeLater r))

so that you may

(run-in-swing-thread #(doto (JFrame.) (.setVisible true)))

Although that's actually not much more concise than just

(SwingUtilities/invokeLater #(doto (JFrame.) (.setVisible true)))

I don't mean to start a language showdown. I learned Scala before
Clojure, actually, and I appreciated it a lot.
>  piccolo-scala.png
> 65KViewDownload
Reply all
Reply to author
Forward
0 new messages