View Javadoc

1   package org.telscenter.pas.common.ui.panel;
2   
3   import java.awt.Color;
4   import java.awt.Dimension;
5   import java.awt.GradientPaint;
6   import java.awt.Graphics;
7   import java.awt.Graphics2D;
8   import java.awt.Insets;
9   import java.awt.LayoutManager;
10  
11  import javax.swing.JPanel;
12  import javax.swing.UIManager;
13  
14  public class SimpleGradientPanel extends JPanel
15  {
16  	/***
17  	 * 
18  	 */
19  	private static final long serialVersionUID = 1L;
20  	public static final int VERTICAL = 0;
21  	public static final int HORIZONTAL = 1;
22  
23  	/***
24  	 * The start color of the gradient
25  	 */
26  	private Color startColor = Color.white;
27  
28  	/***
29  	 * The end color of the gradient
30  	 */
31  	private Color endColor = UIManager.getColor("control");
32  
33  	/***
34  	 * The direction of the gradient
35  	 */
36  	private int direction = VERTICAL;
37  
38  	
39  	
40  	/***
41  	 * @param layout
42  	 */
43  	public SimpleGradientPanel(LayoutManager layout) {
44  		super(layout);
45  	}
46  
47  	/***
48  	 * @param isDoubleBuffered
49  	 */
50  	public SimpleGradientPanel(boolean isDoubleBuffered) {
51  		super(isDoubleBuffered);
52  	}
53  
54  	/***
55  	 * @param layout
56  	 * @param isDoubleBuffered
57  	 */
58  	public SimpleGradientPanel(LayoutManager layout, boolean isDoubleBuffered) {
59  		super(layout, isDoubleBuffered);
60  	}
61  	
62  	
63  	// ------------------------------------------------------------------------------------------------------------------
64  	//  Constructors and getter/setter methods
65  	// ------------------------------------------------------------------------------------------------------------------
66  
67  	/***
68  	 * Create a default SimpleGradientPanel instance.
69  	 */
70  	public SimpleGradientPanel()
71  	{
72  		super();
73  	}
74  
75  	/***
76  	 * Create a SimpleGradientPanel with the given start and end colors.
77  	 *
78  	 * @param aStart The start color for the gradient.
79  	 * @param aEnd   The end color for the gradient.
80  	 */
81  	public SimpleGradientPanel(Color aStart, Color aEnd)
82  	{
83  		super();
84  		startColor = aStart;
85  		endColor = aEnd;
86  	}
87  
88  	/***
89  	 * Create a SimpleGradientPanel with the given start and end colors.
90  	 *
91  	 * @param aStart 		The start color for the gradient.
92  	 * @param aEnd   		The end color for the gradient.
93  	 * @param aDirection The direction of the gradient.
94  	 */
95  	public SimpleGradientPanel(Color aStart, Color aEnd, int aDirection)
96  	{
97  		super();
98  		startColor = aStart;
99  		endColor = aEnd;
100 		direction = aDirection;
101 	}
102 
103 	/***
104 	 * Get the ending color of the gradient.
105 	 */
106 	public Color getEndColor()
107 	{
108 		return endColor;
109 	}
110 
111 	/***
112 	 * Set the ending color to use.
113 	 *
114 	 * @param aColor The color to use.
115 	 */
116 	public void setEndColor(Color aColor)
117 	{
118 		Color oldEndColor = endColor;
119 		endColor = aColor;
120 		super.firePropertyChange("endColor",oldEndColor,endColor);
121 		repaint();
122 	}
123 
124 	/***
125 	 * Get the start color of the gradient
126 	 */
127 	public Color getStartColor()
128 	{
129 		return startColor;
130 	}
131 
132 	/***
133 	 * Set the starting color.
134 	 *
135 	 * @param aColor The color to use
136 	 */
137 	public void setStartColor(Color aColor)
138 	{
139 		Color oldStartColor = endColor;
140 		startColor = aColor;
141 		super.firePropertyChange("startColor",oldStartColor,startColor);
142 		repaint();
143 	}
144 
145 	/***
146 	 * Get the direction (vertical or horizontal) of the gradient.
147 	 */
148 	public int getDirection()
149 	{
150 		return direction;
151 	}
152 
153 	/***
154 	 * Set the direction
155 	 *
156 	 * @param aDirection The direction of the gradient
157 	 */
158 	public void setDirection(int aDirection)
159 	{
160 		int oldDirection = direction;
161 		direction = aDirection;
162 		super.firePropertyChange("direction",oldDirection,aDirection);
163 		repaint();
164 	}
165 
166 	// ------------------------------------------------------------------------------------------------------------------
167 	//  Custom painting methods
168 	// ------------------------------------------------------------------------------------------------------------------
169 
170 	/***
171 	 * Override the default paintComponent method to paint the gradient in the panel.
172 	 *
173 	 * @param g
174 	 */
175 	public void paintComponent(Graphics g)
176 	{
177 		Dimension dim = getSize();
178 		Graphics2D g2 = (Graphics2D) g;
179 		Insets inset = getInsets();
180 		int vWidth = dim.width - (inset.left + inset.right);
181 		int vHeight = dim.height - (inset.top + inset.bottom);
182 
183 		if (direction == HORIZONTAL)
184 		{
185 			paintHorizontalGradient(g2, inset.left, inset.top, vWidth, vHeight, dim.width);
186 		}
187 		else
188 		{
189 			paintVerticalGradient(g2, inset.left, inset.top, vWidth, vHeight, dim.height);
190 		}
191 	}
192 
193 	/***
194 	 * Paints a vertical gradient background from the start color to the end color.
195 	 */
196 	private void paintVerticalGradient(Graphics2D g2, int x, int y, int w, int h, int height)
197 	{
198 		g2.setPaint(new GradientPaint(0, y, startColor, 0, height, endColor));
199 		g2.fillRect(x, y, w, h);
200 	}
201 
202 	/***
203 	 * Paints a horizontal gradient background from the start color to the end color.
204 	 */
205 	private void paintHorizontalGradient(Graphics2D g2, int x, int y, int w, int h, int width)
206 	{
207 		g2.setPaint(new GradientPaint(x, 0, startColor, width, 0, endColor));
208 		g2.fillRect(x, y, w, h);
209 	}
210 }