MISC


j2me | applets and applications | gui development | graphics and games

Last updated: February 2005

Java has been most effective in creating server-side applications, but it has also been growing in popularity as a platform for consumer devices like cellphones. It remains a good choice for developing desktop applications and applets for the Web due to continuing evolution of GUI libraries. Recent developments in the graphics APIs have strengthened Java's ability to implement games and niche modelling applications.




j2me

Java's simplicity and strong security model make it very suitable for embedded systems — scaled down computer systems tailored to particular electronic devices. In fact, according to Sun, "Java started as a part of a larger project to develop advanced software for consumer electronics." Much later, with the emergence of Java 2 Micro Edition (J2ME), Java has become a leading option for developers on many devices such as cellphones.

The cellphone is a key example, but potential devices range all the way from pagers and smart cards up to set-top boxes. As most such devices will be much less powerful than PCs, a special Java platform is needed, one that is flexible enough to unify a rapidly expanding wireless world complicated by many competing standards. This platform, J2ME includes:

  • a special virtual machine (sometimes called the KVM) which uses much less memory.
  • small subsets of the standard API, appropriate for different classes of devices.
  • extra APIs for specific device families.
The first two of these comprise a configuration; the last is called a profile. The first and most prominent profile is MIDP (Mobile Information Device Profile). Aimed at cellphones, MIDP is supported by many vendors including Sun, Motorola, Nokia, Nextel and NTT DoCoMo. It is a set of Java classes built on top of the main configuration for all smaller devices: CLDC (Connected Limited Device Configuration), which includes the KVM. To create a J2ME application, developers don't need to have the target wireless device in hand; they can use an emulator on a PC, for example the application environment in the J2ME toolkit.

One important platform is PalmOS, used not only by Palm Computing for all its handheld devices, but also by several other companies which have licensed the technology. Sun has released a popular MIDP for PalmOS which enables developers to create little applications and download them along with the KVM to their Palm OS handhelds.

A key standard predating MIDP is WAP (Wireless Application Protocol), which was developed by OpenWave among others to enable wireless devices to access and browse the Internet. It can be used to download games and other applications to your cellphone. WAP is a lighter technology independent of Java; it is sometimes considered complementary to, sometimes, competing with MIDP. Gaps in WAP that MIDP should fill include: security, a more richly featured graphical interface, and ability to run applications while the device is not connected.

From 2003 on, a standard called Bluetooth has been very popular. Bluetooth is a wireless communication protocol which allows electronic devices (such as your cellphone and a PC) in close proximity to communicate using radio waves rather than a cable. Like WAP, Bluetooth is a wireless technology, but Bluetooth is strictly for local area communiction between devices. JSR 82 contains the Java APIs for developing Bluetooth applications, which can be done using Sun's J2ME Wireless Toolkit.

Before Sun developed MIDP, it had PersonalJava, which has been integrated into J2ME. PersonalJava contains a greater subset of the JDK, and is appropriate for higher-end wireless devices.

Application development for smaller devices like cellphones is generally considered more challenging. Although very popular, J2ME competes with platforms controlled by cellphone vendors such as Qualcomm's BREW and Symbian OS. Such platforms sometimes admit Java, but also encourage development in languages like C and C++. Some cellphones use the platform Windows Mobile 2003, but it is possible to put MIDP on these if it is not pre-installed.

A division of Sun called Sun Microelectronics created special chips optimized for Java. The core specification is called picoJava; an actual microprocessor called microJava adds I/O, memory, communications and control; and UltraJava is the advanced microprocessor including Sun's VIS (Visual Instruction Set) for graphics. If adopted widely, these chips would make the execution of Java on consumer devices much more efficient.

RESOURCES

Java 2 Micro Edition

J2ME resources and discussion

J2ME news




applets and applications

Having dealt a lot with J2EE and J2ME, let us return to basic desktop Java: what is now known as J2SE. Here at the core of Java there are two essential types of Java program: applications and applets. The application is the basic form, consisting of a set of related classes usually coded in separate files. Classes are grouped into packages, each package corresponding to a subdirectory on disk. As in C++, one file includes a main() function, allowing it to be invoked on the command-line and making it an entry-point for the application.

