/
MainPanel.java
209 lines (187 loc) · 6.98 KB
/
MainPanel.java
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
// -*- mode:java; encoding:utf-8 -*-
// vim:set fileencoding=utf-8:
// @homepage@
package example;
import java.awt.*;
import java.awt.event.HierarchyEvent;
import java.awt.event.HierarchyListener;
import java.awt.font.FontRenderContext;
import java.awt.font.GlyphVector;
import java.awt.font.LineMetrics;
import java.awt.geom.Ellipse2D;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import javax.swing.*;
public final class MainPanel extends JPanel {
private MainPanel() {
super(new BorderLayout());
JScrollPane scroll = new JScrollPane(makeList());
scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
add(scroll);
setPreferredSize(new Dimension(320, 240));
}
private static JList<String> makeList() {
DefaultListModel<String> model = new DefaultListModel<>();
model.addElement("123412354151434");
model.addElement("5678474567356723456245624");
model.addElement("123412354151435467769780896786789567847456735672245624");
model.addElement("7808967867895678474624");
model.addElement("1234541956723624");
model.addElement("abc, 12345, 4567890");
model.addElement("1234123541514354677697808967867895678474567356723456245624");
model.addElement("abc def ghi jkl mno pqr stu");
model.addElement("145689074");
model.addElement("26543654365437");
model.addElement("54645762465");
JList<String> list = new JList<>(model);
list.setCellRenderer(new AnimeListCellRenderer<>(list));
return list;
}
public static void main(String[] args) {
EventQueue.invokeLater(MainPanel::createAndShowGui);
}
private static void createAndShowGui() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (UnsupportedLookAndFeelException ignored) {
Toolkit.getDefaultToolkit().beep();
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException ex) {
ex.printStackTrace();
return;
}
JFrame frame = new JFrame("@title@");
// frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.getContentPane().add(new MainPanel());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
class AnimeListCellRenderer<E> extends JPanel implements ListCellRenderer<E>, HierarchyListener {
private static final Color SELECTEDCOLOR = new Color(0xE6_E6_FF);
private final AnimeIcon icon = new AnimeIcon();
private final MarqueeLabel label = new MarqueeLabel();
private final Timer animator;
private final JList<E> list;
private boolean running;
private int animateIndex = -1;
protected AnimeListCellRenderer(JList<E> l) {
super(new BorderLayout());
this.list = l;
animator = new Timer(80, e -> {
int i = list.getSelectedIndex();
if (i >= 0) {
running = true;
list.repaint(list.getCellBounds(i, i));
} else {
running = false;
}
});
// setOpaque(true);
add(icon, BorderLayout.WEST);
add(label);
list.addHierarchyListener(this);
// animator.start();
}
@Override public final Component add(Component comp) {
return super.add(comp);
}
@Override public final void add(Component comp, Object constraints) {
super.add(comp, constraints);
}
@Override public void hierarchyChanged(HierarchyEvent e) {
if ((e.getChangeFlags() & HierarchyEvent.DISPLAYABILITY_CHANGED) != 0) {
if (e.getComponent().isDisplayable()) {
animator.start();
} else {
animator.stop();
}
}
}
@Override public Component getListCellRendererComponent(JList<? extends E> l, E value, int index, boolean isSelected, boolean cellHasFocus) {
setBackground(isSelected ? SELECTEDCOLOR : l.getBackground());
label.setText(Objects.toString(value, ""));
animateIndex = index;
return this;
}
protected boolean isAnimatingCell() {
return running && animateIndex == list.getSelectedIndex();
}
private final class MarqueeLabel extends JLabel {
private float xx;
@Override public boolean isOpaque() {
return false;
}
@Override protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g.create();
Rectangle r = list.getVisibleRect();
int cw = r.width - icon.getPreferredSize().width;
FontRenderContext frc = g2.getFontRenderContext();
GlyphVector gv = getFont().createGlyphVector(frc, getText());
if (isAnimatingCell() && gv.getVisualBounds().getWidth() > cw) {
LineMetrics lm = getFont().getLineMetrics(getText(), frc);
float yy = lm.getAscent() / 2f + (float) gv.getVisualBounds().getY();
g2.drawGlyphVector(gv, cw - xx, getHeight() / 2f - yy);
xx = cw + gv.getVisualBounds().getWidth() - xx > 0 ? xx + 8f : 0f;
} else {
super.paintComponent(g);
}
g2.dispose();
}
}
private final class AnimeIcon extends JComponent {
private static final double R = 2d;
private static final double SX = 1d;
private static final double SY = 1d;
private static final int WIDTH = (int) (R * 8 + SX * 2);
private static final int HEIGHT = (int) (R * 8 + SY * 2);
private final List<Shape> flipbookFrames = new ArrayList<>(Arrays.asList(
new Ellipse2D.Double(SX + 3 * R, SY + 0 * R, 2 * R, 2 * R),
new Ellipse2D.Double(SX + 5 * R, SY + 1 * R, 2 * R, 2 * R),
new Ellipse2D.Double(SX + 6 * R, SY + 3 * R, 2 * R, 2 * R),
new Ellipse2D.Double(SX + 5 * R, SY + 5 * R, 2 * R, 2 * R),
new Ellipse2D.Double(SX + 3 * R, SY + 6 * R, 2 * R, 2 * R),
new Ellipse2D.Double(SX + 1 * R, SY + 5 * R, 2 * R, 2 * R),
new Ellipse2D.Double(SX + 0 * R, SY + 3 * R, 2 * R, 2 * R),
new Ellipse2D.Double(SX + 1 * R, SY + 1 * R, 2 * R, 2 * R)));
@Override public void updateUI() {
super.updateUI();
setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 2));
}
@Override public boolean isOpaque() {
return false;
}
@Override public Dimension getPreferredSize() {
return new Dimension(WIDTH + 2, HEIGHT);
}
@Override protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g.create();
// g2.setPaint(getBackground());
// g2.fillRect(0, 0, getWidth(), getHeight());
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
if (isAnimatingCell()) {
float alpha = .1f;
for (Shape s : flipbookFrames) {
g2.setPaint(makeColor(alpha));
g2.fill(s);
alpha += .1f;
}
// flipbookFrames.add(flipbookFrames.remove(0));
Collections.rotate(flipbookFrames, 1);
} else {
g2.setPaint(new Color(0x99_99_99));
for (Shape s : flipbookFrames) {
g2.fill(s);
}
}
g2.dispose();
}
}
protected static Color makeColor(float alpha) {
return new Color(.5f, .5f, .5f, alpha);
}
}