Posts Tagged ‘ eclipse

Compare Jython and Java swing authoring

Since I’m teaching myself Java, and recently installed Jython, I thought it would be interesting to compare the two for authoring swing UI’s.  To aid in this development, I got the PyDev plugin for Eclipse installed, allowing for me to use Eclipse as an IDE for Jython and Python, nice.  FYI, I am by no means an expert at swing, or Java.  But the examples do work :)

Both examples display identical “Hello World!” window.

Here’s the Jython:

# texttest.py
from javax.swing import *
from java.awt import *

class TextTest(JFrame):
    def __init__(self):
        JFrame.__init__(self, "TextTest")
        self.setSize(256, 128)
        self.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
        pane = HelloPane()
        self.add(pane)
        self.setVisible(True)

class HelloPane(JPanel):
    def paintComponent(self, comp):
        f = Font("Arial", Font.PLAIN, 32)
        comp.setFont(f)
        comp.drawString("Hello World!", 32, 64)

if __name__ == "__main__":
    tt = TextTest()

And here’s the Java:

// TextTest.java
import javax.swing.*;
import java.awt.*;

public class TextTest extends JFrame {
    public TextTest() {
        super("TextTest");
        setSize(256, 128);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        HelloPane pane = new HelloPane();
        add(pane);
        setVisible(true);
    }

    public static void main(String[] arguments) {
        TextTest frame = new TextTest();
    }
}

class HelloPane extends JPanel {
    public void paintComponent(Graphics comp) {
        Graphics2D comp2D = (Graphics2D)comp;
        Font f = new Font("Arial", Font.PLAIN, 32);
        comp2D.setFont(f);
        comp2D.drawString("Hello World!", 32, 64);
    }
}

Two new books added to the library…

Got to make use of some 40% off coupons at Barnes & Nobel today:

  • Algorithms in a Nutshell : I’d been eying this book for a while now, finally took the plunge.  I’m particularly interested in their chapters about convex hull generation, and path-finding AI.  The problem-solving techniques in general I find quite fascinating.
  • Visualizing Data : By Ben Fry, one of the co-creators of Processing.  I find this interesting not only in the code-examples behind displaying data to the screen, but the processes behind finding and parsing the data in the first place.  Also serves as a good bridge between Processing and Java, with examples of running Processing in Eclipse (see my post here).

Processing in Eclipse

I think that Processing‘s default IDE, the PDE, is pretty accessible to the new programmer.  I’ve had a lot of fun with it.  But since Processing is based on Java, it could be presumed that it could be authored in a Java IDE.  And indeed it can.  Via the processing site, there is a tutorial that walks you through the process of setting up a simple Processing sketch inside the Eclipse Java IDE.  Why would you want to do this?  For me, mainly code reuse:  The PDE doesn’t allow for a lot of easyconvenient code reuse:  Say you make a particle class in sketchA, and you want to use it in sketchB:  You’ll need to physically duplicate the code inside of your new sketch.  Ouch.

After completing this excersize, it just makes me realize how little I know about Java!  But I think Eclipse is a great place to start learning it.

processingeclipose