Click here to Skip to main content
15,868,016 members
Articles / Desktop Programming / MFC
Article

JLIB - A C++ porting of java.awt.*

Rate me:
Please Sign up or sign in to vote.
4.78/5 (37 votes)
30 Nov 20034 min read 136K   1.5K   40   34
An experimental C++ porting of java.awt.* and partial java.lang.*

Introduction

JLIB is a C++ porting of Java's "awt" and partial "lang" package, it supports basic GUI and multi-threading programming. Its purpose is to establish a base to study the differences of C++ and Java, especially the differences while writing libraries.

Background

If a C++ programmer has used Java for a while, when he comes back to the world of C++, he will inevitably recall the good times when he was using Java. At least I am in this case.

C++ is more difficult to study and harder to use than Java for most projects, not only because of the inherent complexity of the language, but also because of the lack of easy-to-use libraries. Even worse, there are totally no standard C++ GUI libraries to use. However, sometimes we are forced to use C++, both for technical and political reasons. So, porting some Java code to C++ becomes an interesting exercise for programmers.

At first, I tried to find one on the web. However, the result was disappointing. Only four were found:

  1. "jtk" by Guillaume Desnoix - a library which is exactly what I dream to, but it had been suspended for years.
  2. "jlWindows" (at CodeProject) by Gabriel Bizzotto - this library is a Win32 porting of Java AWT, but as the author is very busy, this project is also suspended. The author gave me some very helpful suggestions and was very kind to let me use the code of "jlWindows".
  3. AWT-Layout's contrib for wxWindows - it implements AWT style LayoutManagers for wxWindows.
  4. "Celios" from ElCel Technology - a commercial library, not free. It includes network, multi-threading and Unicode support, but does not have "awt" support.

I was quite surprised that there were only four such libraries. There must be more but I simply had not found. At that time, I was reading JDK's community source code and had some working code on my hand, I decided to write a new one to test the idea, this is where JLIB comes from.

JLIB should NOT be commercially used in any form. The purpose of JLIB is only for study. I hope it can provide a runnable code base to demonstrate various differences which influence the way libraries are implemented in Java and C++ and test if it is reasonable to port a Java library to C++.

Currently, JLIB supports all basic "awt" components, such as Button, TextArea and Checkbox; some "swing" components and containers, although it lacks some features, such as Slider, ProgressBar and SplitPane. Most layout managers are also ported from JDK directly to C++. JLIB also includes basic threading support, such as Thread, Monitor (implements the wait/notify methods of java.lang.Object) and a simulated synchronized keyword.

Using the code

As JLIB is only for studying, I did not try to compile it with every compiler, currently JLIB can pass gcc3.2 (mingw2.0) and Visual C++ .NET compilation. You can download and decompress jlib.zip to a folder. jlib.sln and jlib.vcproj are project files for Visual C++ .NET and a makefile is for C++.

Currently there are no documents for JLIB. If you want to view documents, refer to corresponding Java documentation.

Now there is only a Win32 implementation, I plan to implement a Linux version in future. Classes of JLIB are presented in a straightforward way for a Java programmer. Here is an example:

C++
// A tanslation of the following java "Hello,world!" program with JLIB.
//
//import java.awt.*;
//import java.awt.event.*;
//
//class Hello extends Frame {
//    Label hello = new Label("Hello, world!", Label.CENTER);
//
//    Hello() {
//        add(hello);
//
//        addWindowListener(new WindowAdapter() {
//        public void windowClosing(WindowEvent e) {
//            dispose();
//        }
//        });
//    }
//
//    public static void main(String[] args) {
//        Hello hello = new Hello();
//        hello.pack();
//        hello.show();
//    }
//}

// windows.h is required
#include <windows.h> 

// one header for one class, tedious but foolproof
#include <jlib/jlib.h> 
#include <jlib/awt/Frame.h>
#include <jlib/awt/Label.h>
#include <jlib/awt/event/WindowAdapter.h>
#include <jlib/awt/event/WindowEvent.h>

using namespace jlib::awt;
using namespace jlib::awt::event;

class Hello : public Frame, WindowAdapter {
    // A label, in C++, can not call constructor at here, call it
    // at the enclosing class's ctor-initilizer.
    Label hello;

    Hello()
        : Frame("Hello"), 
        hello("Hello, world!", Label::CENTER) { // construct the label
        
        // add the label into the frame.
        add(&hello);

        // no non-static inner class in C++, so let the enclosing
        // class to be the listener.
        addWindowListener(this);
    }

    void windowClosing(const WindowEvent&) {
        dispose();
    }

public:
    // the entry point of the program, will be called from JLIB.
    // ArgumentList is simply a typedef of std::vector<sring>.
    static void main(const ArgumentList&) {
        Hello hello;
        hello.pack();
        hello.show();
    }
};

MAIN_CLASS(Hello) // the class that has the static main method

Points of Interest

There are really some interesting and annoying points while writing JLIB, I will post it here later.

Examples

Some example AWT programs from JDK are ported to JLIB, here are the screenshots:

ArcTest

Clock

SimpleGraph

CardTest

NervousText

Blink

DrawTest

