Skip to content

Commit 85b2d75

Browse files
committed
Checkstyle: LambdaBodyLength
1 parent a507d9e commit 85b2d75

File tree

2 files changed

+49
-43
lines changed

2 files changed

+49
-43
lines changed

examples/FocusBorderAnimation/src/java/example/MainPanel.java

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package example;
66

77
import java.awt.*;
8+
import java.awt.event.ActionEvent;
89
import java.awt.event.FocusEvent;
910
import java.awt.event.FocusListener;
1011
import java.awt.geom.Path2D;
@@ -83,32 +84,7 @@ class AnimatedBorder extends EmptyBorder {
8384

8485
protected AnimatedBorder(JComponent c) {
8586
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));
11288
c.addFocusListener(new FocusListener() {
11389
@Override public void focusGained(FocusEvent e) {
11490
Rectangle r = c.getBounds();
@@ -131,6 +107,33 @@ protected AnimatedBorder(JComponent c) {
131107
});
132108
}
133109

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+
134137
@Override public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) {
135138
super.paintBorder(c, g, x, y, w, h);
136139
Graphics2D g2 = (Graphics2D) g.create();

examples/TransformedShape/src/java/example/MainPanel.java

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.awt.font.TextLayout;
1212
import java.awt.geom.AffineTransform;
1313
import java.awt.geom.Rectangle2D;
14+
import java.util.logging.Logger;
1415
import javax.swing.*;
1516

1617
public final class MainPanel extends JPanel {
@@ -30,7 +31,7 @@ private static void createAndShowGui() {
3031
} catch (UnsupportedLookAndFeelException ignored) {
3132
Toolkit.getDefaultToolkit().beep();
3233
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException ex) {
33-
ex.printStackTrace();
34+
Logger.getGlobal().severe(ex::getMessage);
3435
return;
3536
}
3637
JFrame frame = new JFrame("@title@");
@@ -55,25 +56,27 @@ protected FontRotateAnimation(String str) {
5556
FontRenderContext frc = new FontRenderContext(null, true, true);
5657
Shape outline = new TextLayout(str, font, frc).getOutline(null);
5758
shape = outline;
58-
animator.addActionListener(e -> {
59-
repaint(shape.getBounds()); // clear prev
60-
Rectangle2D b = outline.getBounds2D();
61-
double ax = b.getCenterX();
62-
double ay = b.getCenterY();
63-
AffineTransform at = AffineTransform.getRotateInstance(Math.toRadians(rotate), ax, ay);
64-
double cx = getWidth() / 2d - ax;
65-
double cy = getHeight() / 2d - ay;
66-
AffineTransform toCenterAt = AffineTransform.getTranslateInstance(cx, cy);
67-
68-
Shape s1 = at.createTransformedShape(outline);
69-
shape = toCenterAt.createTransformedShape(s1);
70-
repaint(shape.getBounds());
71-
// rotate = rotate >= 360 ? 0 : rotate + 2;
72-
rotate = (rotate + 2) % 360;
73-
});
59+
animator.addActionListener(e -> rotate(outline));
7460
animator.start();
7561
}
7662

63+
private void rotate(Shape outline) {
64+
repaint(shape.getBounds()); // clear prev
65+
Rectangle2D b = outline.getBounds2D();
66+
double ax = b.getCenterX();
67+
double ay = b.getCenterY();
68+
AffineTransform at = AffineTransform.getRotateInstance(Math.toRadians(rotate), ax, ay);
69+
double cx = getWidth() / 2d - ax;
70+
double cy = getHeight() / 2d - ay;
71+
AffineTransform toCenterAt = AffineTransform.getTranslateInstance(cx, cy);
72+
73+
Shape s1 = at.createTransformedShape(outline);
74+
shape = toCenterAt.createTransformedShape(s1);
75+
repaint(shape.getBounds());
76+
// rotate = rotate >= 360 ? 0 : rotate + 2;
77+
rotate = (rotate + 2) % 360;
78+
}
79+
7780
@Override public void updateUI() {
7881
removeHierarchyListener(listener);
7982
super.updateUI();

0 commit comments

Comments
 (0)