View Javadoc

1   /*
2    * Copyright (c) 2003-2008 by Cosylab d. d.
3    *
4    * This file is part of CosyBeans-Common.
5    *
6    * CosyBeans-Common is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * CosyBeans-Common is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU General Public License
17   * along with CosyBeans-Common.  If not, see <http://www.gnu.org/licenses/>.
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   * A label that paints a gradient as a background. User can specify gradient
38   * colors (one of them is the default label background) and direction (the
39   * default is from the upper left to the upper right corner) relative to the
40   * label's proportions.
41   *
42   * @author <a href="mailto:miha.kadunc@cosylab.com">Miha Kadunc</a>
43   * @version $id$
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  	 * Constructs an empty GradientLabel
58  	 */
59  	public GradientLabel()
60  	{
61  		super();
62  		setOpaque(true);
63  	}
64  
65  	/**
66  	 * Constructs a GradientLabel with the specified icon.
67  	 *
68  	 * @param image
69  	 */
70  	public GradientLabel(Icon image)
71  	{
72  		super(image);
73  		setOpaque(true);
74  	}
75  
76  	/**
77  	 * Constructor for GradientLabel.
78  	 *
79  	 * @param image
80  	 * @param horizontalAlignment
81  	 */
82  	public GradientLabel(Icon image, int horizontalAlignment)
83  	{
84  		super(image, horizontalAlignment);
85  		setOpaque(true);
86  	}
87  	
88  	
89  
90  	/**
91  	 * Constructs a GradientLabel with the specified text.
92  	 *
93  	 * @param text
94  	 */
95  	public GradientLabel(String text)
96  	{
97  		super(text);
98  		setOpaque(true);
99  	}
100 
101 	/**
102 	 * Constructor for GradientLabel.
103 	 *
104 	 * @param text
105 	 * @param icon
106 	 * @param horizontalAlignment
107 	 */
108 	public GradientLabel(String text, Icon icon, int horizontalAlignment)
109 	{
110 		super(text, icon, horizontalAlignment);
111 		setOpaque(true);
112 	}
113 
114 	/**
115 	 * Constructor for GradientLabel.
116 	 *
117 	 * @param text
118 	 * @param horizontalAlignment
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 	 * Returns the backgroundStart, this is the color that is used for the
135 	 * start of label's gradient. The end color is that of the label's
136 	 * background.
137 	 *
138 	 * @return Color
139 	 */
140 	public Color getBackgroundStart()
141 	{
142 		return backgroundOther;
143 	}
144 
145 	/**
146 	 * Returns whether the gradient background is enabled.
147 	 *
148 	 * @return boolean
149 	 */
150 	public boolean isGradientEnabled()
151 	{
152 		return gradientEnabled;
153 	}
154 
155 	/**
156 	 * @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
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 	 * Sets the background color. This color is used for the gradient's end
193 	 * point.
194 	 *
195 	 * @param c The color to be set
196 	 *
197 	 * @see java.awt.Component#setBackground(Color)
198 	 * @see #setBackgroundStart(Color)
199 	 */
200 	public void setBackground(Color c)
201 	{
202 		if (!c.equals(getBackground())) {
203 			clearBuffer();
204 			super.setBackground(c);
205 		}
206 	}
207 
208 	/**
209 	 * Sets the backgroundStart color.
210 	 *
211 	 * @param newBackgroundStart The backgroundStart to set
212 	 *
213 	 * @see #getBackgroundStart()
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 	 * Sets the gradientEnabled property.
230 	 *
231 	 * @param gradientEnabled The gradientEnabled to set
232 	 *
233 	 * @see #isGradientEnabled()
234 	 */
235 	public void setGradientEnabled(boolean gradientEnabled)
236 	{
237 		this.gradientEnabled = gradientEnabled;
238 		clearBuffer();
239 	}
240 
241 	/**
242 	 * Sets the start and end point of the label's gradient, relative to the
243 	 * label's size. Value 0 is the left/top edge of the label. 1 is the
244 	 * right/bottom edge of the label.
245 	 *
246 	 * @param startX the x coordinate of the start of the gradient
247 	 * @param startY the y coordinate of the start of the gradient
248 	 * @param endX the x coordinate of the end of the gradient
249 	 * @param endY the y coordinate of the end of the gradient
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 /* __oOo__ */