Java GUI developers, suffer no more ;-)

Published Monday August 13th, 2007
6 Comments on Java GUI developers, suffer no more ;-)
Posted in Graphics View, Qt, Qt Jambi

I wrote my first Jambi program today (That’s Qt for Java, btw). I admit to being one hard core C++ developer, but having nothing against Java as a language… as opposed to its horrible APIs. Which is also why I’ve shyed away from Qt Jambi, making little sarcastic remarks to those of my colleagues that have done all the work implementing it. But I coincidentally found myself in a situation where I was forced to write a Jambi application. Or rather, port a C++ application to a Jambi .java file. And uhm, I guess you can say I was positively surprised. πŸ™‚

Screenshot of a Jambi application
My First Qt Jambi app, showing perspective transformations using Graphics View.

Since let’s face it, beyond the difference in syntax there’s nothing very different between these two languages (that I care about anyway). Now I am a C++ enthusiast, but I do still think garbage collection is nice sometimes [*], although _constantly_ having to “new” everything is somewhat annoying, just look at the source at the bottom of this post. Native C++ generates library files that need to be in LD_LIBRARY_PATH or somehow known to the runtime when you launch your app. And likewise, Java apps need to know their .jars and .class files. But language details is really not the issue when I choose a tool for a job.

class CustomScene extends QGraphicsScene {
    protected void drawBackground(QPainter painter, QRectF exposed) {
        painter.fillRect(sceneRect(), new QBrush(new QColor(230, 240, 255)));
        painter.setPen(new QPen(QColor.lightGray, 1));

I downloaded the Jambi Linux32 GPL OpenSource package from ftp://ftp.trolltech.com/qtjambi/source, and unpacked it into my home directory as /home/ahanssen/qtjambi-linux32-gpl-4.3.1_01. With some help from Eskil, who pointed out that the package contains a .jar file that basically is all you need to worry about, I spent about 15 minutes porting my app to Jambi. Why so long? Well, because there are a few things the fresh Jambi developer needs to know about what the Qt API looks like in the Java world. And (chuckle) I was too lazy to look up the complete docs at doc.trolltech.com. They even made JavaDoc documentation. Awwww… πŸ˜‰

  • Where C++ uses namespacing with ::, Java uses a ‘.’ instead.
  • For enums, you need to include the name of the enumerator like this: Qt.Orientation.Horizontal.
  • Java doesn’t understand a->b ;-), hehe. Just search & replace your “->” with “.” right away.
  • Typically all qualifiers for function args go away. So “const QRectF &” becomes “QRectF”.
  • There’s no stack-allocation, so setPen(QPen()) becomes setPen(new QPen()).
  • Qt Jambi uses Java’s native types, so instead of qreal, etc, just use double.

I encourage you to try it out, it’s really easy. And it _is_ Qt. To run my Jambi program, all I did was
type this into my console:

# Compile
ahanssen@schizo:~> javac -cp qtjambi-linux32-gpl-4.3.1_01/qtjambi.jar Perspective.java

# Run
ahanssen@schizo:~> java -cp qtjambi-linux32-gpl-4.3.1_01/qtjambi.jar:. Perspective

Note: I didn’t modify my env variables at all to get this working.

Download the source file from http://www.andreas.hanssen.name/Perspective.java, follow the above instructions, and see if it works for you :-).

[*] You cannot believe how much crap I’ve had to deal with because of explicit deleting of objects in Graphics View ;-). It all works fine now, but in a GC world, it wouldn’t have been an issue…

Do you like this? Share it
Share on LinkedInGoogle+Share on FacebookTweet about this on Twitter

Posted in Graphics View, Qt, Qt Jambi

6 comments

Hi, nice to hear something about QtJambi in use…
I think QtJambi was a great decision, because many german students i know learn mostly Java at their universities.
So they all might have easy step into the Qt World.
Java is always said to be damn slow vs. C/C++ applications.

Maybe you as a C++ guy may tell something if there is a big performance gap between a Qt and a QtJambi project.

Peppelorum says:

Cool! And lots of thanks for a live and functional example of how a to use QT Jambi, this is much of help for us that want to try out QT and Java. A question though, is it possible to “embed” the qtjambi.jar when creating a jar on your own? So that distribution and startup becomes more simple I mean.

Cheers!
Peppe

Jan says:

Wow, as an JAVA developer and KDE fan, I’m happy to see Jambi! And it is funny to hear that some people still believe that java is “damn slow” πŸ˜‰ I’ll try it out.

I wrote that it is always said to be slow.
I was just interested about a C++ guys opinion.
imho Java is a great language, because of the ease of reusability of code, you don’t rellay have to care about binary compatibility…

eskil says:

Peppelorum: the qtjambi.jar file is just a .zip file which contains all the compiled Qt Jambi classes. Using Sun’s jar tool, you can easily repackage the .class files exactly as you wish.

Vexorian says:

I wonder if the KDE of the future will head towards Java, I would prefer it to stick to C++ but if a choice is to be done I hope it is towards Jambi and not MONO like gnome is doing.

Commenting closed.

Get started today with Qt Download now