|
|
Converting to Swing |
Empty Space
At least with the default look and feel (Java Look & Feel), the Swing components tend to be tighter (that is, they contain less empty space). To get a sparser look, you might have to add empty borders to some components or margins to text components. Another alternative is to programmatically set the look and feel to one you like. See How to Set the Look and Feel.
Converting Painting Code
AWT components performed drawing in thepaintmethod, and sometimes theupdatemethod. In contrast, Swing components should do all of their drawing in thepaintComponentmethod. If you implementpaintComponentthen you must callsuper.paintComponentfirst thing. If you don't, even if the component is opaque it won't automatically paint its background. See Overview of Custom Paintingfor details. Most Swing components should not override or implement the
paintmethod and no Swing component should override or implement theupdatemethod.Note that Swing components automatically use double-buffering to increase performance. If the program that you're converting implements double-buffering explicitly, this is a unique opportunity to delete that code! For example this AWT animation program,
ImageSequence.java, draws to an off-screen image then paints the image to the screen all at once. Its Swing counterpart,ImageSequenceTimer.java, simply paints directly to the custom component, which handles double buffering automatically. During the conversion process, we removedoffImageandoffGraphicsand code that referred to them.If your painting code draws a title or draws edges around the component, consider replacing it with a border
. For example, you an easily create a box around a group of components by adding the components to a
JPaneland making the panel have a border. The AWT programCoordinatesDemouses a class calledFramedAreathat exists solely to put a frame around the coordinate area. The Swing version of this program,CoordinatesDemo, uses a border instead and deletes theFramedAreaclass.Converting Images
[PENDING: image icon basically wraps anImage. So why use image icons instead of images? Because Swing components are decorated with icons not images. ImageIcon class manages the tracking relationship when loading the image from its source. You can specify a description for an icon.]Thread Issues
[PENDING: Consider using a Timer instead of a Thread.]
|
|
Converting to Swing |