KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sshtools > ui > swing > ColorIcon


1 /*
2  * SSHTools - Java SSH2 API
3  *
4  * Copyright (C) 2002 Lee David Painter.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  *
11  * You may also distribute it and/or modify it under the terms of the
12  * Apache style J2SSH Software License. A copy of which should have
13  * been provided with the distribution.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * License document supplied with your distribution for more details.
19  *
20  */

21
22 package com.sshtools.ui.swing;
23
24 import java.awt.Color JavaDoc;
25 import java.awt.Component JavaDoc;
26 import java.awt.Dimension JavaDoc;
27 import java.awt.Graphics JavaDoc;
28 import javax.swing.Icon JavaDoc;
29
30 /**
31  *
32  *
33  * @author $author$
34  */

35 public class ColorIcon implements Icon JavaDoc {
36     // Private instance variables
37
private Dimension JavaDoc size;
38     private Color JavaDoc color;
39     private Color JavaDoc borderColor;
40
41     /**
42      * Creates a new ColorIcon object.
43      */

44     public ColorIcon() {
45         this(null);
46     }
47
48     /**
49      * Creates a new ColorIcon object.
50      *
51      * @param color
52      */

53     public ColorIcon(Color JavaDoc color) {
54         this(color, null);
55     }
56
57     /**
58      * Creates a new ColorIcon object.
59      *
60      * @param color
61      * @param borderColor
62      */

63     public ColorIcon(Color JavaDoc color, Color JavaDoc borderColor) {
64         this(color, null, borderColor);
65     }
66
67     /**
68      * Creates a new ColorIcon object.
69      *
70      * @param color
71      * @param size
72      * @param borderColor
73      */

74     public ColorIcon(Color JavaDoc color, Dimension JavaDoc size, Color JavaDoc borderColor) {
75         setColor(color);
76         setSize(size);
77         setBorderColor(borderColor);
78     }
79
80     /**
81      *
82      *
83      * @param c
84      * @param g
85      * @param x
86      * @param y
87      */

88     public void paintIcon(Component JavaDoc c, Graphics JavaDoc g, int x, int y) {
89         g.setColor((color == null) ? Color.white : color);
90         g.fillRect(x, y, getIconWidth(), getIconHeight());
91
92         if (borderColor != null) {
93             g.setColor(borderColor);
94             g.drawRect(x, y, getIconWidth(), getIconHeight());
95         }
96
97         
98         if(color == null) {
99             g.setColor(Color.black);
100             g.drawLine(x, y, x + getIconWidth(), y + getIconHeight());
101         }
102     }
103
104     /**
105      *
106      *
107      * @param size
108      */

109     public void setSize(Dimension JavaDoc size) {
110         this.size = size;
111     }
112
113     /**
114      *
115      *
116      * @param color
117      */

118     public void setColor(Color JavaDoc color) {
119         this.color = color;
120     }
121
122     /**
123      *
124      *
125      * @param borderColor
126      */

127     public void setBorderColor(Color JavaDoc borderColor) {
128         this.borderColor = borderColor;
129     }
130
131     /**
132      *
133      *
134      * @return
135      */

136     public int getIconWidth() {
137         return (size == null) ? 16 : size.width;
138     }
139
140     /**
141      *
142      *
143      * @return
144      */

145     public int getIconHeight() {
146         return (size == null) ? 16 : size.height;
147     }
148 }
Popular Tags