Subversion Repository Public Repository

Satyam

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/**
 * Copyright (c) 2012, Dhilshuk Reddy All rights reserved.
 * 
 * Permission to use, copy, modify, and distribute SwingJD software is freely
 * granted, provided that this notice is preserved.
 */
package com.bestray.healthcarecommonutil.util;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.geom.Ellipse2D;

import javax.swing.ButtonModel;
import javax.swing.JButton;


// TODO: Auto-generated Javadoc
/**
 * The Class GradientButton.
 *
 * @author Dhilshuk Reddy
 */
public class GradientButton extends JButton {

	/** The button theme. */
	private int buttonTheme = Theme.GRADIENT_SILVER_THEME;
	
	/** The roll over theme. */
	private int rollOverTheme = Theme.GRADIENT_RED_THEME;
	
	/** The selected theme. */
	private int selectedTheme = Theme.GRADIENT_BLACK_THEME;;
	
	/** The button type. */
	private String buttonType = ButtonType.BUTTON_ROUNDED_RECTANGLUR;
	
	/** The button color. */
	private GradientPaint buttonColor = null;

	/**
	 * Constructor which sets label of the button.
	 * 
	 * @param text
	 *            label on the button
	 */
	public GradientButton(String text) {
		super(text);
		init();
	}

	/**
	 * Constructor which sets the label and theme for the button.
	 * 
	 * @param text
	 *            label on the button
	 * @param buttonTheme
	 *            button theme.
	 */
	public GradientButton(String text, int buttonTheme) {
		super(text);
		this.buttonTheme = buttonTheme;
		init();
	}

	/**
	 * Constructor which sets the label and type for the button.
	 * 
	 * @param text
	 *            label on the button
	 * @param buttonType
	 *            shape of the button
	 */
	public GradientButton(String text, String buttonType) {
		super(text);
		this.buttonType = buttonType;
		init();
	}

	/**
	 * Constructor which sets the label,type and theme for the button.
	 *
	 * @param text label on the button
	 * @param buttonTheme theme of the button
	 * @param buttonType shape of the button
	 */
	public GradientButton(String text, int buttonTheme, String buttonType) {
		super(text);
		this.buttonTheme = buttonTheme;
		this.buttonType = buttonType;
		init();
	}

	/**
	 * Constructor which sets the label,type,theme and roll-over theme for the
	 * button.
	 * 
	 * @param text
	 *            label on the button
	 * @param buttonType
	 *            shape of the button
	 * @param buttonTheme
	 *            theme of the button
	 * @param rolloverTheme
	 *            roll-over theme
	 */
	public GradientButton(String text, String buttonType, int buttonTheme,
			int rolloverTheme) {
		super(text);
		this.buttonType = buttonType;
		this.buttonTheme = buttonTheme;
		this.rollOverTheme = rolloverTheme;
		init();
	}

	/**
	 * Constructor which sets the label,type,theme ,roll-over and selected theme
	 * for the button.
	 * 
	 * @param text
	 *            label on the button
	 * @param buttonType
	 *            shape of the button
	 * @param buttonTheme
	 *            theme of the button
	 * @param rolloverTheme
	 *            roll-over theme
	 * @param selectedTheme
	 *            selected theme
	 */
	public GradientButton(String text, String buttonType, int buttonTheme,
			int rolloverTheme, int selectedTheme) {
		super(text);
		this.buttonTheme = buttonTheme;
		this.buttonType = buttonType;
		this.rollOverTheme = rolloverTheme;
		this.selectedTheme = selectedTheme;
		init();
	}

	/**
	 * Initializes.
	 */
	private void init() {
		setFont(new Font("Thoma", Font.BOLD, 12));
		setContentAreaFilled(false);
		setBorderPainted(false);
		setFocusPainted(false);
	}