History

  • Dec 4, 2002 - First revision.
  • Dec 25, 2002 - Second revision.
    1. Toolkit::getComponent(HWND) was implemented to support "opposite" component in FoucsEvent and WindowEvent. Pointers to components are put into a vector (Toolkit::displayableComponents). The index in the vector was kept into the "userdata" of the corresponding HWND. Any time a Windows message is received, the index is retrieved from the window handle to look up the Component* in the vector. When a window handle should be destroyed, if it is not the last one in the vector, the last will be swapped to the freed slot and the index recorded in the HWND will be adjusted, then the last entry in the vector will be popped. This scheme features a constant lookup time while keeping memory footprint minimized. For details, see Toolkit::getComponent(HWND), Toolkit::addDisplayable(Component*)and Toolkit::removeDisplayable(Component*).
    2. TabbedPane was implemented. Icon and ImageIcon were added.
    3. IGraphics was discarded, the return value of Component::getGraphics() was changed to std::auto_ptr<Graphics>. IImage was discarded and Image Toolkit::getImage() was replaced by std::auto_ptr<Image> Toolkit::createImage().
    4. Graphics::draw3DRect() and Graphics::fill3DRect were added. Color::brighter() and Color::darker() were added.
    5. Two more JDK samples were ported to JLIB - "Blink" and "DrawTest".
    6. Header files were rearranged to speed up compilation. Defined _T macro if it has not been defined to eliminate excessive inclusions of <tchar.h>.
    7. Many bug fixes (probably more were introduced).
  • Nov 17, 2003 - Third revision.
    1. Added consume() support for InputEvents.
    2. EventListeners can be automatically removed from EventDispatchers during destruction. This can greatly ease the management of EventListeners.
    3. Merged jlib::awt::event namespace and jlib::awt namespace as the two namespaces are mutually dependent.
    4. Project files for VC7 were rearranged to facilitate building demos.
    5. Decent make files for gcc are provided. To build the project, you need mingw32 and msys, you can download them from SourceForge.
    6. Minor improvements and bug fixes.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
hzy
Web Developer
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generalwhy do not try acdk at sourceforge Pin
jackyxinli14-Oct-10 15:18
jackyxinli14-Oct-10 15:18 
hi, hzy,
i remember i'd visited acdk at sourceforge when i read your article. and this project had implemented most features of jdk package, i recommend it to you and try it, very nice.
project link is:http://sourceforge.net/projects/acdk/[^]
Generalwin ce Pin
chelsea_zhai28-Jan-10 21:11
chelsea_zhai28-Jan-10 21:11 
GeneralBuffered image class Pin
Vincent3434534310-Oct-05 1:18
sussVincent3434534310-Oct-05 1:18 
GeneralThe Cursor problem and suggestion Pin
gongwx16-Jun-05 16:38
gongwx16-Jun-05 16:38 
GeneralSome problem Pin
Tu Nam12-Apr-04 2:50
Tu Nam12-Apr-04 2:50 
GeneralNewJ Library for C++ Pin
domKing9-Dec-03 13:35
domKing9-Dec-03 13:35 
GeneralNewJ Library for C++ Now Supports JNI Pin
domKing19-May-04 12:02
domKing19-May-04 12:02 
GeneralInteresting, but PortableGui is most developed at this time. Pin
Rodrigo Pinto Pereira de Souza4-Dec-03 1:39
Rodrigo Pinto Pereira de Souza4-Dec-03 1:39 
QuestionAutomatic translation? Pin
Tobix3-Dec-03 22:56
Tobix3-Dec-03 22:56 
AnswerRe: Automatic translation? Pin
whymegod4-Dec-03 8:24
whymegod4-Dec-03 8:24 
GeneralRe: Automatic translation? Pin
pizzafroide211-Jul-06 7:06
pizzafroide211-Jul-06 7:06 
AnswerRe: Automatic translation? Pin
hzy6-Dec-03 16:19
hzy6-Dec-03 16:19 
AnswerRe: Automatic translation? Pin
Vitaly Shelest8-Feb-06 22:10
Vitaly Shelest8-Feb-06 22:10 
GeneralHey Dude Pin
whymegod3-Dec-03 15:40
whymegod3-Dec-03 15:40 
GeneralVery good! Pin
TomM2-Dec-03 4:37
TomM2-Dec-03 4:37 
GeneralRe: Very good! Pin
hzy6-Dec-03 16:10
hzy6-Dec-03 16:10 
GeneralOh! Pin
Stephane Rodriguez.3-Jan-03 0:53
Stephane Rodriguez.3-Jan-03 0:53 
Generalwell done Pin
aamironline2-Jan-03 17:00
aamironline2-Jan-03 17:00 
GeneralAwesome Pin
Peter Kohout5-Dec-02 11:25
Peter Kohout5-Dec-02 11:25 
GeneralNice Pin
joegood5-Dec-02 8:15
joegood5-Dec-02 8:15 
GeneralThis is cool! Pin
Daniel Turini5-Dec-02 7:43
Daniel Turini5-Dec-02 7:43 
Generalgreat work Pin
eco4-Dec-02 22:37
eco4-Dec-02 22:37 
QuestionWhy? Pin
Nemanja Trifunovic4-Dec-02 10:24
Nemanja Trifunovic4-Dec-02 10:24 
AnswerRe: Why? Pin
Jörgen Sigvardsson4-Dec-02 10:41
Jörgen Sigvardsson4-Dec-02 10:41 
GeneralRe: Why? Pin
Giles4-Dec-02 11:03
Giles4-Dec-02 11:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.