Java Tips Weblog

  • Blog Stats

    • 1,231,907 hits
  • Categories

  • Archives

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

TileIcon.java

Related Reading

Java API: javax.swing.Icon
Java API: javax.swing.ImageIcon
The Java™ Tutorials: How to Use Icons

About these ads

2 Responses to “Tile Icon”

  1. Jober said

    Hello, I am super beginner, could you post an example of using the class?
    Thank you….

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.

Join 95 other followers

%d bloggers like this: