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 is very simple, specifying just three methods
used to determine the size of the Icon and display it. Implementations 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 | Default value |
|---|---|---|---|---|---|
| | · | |||
| | · |
Method
- public void paintIcon(Component c, Graphics g, int x, int y)
Paint the
Iconat the specified location on the givenGraphics. For efficiency reasons, theGraphicsobject will (probably) not be clipped, so be sure not to draw “outside the lines.” You must make sure to keep your horizontal position betweenxandx+getIconWidth( )-1, and your vertical position betweenyandy+getIconHeight( )-1while painting. TheComponentis provided to allow its properties (such as foreground or background color) to be ...
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