[e-lang] "Hello, Swing!": a simple Swing application in E and in Jython

Zooko e-lang@mail.eros-os.org
Sun, 21 Apr 2002 05:38:49 -0700


Here is a simple program that draws a picture in a JFrame.  I wrote the 
E version first, to test my "EPainter" glue code [1].  When it didn't work, 
I wrote the Jython version, to test my understanding of the Swing API.  The 
Jython version does what I expect, the E version doesn't.  (But the EPainter 
glue code *does* do what I expect (observed by println's), so the problem is 
somewhere in the following script itself.)

I'll put some comments comparing Jython and E after these listings:

------- begin included E script
def JFrame := <unsafe:javax.swing.JFrame>
def ColorBlack := <unsafe:java.awt.Color> black()
def EPainter := <unsafe:com.zooko.tray.EPainter>

def makeHelloer() :any {
    def whythistoken {
        to paint(g) {
            println("mubmle\n")
            g setColor(ColorBlack)
            g drawString("hello", 10, 10)
            g fillOval(10, 10, 100, 100)
        }
    }
}

def myep := EPainter new(makeHelloer())
def myjf := JFrame new()
myep setOpaque(true)
myjf setContentPane(myep)
myjf setSize(256, 256)
myjf setVisible(true)

------- end included E script

------- begin included Jython script
from javax.swing import JFrame, JPanel
from java.awt import Color

class Helloer(JPanel):
    def paint(self, g):
        print "mubmle\n"
        g.setColor(Color.black)
        g.drawString("hello", 10, 10)
        g.fillOval(10, 10, 100, 100)

myhr = Helloer()
myjf = JFrame()
myjf.setContentPane(myhr)
myjf.setSize(256, 256)
myjf.setVisible(1)

------- end included Jython script


My comments:

The first issue is that the E version doesn't work!  What have I done wrong?  
Certainly the fact that I've coded a bug here that I haven't been able to find 
is the big issue.  It's probably something obvious that I wouldn't do if I had 
a bit more experience programming in E.

The second issue is that the E version requires "EPainter.java" glue code 
written specifically for this script.  One of my reasons to do this experiment 
is to investigate making a tool for E which does the same thing that Jython 
does -- transparently create glue so that E (Jython) objects can inherit from 
Java classes.


After those big two issues then we get down to matters of syntax and style, 
which are not considered of primary interest on the e-lang list right now, 
but I might as well write them down:

1.  See `whythistoken' in the E version.  It's irritating to be required to 
type a token whose purpose is unclear to me.  Note that I wrote this by 
looking at _EiaW_'s example code for an object and copying it, not by learning 
E's underlying semantics and "inventing" objects for myself.

2.  I prefer Python's whitespace to E's curly braces (but oh well).

3.  I (currently) prefer Python's dot operator to E's whitespace operators 
(but oh well).

4.  I prefer Python's "thingie = expr" to E's "def thingie := expr" (but oh 
well).

5.  I prefer Python's `Helloer()' and `JFrame()' to E's `makeHelloer()' and 
`JFrame new()' -- especially the inconsistency in E's spellings.  (I know 
MarcS has just been writing about this.)

6.  The two are actually remarkably similar!  This makes me feel nice and comfy.


Oh, and by the way: 7. it was actually easier to install E than to install 
Jython.  :-)

Regards,

Zooko

Zooko.Com -- Security and Distributed Systems Engineering

[1] http://zooko.com/EPainter.java