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

advertisement

AddThis Social Bookmark Button

Prototyping Desktop Applications
Pages: 1, 2, 3

Setting the System Look and Feel

The following setSystemLookAndFeel() method calls the setLookAndFeel() method of the javax.swing.UIManager class.



It requests Swing to switch from the default look and feel, named Metal, to the native look and feel:

private void setSystemLookAndFeel() {
    try {
        UIManager.setLookAndFeel(
            UIManager.getSystemLookAndFeelClassName());
    } catch (UnsupportedLookAndFeelException x) {
        log(x);
    } catch (ClassNotFoundException x) {
        log(x);
    } catch (IllegalAccessException x) {
        log(x);
    } catch (InstantiationException x) {
        log(x);
    }
}

Normally, no exception is thrown by setLookAndFeel() because the parameter is guaranteed to have a valid value, since it's returned by the getSystemLookAndFeelClassName() method of javax.swing.UIManager. However, using the standard logging API, any exception would be logged as a severe error message:

private static void log(Exception x) {
    Logger.global.severe(x.getMessage());
}

Using the global logger is okay in the case of a prototype, but a production application would have to use its own logger, saving the error messages in a file.

Creating and Showing the Main Window

The createFrame() method creates a MainFrame instance, packs the frame, and loads the source image:

private void createFrame() {
    mainFrame = new MainFrame();
    mainPanel = mainFrame.getMainPanel();
    mainPanel.updateSize();
    mainFrame.pack();
    loadImage();
}

The invocation of updateSize() sets the preferred size of the main panel that is obtained with getMainPanel(). The pack() method causes the main frame to be sized to fit the preferred size of the main panel and the application's toolbar. Note that getMainPanel() and updateSize() are application methods implemented by the MainFrame and MainPanel classes. The pack() method is inherited from java.awt.Window.

The showFrame() method shows the application's main frame and invokes the requestFocus() method of the main panel. Without the requestFocus() call, the focus would be gained by the toolbar's zoom combo box, which is not the main component of the frame. When the application starts, its main component should have the focus, even if the main panel doesn't handle any keyboard events.

The setDefaultCloseOperation() call with the DO_NOTHING_ON_CLOSE parameter disables the default action that happens when the window is closed. The showFrame() method registers its own window listener in order to handle the window-closing event. When the user closes the main frame, the listener saves the annotated image, disposes the frame, and ends the execution of the application with System.exit(0):

private void showFrame() {
     mainFrame.setDefaultCloseOperation(
         MainFrame.DO_NOTHING_ON_CLOSE);
     mainFrame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent e) {
             saveImage();
             mainFrame.dispose();
             System.exit(0);
         }
     });
     mainFrame.show();
     mainPanel.requestFocus();
}

Loading and Saving Images

A finished product would use file dialogs to load the source image and save the annotated image. Ideally, the "File Open" dialog would let users preview images and the "File Save" dialog would allow them to provide various parameters, such as the compression quality of the saved image. The standard file dialogs of Swing are based on a component called JFileChooser, which can be customized with its setAccessory() method that lets you add your own component to the file dialog.

In the case of a prototype, the focus is on the main functionality. Therefore, instead of using a customized file dialog, the prototype gets the paths of the loaded and saved images from the command line. The simple read() and write() methods of the javax.imageio.ImageIO class are used to load and save the image. Note that the Image I/O API lets you find out which graphic formats are supported, and you can set parameters such as the compression quality. You would need these features for a customized file dialog.

The loadImage() method reads the image file, whose path is provided as the first command line argument, and sets the background image of the main panel:

private void loadImage() {
    if (args.length >= 1) 
        try {
            File file = new File(args[0]);
             BufferedImage image = ImageIO.read(file);
             mainPanel.getPaintView().getModel().setBackImage(image);
        } catch (IOException x) {
             log(x);
        }
}

The saveImage() method gets the annotated image of the main panel and writes this image to a file whose path is given as the second argument of the command line:

private void saveImage() {
     if (args.length >= 2)
         try {
             File file = new File(args[1]);
             String name = file.getName();
             int k = name.lastIndexOf('.') + 1;
             String ext = name.substring(k);
             BufferedImage image
                 = mainPanel.getAnnotatedImage();
             ImageIO.write(image, ext, file);
         } catch (IOException x) {
             log(x);
         }
}

Pages: 1, 2, 3

Next Pagearrow