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 Components for more information.
You can also use the setMargin
method
for text components.
AWT components perform painting in thepaint
andupdate
methods. Swing components use thepaintComponent
method instead. To take advantage of automatic background painting, your implementation ofpaintComponent
should callsuper.paintComponent
, first thing. See Overview of Custom Painting for 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.java
paints 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'spaintComponent
method, 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
JPanel
and making the panel have a border. The AWT-based 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.
Although you can useImage
objects in Swing programs, you might want to convert some or all of them intoIcon
objects. 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 usingIcon
objects. Another reason is thatIcon
objects have accessibility support built in.You can convert an
Image
into an icon in one of two ways. The first way is to usenew ImageIcon(anImage)
. The second is to create anImageIcon
using 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 |