![]() ![]() ![]() ![]() |
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 thepaint
method, and sometimes theupdate
method. In contrast, Swing components should do all of their drawing in thepaintComponent
method. If you implementpaintComponent
then you must callsuper.paintComponent
first 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
paint
method and no Swing component should override or implement theupdate
method.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 removedoffImage
andoffGraphics
and 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
JPanel
and making the panel have a border. The AWT programCoordinatesDemo
uses a class calledFramedArea
that exists solely to put a frame around the coordinate area. The Swing version of this program,CoordinatesDemo
, uses a border instead and deletes theFramedArea
class.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 |