ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

Prototyping Desktop Applications Prototyping Desktop Applications

by Andrei Cioroianu

Since you are reading this article, you have probably decided to use Java on desktops. If you haven't made this decision yet, you could read my previous article, where I talked about the benefits that Java provides on the client side. In this article, I'll start presenting a desktop application prototype, which I called JImaging. I'm going to focus on the application's architecture, explaining how I made my technical decisions and how I solved the problems that occurred during development. If you are new to Java desktop development, you should start with The Java Tutorial. You can also find many interesting articles on The Swing Connection.

Why Would You Build a Prototype?

The development of most applications usually starts with a prototype for several reasons. First of all, you have to make sure that the user's requirements can be satisfied using existing technologies. For example, Windows integration cannot be implemented in a Swing application without using native code, which leads to the loss of Java's cross-platform advantage. SWT provides a limited integration with the operating system, while allowing you to run the same application on most native platforms. In many cases, however, the J2SE platform, with its rich set of features (Swing, Java 2D, Image I/O, etc.), provides everything you need for building complex desktop applications. Before committing to a big Java desktop project, you should always build a prototype to see if J2SE satisfies the application's needs.

In addition to proving that your ideas can be implemented and your technical decisions are correct, a prototype can be used to gather user feedback very early in the development process. The prototype can also help you to estimate the time and resources needed to complete your project. It takes a lot of work to build a polished user interface with menus, dialogs, drag-and-drop features, clipboard support, undo management, printing, etc. Before starting all of this work, you should find out how difficult it will be to build the core functionality of your application. If you have to use third-party custom components, you should test them to see if they work properly together. If you have to solve scalability or performance problems, you should find the solutions during the prototyping phase.

User Requirements

The JImaging prototype (see "Resources" below for the source) is a desktop application that lets you annotate images. Email might be the most popular "collaboration tool," but the ability to comment on screenshots is enhanced by a graphic tool that lets you draw lines, rectangles, ellipses, and text notes on an image.

Java is a natural choice for such an application if its users are working with more than one operating system. While Windows dominates the desktop market, there are user groups where the Mac or Linux are preferred. For example, when Java developers are working together on a project over the Internet, there are good chances they aren't using the same operating system.

The user interface is very simple, containing a toolbar and a drawing area, which is sufficient for testing the main functionality of the application. Figure 1 shows what this looks like.

JImaging Prototype
Figure 1. JImaging prototype, being used to annotate its own screenshot

Packages and Classes

Figure 2 shows the arrangement of the code for the prototype. The root package of the application contains only the Main class, which is described in the next section. I'll present the rest of the classes in future articles of this series.

The frames package contains classes that represent the application's main frame, the main panel that is based on JDesktopPane, and the text notes based on JInternalFrame. These three classes are named MainFrame, MainPanel, and NoteFrame.

The paint package groups the PaintView component, its data model (named PaintModel), and the ToolBarBuilder class that creates the application's toolbar. The tools sub-package has a set of tool classes that paint the graphic objects.

The ResourcesSupport class of the resources package is a utility class for handling the ToolBarResources.properties resource bundle and image icons such as those from the images directory.

Prototype Tree
Figure 2. The packages and classes of the prototype

The Main Class

This class implements the application's main() method and was packed together with all other classes and resources in a JAR file named JImaging.jar using the following command:

jar cfm JImaging.jar m.txt com

The com directory contains the packages with their classes, .properties resources, and .gif image icons. The m.txt file indicates the main class of the application with

Main-Class: com.devsphere.articles.desktop.Main

The jar tool copies the content of the m.txt file into the META-INF/manifest.mf file created automatically in JImaging.jar.

The following import declarations indicate the classes used by Main:

package com.devsphere.articles.desktop;
import com.devsphere.articles.desktop.frames.MainFrame;
import com.devsphere.articles.desktop.frames.MainPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;
import java.util.logging.Logger;

The main() method calls the Main() constructor, sets the system look and feel of Swing, creates the main window, and then shows it:

public class Main {
    private String args[];
    private MainFrame mainFrame;
    private MainPanel mainPanel;
    private Main(String args[]) {
        this.args = args;
    public static void main(String args[]) {
        Main main = new Main(args);

The command line may contain one or two arguments. The user can specify the path of a source image as the first argument in the command line. The application loads and shows the source image, allowing the user to annotate it. If the second argument is present, the application saves the annotated image to a file whose path is specified in the command line. To launch the application, run the following command:

java   -jar JImaging.jar   sourceImage  annotatedImage

J2SE 1.4 can load GIF, JPEG, and PNG files, but it can save images using only the JPEG and PNG formats. You may not use the GIF format to save the annotated image.

The remainder of this section describes the methods of the Main class.

Pages: 1, 2, 3

Next Pagearrow