KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > gui > base > IconDecorator


1 package org.columba.core.gui.base;
2
3 import java.awt.Component JavaDoc;
4 import java.awt.Graphics JavaDoc;
5
6 import javax.swing.Icon JavaDoc;
7
8 public class IconDecorator implements Icon JavaDoc {
9
10     private Icon JavaDoc originalIcon;
11
12     private Icon JavaDoc decorationIcon;
13
14     private int xDiff;
15
16     private int yDiff;
17
18     private Location location;
19
20     // Java 1.5 enumeration
21
public enum Location {
22         UPPER_LEFT, UPPER_RIGHT, LOWER_LEFT, LOWER_RIGHT
23     };
24
25     public IconDecorator(Icon JavaDoc original, Icon JavaDoc decoration) {
26         this(original, decoration, Location.LOWER_LEFT);
27     }
28
29     public IconDecorator(Icon JavaDoc original, Icon JavaDoc decoration, Location loc) {
30
31         this.location = loc;
32         this.originalIcon = original;
33         this.decorationIcon = decoration;
34         if (decoration.getIconHeight() > original.getIconHeight()
35                 || decoration.getIconWidth() > original.getIconWidth()) {
36             throw new IllegalArgumentException JavaDoc(
37                     "Decoration must be smaller than the original");
38         }
39         this.xDiff = originalIcon.getIconWidth()
40                 + decorationIcon.getIconWidth()/4;
41         this.yDiff = originalIcon.getIconHeight() - decorationIcon.getIconHeight();
42     }
43
44     public int getIconHeight() {
45         return originalIcon.getIconHeight();
46     }
47
48     public int getIconWidth() {
49         return originalIcon.getIconWidth();
50     }
51
52     public void paintIcon(Component JavaDoc owner, Graphics JavaDoc g, int x, int y) {
53         // paint original first
54
originalIcon.paintIcon(owner, g, x, y);
55
56         int decorationX = x;
57         int decorationY = y;
58         // augment x.
59
if (location == Location.UPPER_RIGHT
60                 || location == Location.LOWER_RIGHT) {
61             decorationX += xDiff;
62         }
63         // augment y.
64
if (location == Location.LOWER_LEFT || location == Location.LOWER_RIGHT) {
65             decorationY += yDiff;
66         }
67
68         decorationIcon.paintIcon(owner, g, decorationX, decorationY);
69     }
70
71 }
Popular Tags