1
+ /*
2
+ * MIT License
3
+ *
4
+ * Copyright (c) 2018 atharva washimkar, Vincent Palazzo
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in all
14
+ * copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ * SOFTWARE.
23
+ */
1
24
package mdlaf .animation ;
2
25
3
- import javax .swing .JComponent ;
4
- import javax .swing .Timer ;
26
+ import javax .swing .*;
5
27
import java .awt .Color ;
6
- import java .awt .event .ActionEvent ;
7
- import java .awt .event .ActionListener ;
8
- import java .awt .event .MouseEvent ;
9
- import java .awt .event .MouseListener ;
10
-
11
- public class MaterialUITimer implements MouseListener , ActionListener {
12
-
13
- private Color from , to ;
14
- private boolean forward ;
15
- private int alpha , steps ;
16
- private int [] forwardDeltas , backwardDeltas ;
17
-
18
- private JComponent component ;
19
- private Timer timer ;
20
-
21
- protected MaterialUITimer (JComponent component , Color to , int steps , int interval ) {
22
- this .from = component .getBackground ();
23
- this .to = to ;
24
-
25
- this .forwardDeltas = new int [4 ];
26
- this .backwardDeltas = new int [4 ];
27
-
28
- forwardDeltas [0 ] = (from .getRed () - to .getRed ()) / steps ;
29
- forwardDeltas [1 ] = (from .getGreen () - to .getGreen ()) / steps ;
30
- forwardDeltas [2 ] = (from .getBlue () - to .getBlue ()) / steps ;
31
- forwardDeltas [3 ] = (from .getAlpha () - to .getAlpha ()) / steps ;
32
-
33
- backwardDeltas [0 ] = (to .getRed () - from .getRed ()) / steps ;
34
- backwardDeltas [1 ] = (to .getGreen () - from .getGreen ()) / steps ;
35
- backwardDeltas [2 ] = (to .getBlue () - from .getBlue ()) / steps ;
36
- backwardDeltas [3 ] = (to .getAlpha () - from .getAlpha ()) / steps ;
37
-
38
- this .steps = steps ;
39
-
40
- this .component = component ;
41
- this .component .addMouseListener (this );
42
- timer = new Timer (interval , this );
43
- }
44
-
45
- private Color nextColor () {
46
- int rValue = from .getRed () - alpha * forwardDeltas [0 ];
47
- int gValue = from .getGreen () - alpha * forwardDeltas [1 ];
48
- int bValue = from .getBlue () - alpha * forwardDeltas [2 ];
49
- int aValue = from .getAlpha () - alpha * forwardDeltas [3 ];
50
-
51
- return new Color (rValue , gValue , bValue , aValue );
52
- }
53
-
54
- private Color previousColor () {
55
- int rValue = to .getRed () - (steps - alpha ) * backwardDeltas [0 ];
56
- int gValue = to .getGreen () - (steps - alpha ) * backwardDeltas [1 ];
57
- int bValue = to .getBlue () - (steps - alpha ) * backwardDeltas [2 ];
58
- int aValue = to .getAlpha () - (steps - alpha ) * backwardDeltas [3 ];
59
-
60
- return new Color (rValue , gValue , bValue , aValue );
61
- }
62
-
63
- @ Override
64
- public void mousePressed (MouseEvent me ) {
65
- if (!me .getComponent ().isEnabled ()){
66
- return ;
67
- }
68
- alpha = steps - 1 ;
69
- forward = false ;
70
- timer .start ();
71
-
72
- alpha = 0 ;
73
- forward = true ;
74
- timer .start ();
75
- }
76
-
77
- @ Override
78
- public void mouseReleased (MouseEvent me ) {
79
-
80
- }
81
-
82
- @ Override
83
- public void mouseClicked (MouseEvent me ) {
84
-
85
- }
86
-
87
- @ Override
88
- public void mouseExited (MouseEvent me ) {
89
- if (!me .getComponent ().isEnabled ()){
90
- return ;
91
- }
92
- alpha = steps - 1 ;
93
- forward = false ;
94
- timer .start ();
95
- }
96
-
97
- @ Override
98
- public void mouseEntered (MouseEvent me ) {
99
- if (!me .getComponent ().isEnabled ()){
100
- return ;
101
- }
102
- alpha = 0 ;
103
- forward = true ;
104
- timer .start ();
105
- }
106
-
107
- @ Override
108
- public void actionPerformed (ActionEvent ae ) {
109
- if (forward ) {
110
- component .setBackground (nextColor ());
111
- ++alpha ;
112
- }
113
- else {
114
- component .setBackground (previousColor ());
115
- --alpha ;
116
- }
117
-
118
- if (alpha == steps + 1 || alpha == -1 ) {
119
- timer .stop ();
120
- }
121
- }
122
- }
28
+ import java .awt .event .*;
29
+
30
+ public class MaterialUITimer implements MouseListener , ActionListener , MouseMotionListener {
31
+
32
+ private Color from , to ;
33
+ private boolean forward ;
34
+ private int alpha , steps ;
35
+ private int [] forwardDeltas , backwardDeltas ;
36
+
37
+ private JComponent component ;
38
+ private Timer timer ;
39
+
40
+ protected MaterialUITimer (JComponent component , Color to , int steps , int interval ) {
41
+ if (component == null || !component .isEnabled ()) {
42
+ return ;
43
+ }
44
+ this .from = component .getBackground ();
45
+ this .to = to ;
46
+
47
+ this .forwardDeltas = new int [4 ];
48
+ this .backwardDeltas = new int [4 ];
49
+
50
+ forwardDeltas [0 ] = (from .getRed () - to .getRed ()) / steps ;
51
+ forwardDeltas [1 ] = (from .getGreen () - to .getGreen ()) / steps ;
52
+ forwardDeltas [2 ] = (from .getBlue () - to .getBlue ()) / steps ;
53
+ forwardDeltas [3 ] = (from .getAlpha () - to .getAlpha ()) / steps ;
54
+
55
+ backwardDeltas [0 ] = (to .getRed () - from .getRed ()) / steps ;
56
+ backwardDeltas [1 ] = (to .getGreen () - from .getGreen ()) / steps ;
57
+ backwardDeltas [2 ] = (to .getBlue () - from .getBlue ()) / steps ;
58
+ backwardDeltas [3 ] = (to .getAlpha () - from .getAlpha ()) / steps ;
59
+
60
+ this .steps = steps ;
61
+
62
+ this .component = component ;
63
+ this .component .addMouseListener (this );
64
+ timer = new Timer (interval , this );
65
+ component .setBackground (from );
66
+ }
67
+
68
+ private Color nextColor () {
69
+ int rValue = from .getRed () - alpha * forwardDeltas [0 ];
70
+ int gValue = from .getGreen () - alpha * forwardDeltas [1 ];
71
+ int bValue = from .getBlue () - alpha * forwardDeltas [2 ];
72
+ int aValue = from .getAlpha () - alpha * forwardDeltas [3 ];
73
+
74
+ return new Color (rValue , gValue , bValue , aValue );
75
+ }
76
+
77
+ private Color previousColor () {
78
+ int rValue = to .getRed () - (steps - alpha ) * backwardDeltas [0 ];
79
+ int gValue = to .getGreen () - (steps - alpha ) * backwardDeltas [1 ];
80
+ int bValue = to .getBlue () - (steps - alpha ) * backwardDeltas [2 ];
81
+ int aValue = to .getAlpha () - (steps - alpha ) * backwardDeltas [3 ];
82
+
83
+ return new Color (rValue , gValue , bValue , aValue );
84
+ }
85
+
86
+ @ Override
87
+ public void mousePressed (MouseEvent me ) {
88
+ if (!me .getComponent ().isEnabled ()) {
89
+ return ;
90
+ }
91
+ alpha = steps - 1 ;
92
+ forward = false ;
93
+ timer .start ();
94
+
95
+ alpha = 0 ;
96
+ forward = true ;
97
+ timer .start ();
98
+ }
99
+
100
+ @ Override
101
+ public void mouseReleased (MouseEvent me ) {
102
+
103
+ }
104
+
105
+ @ Override
106
+ public void mouseClicked (MouseEvent me ) {
107
+
108
+ }
109
+
110
+ @ Override
111
+ public void mouseExited (MouseEvent me ) {
112
+ if (!me .getComponent ().isEnabled ()) {
113
+ return ;
114
+ }
115
+ alpha = steps - 1 ;
116
+ forward = false ;
117
+ timer .start ();
118
+ }
119
+
120
+ @ Override
121
+ public void mouseEntered (MouseEvent me ) {
122
+ if (!me .getComponent ().isEnabled ()) {
123
+ return ;
124
+ }
125
+ alpha = 0 ;
126
+ forward = true ;
127
+ timer .start ();
128
+ }
129
+
130
+ @ Override
131
+ public void actionPerformed (ActionEvent ae ) {
132
+ if (forward ) {
133
+ component .setBackground (nextColor ());
134
+ ++alpha ;
135
+ } else {
136
+ component .setBackground (previousColor ());
137
+ --alpha ;
138
+ }
139
+
140
+ if (alpha == steps + 1 || alpha == -1 ) {
141
+ timer .stop ();
142
+ }
143
+ }
144
+
145
+ @ Override
146
+ public void mouseDragged (MouseEvent e ) {
147
+ //do nothing this is util only implements interface MouseMotions
148
+ }
149
+
150
+ @ Override
151
+ public void mouseMoved (MouseEvent e ) {
152
+ //do nothing this is util only implements interface MouseMotions
153
+ }
154
+ }
0 commit comments