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.
Posted by
Thumbnail Icon
In the last two posts, we presented StretchIcon and ShrinkIcon, both used for scaling images to fit a component whose size is determined by the size and layout of its container. ThumbnailIcon extends ShrinkIcon to reduce an image when needed to fit within a defined size, padding and centering it horizontally or vertically as required. Like ShrinkIcon, this class does not magnify the image.
Shrink Icon
When writing about StretchIcon, published earlier, I mentioned that the image chosen for a StretchIcon should be large enough that it does not get magnified in fitting it to the component, since drawing an image larger than its natural size can lead to pixelation. ShrinkIcon extends StretchIcon to ensure such pixelation doesn’t happen.
Stretch Icon
An implementation of the Icon interface reports its size via the two methods getIconWidth() and getIconHeight(). It’s normal to expect that the Icon’s paintIcon(Component, Graphics, int, int) method will respect these bounds. If I had designed Swing’s interaction with Icons, I would have made sure of that by setting a clip to the Graphics passed to paintIcon(…).
Luckily for me, I didn’t design Swing, as that would have made StretchIcon impossible.
Keeping Menus Open
The default behavior of menus on all platforms, not just in Java, is for the menu to disappear when any item is selected. But once in a way, one comes across a situation where keeping the menu open could improve the user experience. For example, in the “Quick Preferences” submenu in Opera browser, a user who wants to set more than one option is forced to navigate the menu repeatedly.
Fortunately, the flexibility afforded in terms of extending the JDK classes makes this possible in Java.
Continue reading