Here is some sample Java code, containing a class for printing the current date and time:


package examples.times; // the package this class is made part of

import java.util.Date; // import a class from the core Java API

public class CurrentTime
{
    private String currentTime; // member variable, illustrative

    // constructor
    public CurrentTime() {
        String time = getTimeAsString();
        System.out.println("The date/time is " + time);
        this.currentTime = time; // illustrative, not used
    }

    // method to get a String representation of the current time
    public String getTimeAsString() {
        Date now = new java.util.Date();
        return now.toString();
    }
 
    // main: automatically executed on calling 
    // "java examples.times.CurrentTime" from the command-line 
    public static void main(String args[])
    {
        new CurrentTime(); // call the constructor
    }
}

Applications are general-purpose: an applet is a special type of Java program which runs in a browser window. Applets are often smaller than applications and are constrained by the "sandbox" in which they operate from executing potentially dangerous instructions. But when Sun started popularizing Java in 1995 the browser market was also growing fast, so applets were the way many developers (including myself) were introduced to Java.

TextScroll Applet APPLET TEXTSCROLL REQUIRES COMMUNICATOR OR IE 4.

The applet developer writes code within a fixed structure of callback methods. An applet's lifecycle is defined in its mandatory superclass, java.applet.Applet, and includes these key methods which are to be overridden:

  • init()
  • start()
  • paint(Graphics g)
  • run()
  • stop()
  • destroy()
There is an example of an applet on the left: a little illustrative program for scrolling text I wrote. The code is stored in a .java file as usual and compiled to a .class file using javac or an IDE's compiler; tested using appletviewer or the IDE's runtime engine, and then embedded in a web page using code like the following:

<applet code="TextScroll.class" width=190 height=190>
<param name=img value="images/appletback.gif">
...
TextScroll Applet
</applet>

A recent development is the release of the Web Start tool from Sun. This allows applications (not just applets) to be launched from a browser.

RESOURCES

Overview of applets in Sun's Java tutorial

Gamelan has a large collection of applets




gui development

With the JDK 1.0 Sun included a basic GUI toolkit called the AWT, with classes for frames, buttons, labels, select lists (Choice and List), text fields, text areas, scrollbars, menus, checkboxes etc. You can do general line drawing on a Canvas, but you have to get a Graphics object — fonts and colors are associated with the graphical context.

All the GUI elements are subclassed under the Component class. One of the subclasses of Component is the Container class. Containers are graphical elements like panels or windows that hold other graphical elements. To place an element at a particular place in a container it is necessary to first specify a layout manager using setLayout and then use add. In other words elements are placed relative to each other rather than at specific coordinates. There are different layout managers provided: the most complex is GridBagLayout.

The AWT is very basic and cannot be used to create professional-looking applications. A couple of years after the introduction of Java, Sun attempted to overcome its limitations by introducing the JFC (Java Foundation Classes), whose core component was a a high-end GUI toolkit called Swing.

Swing includes all the main components that developers are used to working with — progress bars, tabbed panes, sliders, color choosers, etc. — in addition to extending the functionality of ordinary AWT components such as buttons and text boxes. It is also much more efficient, owing to the lightweight nature of all Swing components. This means that the drawing and events of each component is entirely controlled by Java; with the AWT's peer model it had been necessary to bind each component to its peer component on the native platform. With Swing everything is drawn by Java and this has the added advantage that you can have an arbitrary look and feel on any platform, Swing has three different looks built in — Windows, Motif, and the default JLF (Java Look-and-Feel). In addition, GUI applications can take advantage of JFC classes' support for undo/redo support, clipboard (data transfer) support and much better printing functionality.

Swing uses the MVC (Model-View-Controller) paradigm well-known to Smalltalk programmers. Each component has a Model to represent internal state and a View-Controller (also called a delegate) for the graphical look. For instance there are ButtonModel and ButtonUI (delegate) interfaces for the JButton component. If you wanted a button to keep track of how many times it has been clicked you would implement the ButtonModel interface; if you wanted to create a new look-and-feel for the buttons in your application you would need to implement the UI interface. The programmer can change the default implementations of both the Model and the UI using the methods getUI(), setUI(), getModel(), and setModel().

Typically an application developer will want to create a main window which allows many sub-windows — a Multiple Document Interface (MDI). Swing introduces support for this — the JDesktopPane and JInternalFrame classes are particularly useful.

Integrated into the Java 2 platform, JFC's Swing made earlier attempts like IFC from Netscape and AFC/WFC from Microsoft irrelevant. However, some still consider Swing too slow. Third parties have continued to develop alternative GUI APIs. A key one is the SWT (Standard Widget Toolkit) framework, which is packaged with IBM's Eclipse IDE. SWT actually returns to the AWT model of using native components. However, it is not restricted to a small range of common components like AWT, because it emulates components on platforms where they do not exist. Also, on top of SWT there is a library called JFace including wizards, utilities, and more advanced functionality. The Eclipse approach to GUI development is often praised as being somewhat faster than Swing, but criticized by Sun for not being "pure Java".

Java GUI applications have had only moderate success. For instance there have been various attempts from Corel, Lotus, Applix, and ThinkFree to create successful office suites implemented in Java, but none have threatened the market dominance enjoyed by Microsoft Office. Recently IBM (now owner of Lotus) announced a new office suite based on SWT. However, perhaps the most successful Java GUI applications have been development tools for Java such as Eclipse, NetBeans, Jtest, and JProbe.

RESOURCES

Introducing the AWT from JavaWorld

Screenshot of the SwingSet tour application.

Lissajous Curves Example — version of the graphical pattern generator shown at the top of the Overview page, but with controls. I got the basic algorithm from the book Computers and the Imagination by the graphical mathematician Pickover.

Eclipse vs. Swing from JavaPro

LucidDoc — an application I have built with Swing.




graphics and games

Sun has been working hard to make Java graphics competitive, implementing its platform-independent Media APIs on top of platform-dependent graphics packages. The Java 2D API, integrated into Java 1.2, presents a graphical model which goes far beyond what the AWT had to offer: greater rendering abstraction and flexibility; more colors and fonts; anti-aliased text; definition of objects independent of resolution using Bezier paths; transparency options; and many more additions.

There is also a Java 3D API (currently at version 1.3). It has been seen as a dynamic complement to VRML — whereas VRML can describe the static qualities of a 3D scene, the 3D API can be used to implement browsers for it. Both depend on a tree-like representation of 3D reality, called a scene graph. Development has been slow, and interest, along with interest in VRML, seems to be at a low point. But there are other ideas for the API. For instance, it's possible to build user interfaces in 3D (with drag-and-drop) for business and other applications. Also, the Space Science and Engineering Center (SSEC) and NCSA have developed a class library called VisAD on top of the Java 3D for scientific modelling applications.

Many games, both applets and applications, have been developed with Java, though C++ is still a much more popular alternative for commercial-grade games. One problem with creating Java games is that the garbage collection thread is largely out of the developer's control, and can interrupt smooth animations. However, J2SE 1.4 has enhanced the 2D API with many useful features for gaming, especially the ability to write images directly in VRAM. Why is this good? A common technique to eliminate flickering in games is called double buffering, where each image (frame) is composed offscreen and "blitted" to the screen all at once. If the frame is stored in VRAM rather than ordinary system memory, things are even better because it can be sent to the screen much more quickly — by just switching the video pointer (page flipping).

Furthermore, with the growing popularity of Java on mobile devices, the possibility of Java becoming a games platform seems to have gained a new lease of life. It is now possible to download many Java games for Nokia, Siemens, and other types of cellphones.

RESOURCES

Features of the Java 2D API

Mobile phone games implemented in Java


PacDasher, my Pac-Man clone
at SourceForge