Icons
Swing
introduces the concept of an icon for use in a variety of components.
The Icon interface and
ImageIcon class make dealing with simple images
extremely easy.
The Icon Interface
The Icon
interface is very simple, specifying just three methods used to
determine the size of the Icon and to display it.
Implementors of this interface are free to store and display the
image in any way, providing a great deal of flexibility. In other
words, icons don’t have to be bitmaps or GIF images, but are
free to render themselves any way they choose; as we’ll see
later, an icon can simply draw on the component if that’s more
efficient. The examples at the end of this section show a couple of
different ways the interface might be implemented.
Properties
The Icon interface defines the properties listed
in Table 4.3. The iconHeight
and iconWidth properties specify the size of the
Icon in pixels.
Table 4-3. Icon Properties
|
Property |
Data Type |
get |
is |
set |
bound |
Default Value |
|---|---|---|---|---|---|---|
|
|
|
• | ||||
|
|
|
• |
Method
- public abstract void paintIcon(Component c, Graphics g, int x, int y)
Paints the
Iconat the specified location on the givenGraphics. TheComponentis provided to allow its properties (such as foreground or background color) to be used when painting, or to allow the component to be used as an image observer (see Section 4.2.2, later in this chapter).
Implementing Your Own Icons
Here’s a class that implements the Icon interface and uses ovals as simple ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access