1 package org.telscenter.pas.common.ui.button;
2
3 import java.awt.BorderLayout;
4 import java.awt.Color;
5 import java.awt.event.ActionEvent;
6 import java.awt.event.ActionListener;
7
8 import javax.swing.Action;
9 import javax.swing.Icon;
10 import javax.swing.JButton;
11 import javax.swing.JFrame;
12 import javax.swing.Timer;
13
14 public class FadeJButton extends JButton{
15 Color start, end;
16 float rint, gint, bint, rdelta, gdelta, bdelta;
17 float r, g, b;
18 float rstart, rend, gstart, gend, bstart, bend;
19 boolean forward;
20 float maxint;
21 String maxstr;
22
23 public FadeJButton(Color s, Color e){
24 this (s, e, null, null);
25 }
26
27 public FadeJButton(Color s, Color e, Action action){
28 this(s, e);
29 setAction(action);
30 }
31
32 public FadeJButton(Color s, Color e, Icon icon){
33 this(s, e, null, icon);
34 }
35
36 public FadeJButton(Color s, Color e, String text){
37 this(s, e, text, null);
38 }
39
40 public FadeJButton(Color s, Color e, String text, Icon icon){
41 super(text, icon);
42 start = s;
43 end = e;
44 setBackground(start);
45 forward = true;
46 Fader();
47 }
48
49 void Fader(){
50 rint = fval(start.getRed(), end.getRed());
51 gint = fval(start.getGreen(), end.getGreen());
52 bint = fval(start.getBlue(), end.getBlue());
53
54 maxint = Math.abs(rint); maxstr = "red";
55 if (maxint < Math.abs(gint)){
56 maxint = Math.abs(gint); maxstr = "green";
57 }
58 if (maxint < Math.abs(bint)){
59 maxint = Math.abs(bint); maxstr = "blue";
60 }
61
62 rdelta = rint / 100.0F;
63 gdelta = gint / 100.0F;
64 bdelta = bint / 100.0F;
65
66 rstart = fcolval(start.getRed());
67 gstart = fcolval(start.getGreen());
68 bstart = fcolval(start.getBlue());
69
70 rend = fcolval(end.getRed());
71 gend = fcolval(end.getGreen());
72 bend = fcolval(end.getBlue());
73
74 r = fcolval(start.getRed());
75 g = fcolval(start.getGreen());
76 b = fcolval(start.getBlue());
77
78 Timer t = new Timer(15, new ActionListener(){
79 public void actionPerformed(ActionEvent e){
80 setBackground(new Color(r, g, b));
81 if (r + g + b < 0.9){
82 setForeground(Color.white);
83 }
84 else{
85 setForeground(Color.black);
86 }
87 if (forward){
88 r += rdelta;
89 g += gdelta;
90 b += bdelta;
91 if (over(maxstr, r, g, b)){
92 r = rend;
93 g = gend;
94 b = bend;
95 forward = !forward;
96 }
97 }
98 else{
99 r -= rdelta;
100 g -= gdelta;
101 b -= bdelta;
102 if (under(maxstr, r, g, b)){
103 r = rstart;
104 g = gstart;
105 b = bstart;
106 forward = !forward;
107 }
108 }
109 }
110 });
111 t.start();
112 }
113
114 float fval(int colval0, int colval1){
115 float s = (float)colval0 / 255.0F;
116 float e = (float)colval1 / 255.0F;
117
118 return e - s;
119 }
120
121 float fcolval(int cv){
122 return (float)cv / 255.0F;
123 }
124
125 boolean over(String col, float r, float g, float b){
126 boolean retval;
127
128 if (col.equals("red")){
129 if (rint > 0){
130 retval = r > rend;
131 }
132 else{
133 retval = r < rend;
134 }
135 }
136 else if (col.equals("green")){
137 if (gint > 0){
138 retval = g > gend;
139 }
140 else{
141 retval = g < gend;
142 }
143 }
144 else{
145 if (bint > 0){
146 retval = b > bend;
147 }
148 else{
149 retval = b < bend;
150 }
151 }
152 return retval;
153 }
154
155 boolean under(String col, float r, float g, float b){
156 boolean retval;
157
158 if (col.equals("red")){
159 if (rint > 0){
160 retval = r < rstart;
161 }
162 else{
163 retval = r > rstart;
164 }
165 }
166 else if (col.equals("green")){
167 if (gint > 0){
168 retval = g < gstart;
169 }
170 else{
171 retval = g > gstart;
172 }
173 }
174 else{
175 if (bint > 0){
176 retval = b < bstart;
177 }
178 else{
179 retval = b > bstart;
180 }
181 }
182 return retval;
183 }
184
185 public static void main(String[] args){
186 JFrame frame = new JFrame();
187 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
188
189 FadeJButton fjb1 = new FadeJButton(Color.yellow, Color.cyan, "FadeButton");
190 FadeJButton fjb2 = new FadeJButton(Color.red, Color.black, "FadeButton");
191 frame.add(fjb1, BorderLayout.NORTH);
192 frame.add(fjb2, BorderLayout.SOUTH);
193 frame.setBounds(0, 0, 200, 200);
194 frame.setVisible(true);
195 }
196 }