Tile Icon
Posted by Darryl Burke on April 22, 2012
The usual way to repeat, or tile a background image across and down a GUI component is to write nested loops in a paintComponent(…) override. TileIcon extends ImageIcon to provide this functionality and can fill the background of a component that can display an Icon.
Like StretchIcon and ShrinkIcon published earlier, TileIcon fills the component area, excluding any border or insets. Like the other two classes, TileIcon is meant to be used only in conjunction with a component whose size is determined by the size and layout of the container in which it is placed.
TileIcon provides three tiling styles: the default style is to tile the image starting at the top left corner; the other two styles center the tile grid in the component, one by placing the center of a tile at the center and the other by placing the corners of four tiles at the center of the component. Since an Icon is a shared resource that does not retain a reference to any component(s) to which the Icon may be set, code that changes the tiling style of an already displayed TileIcon is responsible for repainting the component(s) affected by the change.
Like StretchIcon and ShrinkIcon, TileIcon is a drop-in replacement for ImageIcon, which it extends, except that ImageIcon’s no-arg constructor isn’t supported.
Get The Code
Related Reading
Java API: javax.swing.Icon
Java API: javax.swing.ImageIcon
The Java™ Tutorials: How to Use Icons
Jober said
Hello, I am super beginner, could you post an example of using the class?
Thank you….
Darryl Burke said
There’s a link above to the Oracle tutorial on How to Use Icons. Start there.
Darryl