	/* (non-Javadoc)
	 * @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
	 */
	@Override
	public void paintComponent(Graphics g) {
		Graphics2D g2d = (Graphics2D) g.create();
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);
		int h = getHeight();
		int w = getWidth();
		int height = getHeight();
		ButtonModel model = getModel();
		if (model.isRollover()) {
			buttonColor = ColorUtils.getInStance().getGradientColor(
					this.rollOverTheme, height, this);

		} else {

			buttonColor = ColorUtils.getInStance().getGradientColor(
					this.buttonTheme, height, this);

		}
		if (model.isSelected() || model.isPressed()) {
			buttonColor = ColorUtils.getInStance().getGradientColor(
					this.selectedTheme, height, this);

		}
		drawShape(g2d, w, h);
		super.paintComponent(g);
		g2d.dispose();
	}

	/**
	 * Draws the shape.
	 * 
	 * @param g2d
	 *            2D Graphics object.
	 * @param w
	 *            width of the button
	 * @param h
	 *            height of the Button
	 */
	private void drawShape(Graphics2D g2d, int w, int h) {
		if (buttonType == ButtonType.BUTTON_ROUNDED_RECTANGLUR) {

			g2d.setPaint(buttonColor);
			g2d.fillRoundRect(0, 0, w, h, 10, 10);
			g2d.setPaint(new Color(100, 100, 100, 100));
			g2d.drawRoundRect(0, 0, w - 1, h - 1, 10, 10);
			g2d.setPaint(new Color(255, 255, 255, 50));
			g2d.drawRoundRect(1, 1, w - 3, h - 3, 10, 10);
		} else if (buttonType == ButtonType.BUTTON_RECTANGULAR) {
			g2d.setPaint(buttonColor);
			g2d.fillRect(1, 1, w - 2, h - 2);
			g2d.setPaint(new Color(100, 100, 100, 100));
			g2d.drawRect(0, 0, w - 1, h - 1);
		} else if (buttonType == ButtonType.BUTTON_OVAL) {
			g2d.setPaint(buttonColor);
			g2d.fillOval(1, 1, w - 20, h - 2);
			g2d.setPaint(new Color(100, 100, 100, 100));
			g2d.drawOval(0, 0, w - 20, h - 1);
		} else if (buttonType == ButtonType.BUTTON_ELLIPSE) {
			g2d.setPaint(buttonColor);
			Shape shape = new Ellipse2D.Double(1, 1, w - 2, h - 2);
			g2d.fill(shape);
			g2d.setPaint(new Color(100, 100, 100, 100));
			shape = new Ellipse2D.Double(0, 0, w - 1, h - 1);
			g2d.draw(shape);
		} else if (buttonType == ButtonType.BUTTON_CIRCULAR) {
			int size = Math.min(getWidth(), getHeight() - 2);
			g2d.setPaint(buttonColor);
			g2d.fillOval(2, 2, (size - 2 * 2), (size - 2 * 2));
			g2d.setStroke(new BasicStroke(2));
			g2d.setColor(new Color(100, 100, 100, 100));
			g2d.drawOval(2, 2, (size - 2 * 2), (size - 2 * 2));
		} else if (buttonType == ButtonType.BUTTON_ROUNDED) {
			g2d.setPaint(buttonColor);
			g2d.fillRoundRect(1, 1, w - 2, h - 2, h - 5, h - 5);
			g2d.setPaint(new Color(100, 100, 100, 100));
			g2d.drawRoundRect(0, 0, w - 1, h - 1, h - 3, h - 3);
		}
	}

	/**
	 * Draws Shadow.
	 * 
	 * @param g2
	 *            2D Graphics Object
	 * @param shadowWidth
	 *            width of shadow
	 * @param clipShape
	 *            shape
	 */
	private void paintBorderShadow(Graphics2D g2, int shadowWidth,
			Shape clipShape) {
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);
		int sw = shadowWidth * 2;
		for (int i = sw; i >= 2; i -= 2) {
			float pct = (float) (sw - i) / (sw - 1);
			g2.setColor(getMixedColor(Color.LIGHT_GRAY, pct, new Color(255,
					255, 255, 100), 1.0f - pct));
			g2.setStroke(new BasicStroke(5));
			g2.fill(clipShape);
		}
	}

	/**
	 * Returns the Color.
	 * 
	 * @param c1
	 *            Color
	 * @param pct1
	 *            float value
	 * @param c2
	 *            Color
	 * @param pct2
	 *            float value
	 * @return Color
	 */
	private static Color getMixedColor(Color c1, float pct1, Color c2,
			float pct2) {
		float[] clr1 = c1.getComponents(null);
		float[] clr2 = c2.getComponents(null);
		for (int i = 0; i < clr1.length; i++) {
			clr1[i] = (clr1[i] * pct1) + (clr2[i] * pct2);
		}
		return new Color(clr1[0], clr1[1], clr1[2], clr1[3]);
	}

	/**
	 * Returns the Selected Theme.
	 * 
	 * @return selected theme
	 */
	public int getSelectedTheme() {
		return selectedTheme;
	}

	/**
	 * Sets the selected theme.
	 * 
	 * @param selectedTheme
	 *            theme when the button is selected
	 */
	public void setSelectedTheme(int selectedTheme) {
		this.selectedTheme = selectedTheme;
	}

}

Commits for Satyam/AuroCareCommonUtil/src/main/java/com/bestray/healthcarecommonutil/util/GradientButton.java

Diff revisions: vs.
Revision Author Commited Message
1 girijabapi picture girijabapi Fri 20 Jul, 2018 05:59:17 +0000