File: DecoratedIcon.java

package info (click to toggle)
libswidgets-java 0.1.4-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, lenny, squeeze
  • size: 356 kB
  • ctags: 667
  • sloc: java: 3,436; xml: 64; makefile: 11
file content (105 lines) | stat: -rw-r--r-- 3,423 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// $Id: DecoratedIcon.java,v 1.1 2004/11/03 18:30:04 bobtarling Exp $

/*
 * DecoratedIcon.java
 *
 * Created on 25 February 2003, 20:47
 */

package org.tigris.swidgets;

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;
import javax.swing.UIManager;

/**
 * The base class for adding simple decorations to existing icons.
 * This should be extended for each decoration style.
 * @author Bob Tarling
 */
public abstract class DecoratedIcon extends ImageIcon {
    
    /**
     * If the icon is for hoovering: <code>ROLLOVER</code>
     */
    public static final int ROLLOVER = 0;
    
    /**
     * If the icon is the normally shown one: <code>STANDARD</code>
     */
    public static final int STANDARD = 1;

    /**
     * This is the sprite buffer for the arrow image of the left button.
     */
    private int[][] imageBuffer;

    private int popupIconWidth = 11;
    private int popupIconHeight = 16;
    private int popupIconOffset = 5;

    private ImageIcon imageIcon;
    
    /** Construct a decorated icon made up of the given icon and decorated with
     * the icon defined in the descendant class.
     * @param theImageIcon The icon to decorate
     */        
    DecoratedIcon(ImageIcon theImageIcon) {
        imageIcon = theImageIcon;
    }
    
    /**
     * Initialise the icon.
     * @param buffer the buffer containing the icon definition (pixels)
     */
    protected void init(int[][] buffer) {
        imageBuffer = buffer;
        popupIconWidth = imageBuffer[0].length;
        popupIconHeight = imageBuffer.length;
        BufferedImage mergedImage =
	    new BufferedImage(imageIcon.getIconWidth()
			      + popupIconOffset + popupIconWidth,
			      imageIcon.getIconHeight(),
			      BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2 = mergedImage.createGraphics();
        g2.drawImage(imageIcon.getImage(), null, null);
        setImage(mergedImage);
    }

    /** Paints the icon. The top-left corner of the icon is drawn at
     * the point (x, y) in the coordinate space of the graphics
     * context g. If this icon has no image observer, this method uses
     * the c component as the observer.
     *
     * @param c the component to be used as the observer if this icon
     * has no image observer
     * @param g the graphics context
     * @param x the X coordinate of the icon's top-left corner
     * @param y the Y coordinate of the icon's top-left corner
     */
    public void paintIcon(Component c, Graphics g, int x, int y) {
        super.paintIcon(c, g, x, y);

        int xOffset = x + imageIcon.getIconWidth() + popupIconOffset;
        // Initialize the color array
        Color[] colors = {
                c.getBackground(),
                UIManager.getColor("controlDkShadow"),
                UIManager.getColor("infoText"),
                UIManager.getColor("controlHighlight")};

        for (int i = 0; i < popupIconWidth; i++) {
            for (int j = 0; j < popupIconHeight; j++) {
                if (imageBuffer[j][i] != 0) {
                    g.setColor(colors[imageBuffer[j][i]]);
                    g.drawLine(xOffset + i, y + j, xOffset + i, y + j);
                }
            }
        }
    }
}