-
Notifications
You must be signed in to change notification settings - Fork 163
Expand file tree
/
Copy pathMainPanel.java
More file actions
97 lines (85 loc) · 2.79 KB
/
MainPanel.java
File metadata and controls
97 lines (85 loc) · 2.79 KB
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
// -*- mode:java; encoding:utf-8 -*-
// vim:set fileencoding=utf-8:
// @homepage@
package example;
import java.awt.*;
import java.io.IOException;
import java.io.InputStream;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.*;
public final class MainPanel extends JPanel {
private Fade mode = Fade.IN;
private MainPanel() {
super(new BorderLayout());
Timer animator = new Timer(25, null);
AtomicInteger alpha = new AtomicInteger(10);
String path = "example/test.png";
ClassLoader cl = Thread.currentThread().getContextClassLoader();
Icon icon = Optional.ofNullable(cl.getResource(path)).map(url -> {
Icon i;
try (InputStream s = url.openStream()) {
i = new ImageIcon(ImageIO.read(s));
} catch (IOException ex) {
i = UIManager.getIcon("OptionPane.errorIcon");
}
return i;
}).orElseGet(() -> UIManager.getIcon("OptionPane.errorIcon"));
Component fade = new JComponent() {
@Override protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g.create();
g2.setPaint(Color.BLACK);
g2.fillRect(0, 0, getWidth(), getHeight());
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha.get() * .1f));
icon.paintIcon(this, g2, 0, 0);
g2.dispose();
}
};
animator.addActionListener(e -> {
if (mode == Fade.IN && alpha.get() < 10) {
alpha.incrementAndGet(); // alpha += 1;
} else if (mode == Fade.OUT && alpha.get() > 0) {
alpha.decrementAndGet(); // alpha -= 1;
} else {
animator.stop();
}
fade.repaint();
});
JButton button = new JButton("Fade In/Out");
button.addActionListener(e -> {
mode = mode.toggle();
animator.start();
});
add(fade);
add(button, BorderLayout.SOUTH);
setOpaque(false);
setPreferredSize(new Dimension(320, 240));
}
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) {
Logger.getGlobal().severe(ex::getMessage);
return;
}
JFrame frame = new JFrame("@title@");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.getContentPane().add(new MainPanel());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
enum Fade {
IN, OUT;
public Fade toggle() {
return this == IN ? OUT : IN;
}
}