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