1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.cosylab.gui.components;
21
22 import java.awt.BorderLayout;
23 import java.awt.Color;
24 import java.awt.Dimension;
25 import java.awt.GradientPaint;
26 import java.awt.Graphics;
27 import java.awt.Graphics2D;
28 import java.awt.image.BufferedImage;
29
30 import javax.swing.Icon;
31 import javax.swing.JFrame;
32
33 import com.cosylab.gui.components.util.ColorHelper;
34
35
36
37
38
39
40
41
42
43
44
45 public class GradientLabel extends ResizableTextLabel
46 {
47 private static final long serialVersionUID = 1L;
48 private Color backgroundOther = ColorHelper.getControlShadow();
49 private BufferedImage daBuffer = null;
50 private double endX = 1;
51 private double endY = 0;
52 private boolean gradientEnabled = true;
53 private double startX = 0.2f;
54 private double startY = 0;
55
56
57
58
59 public GradientLabel()
60 {
61 super();
62 setOpaque(true);
63 }
64
65
66
67
68
69
70 public GradientLabel(Icon image)
71 {
72 super(image);
73 setOpaque(true);
74 }
75
76
77
78
79
80
81
82 public GradientLabel(Icon image, int horizontalAlignment)
83 {
84 super(image, horizontalAlignment);
85 setOpaque(true);
86 }
87
88
89
90
91
92
93
94
95 public GradientLabel(String text)
96 {
97 super(text);
98 setOpaque(true);
99 }
100
101
102
103
104
105
106
107
108 public GradientLabel(String text, Icon icon, int horizontalAlignment)
109 {
110 super(text, icon, horizontalAlignment);
111 setOpaque(true);
112 }
113
114
115
116
117
118
119
120 public GradientLabel(String text, int horizontalAlignment)
121 {
122 super(text, horizontalAlignment);
123 setOpaque(true);
124 }
125
126 private void clearBuffer()
127 {
128 synchronized (this) {
129 daBuffer = null;
130 }
131 }
132
133
134
135
136
137
138
139
140 public Color getBackgroundStart()
141 {
142 return backgroundOther;
143 }
144
145
146
147
148
149
150 public boolean isGradientEnabled()
151 {
152 return gradientEnabled;
153 }
154
155
156
157
158 protected void paintComponent(Graphics g)
159 {
160 boolean oldOpaque = isOpaque();
161
162 if (oldOpaque && (isGradientEnabled())) {
163 int w = getWidth();
164 int h = getHeight();
165
166 synchronized (this) {
167 if ((daBuffer == null) || (daBuffer.getWidth() != w)
168 || (daBuffer.getHeight() != h)) {
169 daBuffer = new BufferedImage(w, h,
170 BufferedImage.TYPE_3BYTE_BGR);
171
172 Graphics2D g2d = daBuffer.createGraphics();
173 g2d.setPaint(new GradientPaint((int)(startX * w),(int)(startY * h),
174 getBackgroundStart(), (int)(endX * w), (int)(endY * h),
175 getBackground()));
176 g2d.fillRect(0, 0, w, h);
177 }
178
179 g.drawImage(daBuffer, 0, 0, this);
180 }
181 }
182
183 if (isGradientEnabled()) {
184 setOpaque(false);
185 }
186
187 super.paintComponent(g);
188 super.setOpaque(oldOpaque);
189 }
190
191
192
193
194
195
196
197
198
199
200 public void setBackground(Color c)
201 {
202 if (!c.equals(getBackground())) {
203 clearBuffer();
204 super.setBackground(c);
205 }
206 }
207
208
209
210
211
212
213
214
215 public void setBackgroundStart(Color newBackgroundStart)
216 {
217 if (newBackgroundStart != null
218 && !newBackgroundStart.equals(backgroundOther)) {
219 Color oldBackgroundStart = backgroundOther;
220 backgroundOther = newBackgroundStart;
221 clearBuffer();
222 repaint();
223 firePropertyChange("backgroundStart", oldBackgroundStart,
224 newBackgroundStart);
225 }
226 }
227
228
229
230
231
232
233
234
235 public void setGradientEnabled(boolean gradientEnabled)
236 {
237 this.gradientEnabled = gradientEnabled;
238 clearBuffer();
239 }
240
241
242
243
244
245
246
247
248
249
250
251 public void setGradientPoints(double startX, double startY, double endX,
252 double endY)
253 {
254 this.startX = startX;
255 this.startY = startY;
256 this.endX = endX;
257 this.endY = endY;
258 clearBuffer();
259 }
260
261 public static void main(String[] args) {
262 JFrame f = new JFrame();
263 f.getContentPane().setLayout(new BorderLayout());
264 f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
265 f.setSize(new Dimension(200,100));
266 f.getContentPane().add(new GradientLabel());
267 f.setVisible(true);
268 }
269 }
270
271