[e-lang] E's Tamed Java API - Drawing in a UI

Marc Stiegler marcs at skyhunter.com
Wed Mar 29 15:46:30 EST 2006


Zooko's Epainter depends for operation upon using a Java method that the
E JavaDoc says is suppressed. In fact, the E JavaDoc is several years
stale, and the method EPainter needs was unsuppressed when Zooko
identified it (and I verified) that it was the key method for enabling
canvas drawing, (and that it did not reveal any inappropriate or
surprising authority). There are only a few changes in what is and is
not suppressed from those years of staleness, but a couple of the
changes are crucial, like this one. Looking at the safej files is the
only way to be certain what is suppressed and what is not.

Anyway, a key element of successful drawing (whether in E or Java) is to
avoid getting the graphicsContext from the panel, but rather, have the
system return the graphicsContext object to you when a paint event
notification is raised. Unless you work inside the paint event, you will
not know to repaint when a part of the panel is revealed during window
dragging. The one technique of getting the context that is allowed in E 
is the specific technique that is the standard when writing serious Java 
apps.

If, between kevin's example and my high-level explanation here, you are
still having trouble, bug me again and I'll dig up more stuff on it for
you. At the moment, the Java UI tools are not swapped into primary RAM
in my brain :-)

--marcs

Kevin Reid wrote:
> 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/>
>
>
> _______________________________________________
> e-lang mailing list
> e-lang at mail.eros-os.org
> http://www.eros-os.org/mailman/listinfo/e-lang
>
>





More information about the e-lang mailing list