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:
- "jtk" by Guillaume Desnoix - a library which is exactly what I dream to, but it had been suspended for years.
- "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".
- AWT-Layout's contrib for wxWindows - it implements AWT style LayoutManagers for wxWindows.
- "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:
#include <windows.h>
#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 {
Label hello;
Hello()
: Frame("Hello"),
hello("Hello, world!", Label::CENTER) {
add(&hello);
addWindowListener(this);
}
void windowClosing(const WindowEvent&) {
dispose();
}
public:
static void main(const ArgumentList&) {
Hello hello;
hello.pack();
hello.show();
}
};
MAIN_CLASS(Hello)
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:
History
- Dec 4, 2002 - First revision.
- Dec 25, 2002 - Second revision.
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*)
.
TabbedPane
was implemented. Icon
and ImageIcon
were added.
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()
.
Graphics::draw3DRect()
and Graphics::fill3DRect
were added. Color::brighter()
and Color::darker()
were added.
- Two more JDK samples were ported to JLIB - "Blink" and "DrawTest".
- Header files were rearranged to speed up compilation. Defined
_T
macro if it has not been defined to eliminate excessive inclusions of <tchar.h>
.
- Many bug fixes (probably more were introduced).
- Nov 17, 2003 - Third revision.
- Added
consume()
support for InputEvent
s.
EventListener
s can be automatically removed from EventDispatcher
s during destruction. This can greatly ease the management of EventListener
s.
- Merged
jlib::awt::event
namespace and jlib::awt
namespace as the two namespaces are mutually dependent.
- Project files for VC7 were rearranged to facilitate building demos.
- Decent make files for gcc are provided. To build the project, you need mingw32 and msys, you can download them from SourceForge.
- Minor improvements and bug fixes.