[e-lang] E's Tamed Java API - Drawing in a UI
Kevin Reid
kpreid at attglobal.net
Mon Mar 27 10:42:59 EST 2006
On Mar 26, 2006, at 22:34, Toby Murray wrote:
>
> I've come to writing the UI and want the ability to draw arbitrarily
> within a fixed region of a window. ...
>
> But the getGraphics() method of all JComponents is suppressed
> (according
> to the javadocs on erights.org), from which JPanel inherits.
The class com.zooko.tray.EPainter is intended for this purpose.
Here's a demo application I wrote a while ago and just cleaned up:
#!/usr/bin/env rune
pragma.enable("easy-return")
pragma.disable("explicit-result-guard")
# --- init
currentVat.morphInto("awt")
# --- definitions
def makeEPainter := <unsafe:com.zooko.tray.EPainter>
def catching(inner) {
def catcher {
match msg {
try {
E.callWithPair(inner, msg)
} catch p {
stderr.println(`$p${p.eStack()}`)
null
}
}
}
return catcher
}
def makeScribbleComponent(model) {
var x := 0
var y := 0
def scribbleC := makeEPainter(catching(def painter {
to paintComponent(g) {
for line :Tuple[int, int, int, int] in model {
E.call(g, "drawLine", line)
}
}
}))
scribbleC.addMouseListener(catching(def ml {
to mousePressed(event) {
x := event.getX()
y := event.getY()
}
match _ {}
}))
scribbleC.addMouseMotionListener(catching(def mml {
to mouseDragged(event) {
model.push([x, y,
x := event.getX(),
y := event.getY()])
scribbleC.repaint()
}
match _ {}
}))
scribbleC.setPreferredSize(<awt:makeDimension>(300, 300))
return scribbleC
}
# --- application
def lines := [].diverge()
def done
def frame := <unsafe:javax.swing.JFrame>("Scribble")
frame.setContentPane(makeScribbleComponent(lines))
frame.addWindowListener(catching(def mainWindowListener {
to windowClosing(event) :void {
done__Resolver.resolve(null)
}
match _ {}
}))
frame.setLocation(50, 50)
frame.pack()
frame.show()
interp.waitAtTop(done)
--
Kevin Reid <http://homepage.mac.com/kpreid/>
More information about the e-lang
mailing list