5
5
package example ;
6
6
7
7
import java .awt .*;
8
+ import java .awt .event .ActionEvent ;
8
9
import java .awt .event .FocusEvent ;
9
10
import java .awt .event .FocusListener ;
10
11
import java .awt .geom .Path2D ;
@@ -83,32 +84,7 @@ class AnimatedBorder extends EmptyBorder {
83
84
84
85
protected AnimatedBorder (JComponent c ) {
85
86
super (BORDER , BORDER , BORDER + BOTTOM_SPACE , BORDER );
86
- animator .addActionListener (e -> {
87
- if (startTime < 0 ) {
88
- startTime = System .currentTimeMillis ();
89
- }
90
- long playTime = System .currentTimeMillis () - startTime ;
91
- double progress = playTime / PLAY_TIME ;
92
- boolean stop = progress > 1d || points .isEmpty ();
93
- if (stop ) {
94
- startTime = -1L ;
95
- ((Timer ) e .getSource ()).stop ();
96
- c .repaint ();
97
- return ;
98
- }
99
- Point2D pos = new Point2D .Double ();
100
- pos .setLocation (points .get (0 ));
101
- borderPath .reset ();
102
- borderPath .moveTo (pos .getX (), pos .getY ());
103
- int idx = Math .min (Math .max (0 , (int ) (points .size () * progress )), points .size () - 1 );
104
- for (int i = 0 ; i <= idx ; i ++) {
105
- pos .setLocation (points .get (i ));
106
- borderPath .lineTo (pos .getX (), pos .getY ());
107
- borderPath .moveTo (pos .getX (), pos .getY ());
108
- }
109
- borderPath .closePath ();
110
- c .repaint ();
111
- });
87
+ animator .addActionListener (e -> animation (c , e ));
112
88
c .addFocusListener (new FocusListener () {
113
89
@ Override public void focusGained (FocusEvent e ) {
114
90
Rectangle r = c .getBounds ();
@@ -131,6 +107,33 @@ protected AnimatedBorder(JComponent c) {
131
107
});
132
108
}
133
109
110
+ private void animation (JComponent c , ActionEvent e ) {
111
+ if (startTime < 0 ) {
112
+ startTime = System .currentTimeMillis ();
113
+ }
114
+ long playTime = System .currentTimeMillis () - startTime ;
115
+ double progress = playTime / PLAY_TIME ;
116
+ boolean stop = progress > 1d || points .isEmpty ();
117
+ if (stop ) {
118
+ startTime = -1L ;
119
+ ((Timer ) e .getSource ()).stop ();
120
+ c .repaint ();
121
+ return ;
122
+ }
123
+ Point2D pos = new Point2D .Double ();
124
+ pos .setLocation (points .get (0 ));
125
+ borderPath .reset ();
126
+ borderPath .moveTo (pos .getX (), pos .getY ());
127
+ int idx = Math .min (Math .max (0 , (int ) (points .size () * progress )), points .size () - 1 );
128
+ for (int i = 0 ; i <= idx ; i ++) {
129
+ pos .setLocation (points .get (i ));
130
+ borderPath .lineTo (pos .getX (), pos .getY ());
131
+ borderPath .moveTo (pos .getX (), pos .getY ());
132
+ }
133
+ borderPath .closePath ();
134
+ c .repaint ();
135
+ }
136
+
134
137
@ Override public void paintBorder (Component c , Graphics g , int x , int y , int w , int h ) {
135
138
super .paintBorder (c , g , x , y , w , h );
136
139
Graphics2D g2 = (Graphics2D ) g .create ();
0 commit comments