2
* @(#)MultiIcon.java 1.0.1 2006-02-14
4
* Copyright (c) 2005 Werner Randelshofer
5
* Staldenmattweg 2, Immensee, CH-6405, Switzerland.
8
* This software is the confidential and proprietary information of
9
* Werner Randelshofer. ("Confidential Information"). You shall not
10
* disclose such Confidential Information and shall use it only in
11
* accordance with the terms of the license agreement you entered into
12
* with Werner Randelshofer.
15
package org.pushingpixels.substance.internal.contrib.randelshofer.quaqua;
19
import javax.swing.plaf.*;
21
import org.pushingpixels.substance.internal.contrib.randelshofer.quaqua.util.*;
25
* An icon which paints one out of multiple icons depending on the state
27
* MultiIcon can lazily create the icons from a tiled image.
29
* @author Werner Randelshofer
30
* @version 1.0.1 2006-02-14 Created tileCount icons from image.
31
* <br>1.0 October 17, 2005 Created.
33
public abstract class MultiIcon implements Icon {
35
* The icons from which we choose from.
36
* This variable is null, if we are using a tiled image as our base.
38
protected Icon[] icons;
40
/** Holds the icon pictures in a single image. This variable is used only
41
*until we create the icons array. Then it is set to null.
43
private Image tiledImage;
45
* The number of icons in the tiledImage.
47
private int tileCount;
49
* Whether the tiledImage needs to be tiled horizontaly or vertically
50
* to get the icons out of it.
52
private boolean isTiledHorizontaly;
56
* Creates a new instance from an array of icons.
57
* All icons must have the same dimensions.
58
* If an icon is null, an icon is derived for the state from the
61
public MultiIcon(Icon[] icons) {
63
generateMissingIcons();
67
* Creates a new instance from an array of images.
68
* All icons must have the same dimensions.
69
* If an icon is null, an icon is derived for the state from the
72
public MultiIcon(Image[] images) {
73
this.icons = new Icon[images.length];
74
for (int i=0, n = icons.length; i < n; i++) {
75
if (images[i] != null) {
76
icons[i] = new ImageIcon(images[i]);
79
generateMissingIcons();
83
* Creates a new instance.
84
* The icon representations are created lazily from the tiled image.
86
public MultiIcon(Image tiledImage, int tileCount, boolean isTiledHorizontaly) {
87
this.tiledImage = tiledImage;
88
this.tileCount = tileCount;
89
this.isTiledHorizontaly = isTiledHorizontaly;
94
public int getIconHeight() {
95
generateIconsFromTiledImage();
96
return icons[0].getIconHeight();
100
public int getIconWidth() {
101
generateIconsFromTiledImage();
102
return icons[0].getIconWidth();
106
public void paintIcon(java.awt.Component c, java.awt.Graphics g, int x, int y) {
107
generateIconsFromTiledImage();
108
Icon icon = getIcon(c);
110
icon.paintIcon(c, g, x, y);
114
private void generateIconsFromTiledImage() {
116
icons = new Icon[tileCount];
117
Image[] images = Images.split(tiledImage, tileCount, isTiledHorizontaly);
118
for (int i=0, n = Math.min(images.length, icons.length); i < n; i++) {
119
if (images[i] != null) {
120
icons[i] = new ImageIcon(images[i]);
123
generateMissingIcons();
128
protected abstract Icon getIcon(Component c);
129
protected abstract void generateMissingIcons();