|      | Start of Tutorial > Start of Trail > Start of Lesson | Search Feedback Form | 
 
This section gives you conversion tips for dealing with empty space, painting, and images.
If you use the Java Look & Feel, your Swing program might have less space between components than the previous version. If you want to add more space, you can do it using any combination of layout managers, empty borders, and invisible spacer components. See Putting Space Between Componentsfor more information. You can also use the
setMarginmethod for text components.
AWT components perform painting in thepaintandupdatemethods. Swing components use thepaintComponentmethod instead. To take advantage of automatic background painting, your implementation ofpaintComponentshould callsuper.paintComponent, first thing. See Overview of Custom Paintingfor details.
Note that Swing components automatically use double buffering to make their painting smooth. If the program that you're converting implements double buffering explicitly, this is a unique opportunity to delete some code! For example the AWT-based animation program
ImageSequence.javapaints to an off-screen image and then paints the image to the screen all at once. Its Swing counterpart,
ImageSequenceTimer.java, simply puts its paint code in the custom component's
paintComponentmethod, and double buffering is handled automatically. During the conversion process, we removedoffImage,offGraphics, and all code that referred to them.If your painting code puts a title or edges around the component, consider replacing it with a border
. For example, you can easily create a box around a group of components by adding the components to a
JPaneland making the panel have a border. The AWT-based programCoordinatesDemouses a class called
FramedAreathat exists solely to put a frame around the coordinate area. The Swing version of this program,CoordinatesDemo, uses a border instead and deletes the
FramedAreaclass.
Although you can useImageobjects in Swing programs, you might want to convert some or all of them intoIconobjects. The main reason is Swing components that can display images -- labels, buttons, cell renderers in tables and trees and lists, tabbed panes, menus, and so on -- do so usingIconobjects. Another reason is thatIconobjects have accessibility support built in.You can convert an
Imageinto an icon in one of two ways. The first way is to usenew ImageIcon(anImage). The second is to create anImageIconusing the same data source that you used to create theImage. When you use the second way, the constructor automatically loads the image data, returning when the image is ready to use.
 
|      | Start of Tutorial > Start of Trail > Start of Lesson | Search Feedback Form | 
Copyright 1995-2002 Sun Microsystems, Inc. All rights reserved.