KickJava   Java API By Example, From Geeks To Geeks.

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


1 //The contents of this file are subject to the Mozilla Public License Version 1.1
2
//(the "License"); you may not use this file except in compliance with the
3
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
4
//
5
//Software distributed under the License is distributed on an "AS IS" basis,
6
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
7
//for the specific language governing rights and
8
//limitations under the License.
9
//
10
//The Original Code is "The Columba Project"
11
//
12
//The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
13
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
14
//
15
//All Rights Reserved.
16
package org.columba.core.gui.base;
17
18 import java.awt.Color JavaDoc;
19 import java.awt.Graphics2D JavaDoc;
20 import java.awt.image.BufferedImage JavaDoc;
21
22 import javax.swing.Icon JavaDoc;
23 import javax.swing.ImageIcon JavaDoc;
24
25
26 /**
27  * A <code>JComboBox</code> item that represent different types of color.
28  *
29  * @author redsolo
30  */

31 public class ColorItem {
32     private Color JavaDoc itemColor;
33     private String JavaDoc itemName;
34     private Icon JavaDoc itemIcon;
35
36     /**
37  * Creates a color item with the specified color and name.
38  * @param color the color to represent.
39  * @param name the name of the color.
40  */

41     public ColorItem(Color JavaDoc color, String JavaDoc name) {
42         setColor(color);
43         itemName = name;
44     }
45
46     /**
47  * @return the color.
48  */

49     public Color JavaDoc getColor() {
50         return itemColor;
51     }
52
53     /**
54  * Set a new color.
55  * This method recreates a new icon to represent the color.
56  * @param color the new color.
57  */

58     public final void setColor(Color JavaDoc color) {
59         itemColor = color;
60         itemIcon = createIcon(color);
61     }
62
63     /**
64  * @return the name of the Color
65  */

66     public String JavaDoc getName() {
67         return itemName;
68     }
69
70     /**
71  * @return the icon for this ComboBox item.
72  */

73     public Icon JavaDoc getIcon() {
74         return itemIcon;
75     }
76
77     /**
78  * Creates and returns an icon that represents the specified color.
79  * @param color create an icon for this color.
80  * @return an Icon.
81  */

82     private Icon JavaDoc createIcon(Color JavaDoc color) {
83         int width = 45;
84         int height = 14;
85         BufferedImage JavaDoc image = new BufferedImage JavaDoc(width, height,
86                 BufferedImage.TYPE_INT_ARGB);
87
88         Graphics2D JavaDoc graphics = (Graphics2D JavaDoc) image.getGraphics();
89         graphics.setColor(Color.black);
90         graphics.drawRect(1, 1, width - 3, height - 3);
91         graphics.setColor(color);
92         graphics.fillRect(2, 2, width - 4, height - 4);
93         graphics.dispose();
94
95         return new ImageIcon JavaDoc(image);
96     }
97 }
98
Popular Tags