-
Notifications
You must be signed in to change notification settings - Fork 163
Expand file tree
/
Copy pathMainPanel.java
More file actions
260 lines (230 loc) · 7.81 KB
/
MainPanel.java
File metadata and controls
260 lines (230 loc) · 7.81 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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
// -*- mode:java; encoding:utf-8 -*-
// vim:set fileencoding=utf-8:
// @homepage@
package example;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.logging.Logger;
import javax.swing.*;
public final class MainPanel extends JPanel {
public static final Color DRAWER_BG = new Color(33, 37, 41);
public static final Color HOVER_COLOR = new Color(52, 58, 64);
public static final int DRAWER_WIDTH = 200;
public static final int DURATION = 400;
private final JPanel overlay = makeOverlay();
private final JPanel drawer = makeDrawer();
private final Timer timer = new Timer(10, e -> animate());
private DrawerPosition position = DrawerPosition.LEFT;
private long startTime;
private int startX;
private int targetX;
private boolean isOpen;
private MainPanel() {
super();
EventQueue.invokeLater(() -> {
initLayeredPane();
updateLayoutSizes();
});
add(makeControlBox());
setBackground(Color.GRAY);
setPreferredSize(new Dimension(320, 240));
}
private void initLayeredPane() {
JLayeredPane layeredPane = getRootPane().getLayeredPane();
// layeredPane.add(this, JLayeredPane.DEFAULT_LAYER);
layeredPane.add(overlay, JLayeredPane.PALETTE_LAYER);
layeredPane.add(drawer, JLayeredPane.MODAL_LAYER);
layeredPane.addComponentListener(new ComponentAdapter() {
@Override public void componentResized(ComponentEvent e) {
updateLayoutSizes();
}
});
}
private JPanel makeControlBox() {
JButton btnOpen = new JButton("Open Menu");
btnOpen.addActionListener(e -> toggleDrawer());
JRadioButton rbLeft = new JRadioButton("Left", true);
JRadioButton rbRight = new JRadioButton("Right", false);
ButtonGroup group = new ButtonGroup();
group.add(rbLeft);
group.add(rbRight);
ActionListener positionSwitcher = e -> {
position = rbLeft.isSelected() ? DrawerPosition.LEFT : DrawerPosition.RIGHT;
updateLayoutSizes();
};
rbLeft.addActionListener(positionSwitcher);
rbRight.addActionListener(positionSwitcher);
JPanel controls = new JPanel();
controls.setOpaque(false);
controls.add(btnOpen);
controls.add(rbLeft);
controls.add(rbRight);
return controls;
}
private JPanel makeOverlay() {
JPanel ov = new OverlayPanel();
ov.setVisible(false);
ov.addMouseListener(new MouseAdapter() {
@Override public void mousePressed(MouseEvent e) {
if (isOpen) {
toggleDrawer();
}
}
});
return ov;
}
private JPanel makeDrawer() {
Box menuContainer = Box.createVerticalBox();
menuContainer.setBackground(DRAWER_BG);
menuContainer.add(new NavButton("🏠 Dashboard"));
menuContainer.add(new NavButton("📩 Messages"));
menuContainer.add(new NavButton("📊 Analytics"));
menuContainer.add(new NavButton("⚙️ Settings"));
menuContainer.add(Box.createVerticalGlue());
menuContainer.add(new NavButton("🚪 Logout"));
JScrollPane scroll = new JScrollPane(menuContainer);
scroll.setBorder(BorderFactory.createEmptyBorder());
scroll.getVerticalScrollBar().setUnitIncrement(16);
scroll.getViewport().setBackground(DRAWER_BG);
JPanel dr = new JPanel(new BorderLayout());
dr.setBackground(DRAWER_BG);
dr.add(scroll);
// KeyStroke escape = KeyStroke.getKeyStroke("ESCAPE");
// dr.getInputMap(WHEN_IN_FOCUSED_WINDOW).put(escape, "closeDrawer");
// dr.getActionMap().put("closeDrawer", new AbstractAction() {
// @Override public void actionPerformed(ActionEvent e) {
// if (isOpen) {
// toggleDrawer();
// }
// }
// });
return dr;
}
private void updateLayoutSizes() {
Container contentPane = getRootPane().getContentPane();
Dimension d = contentPane.getSize(); // = this.getSize();
int w = d.width;
int h = d.height;
contentPane.setBounds(0, 0, w, h); // this.setBounds(0, 0, w, h);
overlay.setBounds(0, 0, w, h);
drawer.setBounds(isOpen ? getOpenX(w) : getClosedX(w), 0, DRAWER_WIDTH, h);
revalidate();
repaint();
}
private int getOpenX(int w) {
return position == DrawerPosition.LEFT ? 0 : w - DRAWER_WIDTH;
}
private int getClosedX(int w) {
return position == DrawerPosition.LEFT ? -DRAWER_WIDTH : w;
}
private void toggleDrawer() {
if (!timer.isRunning()) {
isOpen = !isOpen;
int w = getRootPane().getContentPane().getWidth();
startX = drawer.getX();
targetX = isOpen ? getOpenX(w) : getClosedX(w);
overlay.setVisible(isOpen);
startTime = System.currentTimeMillis();
timer.start();
}
}
private void animate() {
long elapsed = System.currentTimeMillis() - startTime;
double progress = Math.min(1d, (double) elapsed / DURATION);
double easedProgress = 1 - Math.pow(1 - progress, 3);
int currentX = (int) (startX + (targetX - startX) * easedProgress);
drawer.setLocation(currentX, 0);
boolean stop = progress >= 1d;
if (stop) {
timer.stop();
if (!isOpen) {
overlay.setVisible(false);
}
}
}
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("Drawer Demo");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.getContentPane().add(new MainPanel());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
enum DrawerPosition {
LEFT, RIGHT
}
class OverlayPanel extends JPanel {
@Override public void updateUI() {
super.updateUI();
setOpaque(false);
setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
}
@Override protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g.create();
g2.setColor(new Color(0, 0, 0, 140));
g2.fillRect(0, 0, getWidth(), getHeight());
g2.dispose();
}
}
class NavButton extends JButton {
public static final Color TEXT_COLOR = new Color(248, 249, 250);
private transient MouseListener mouseListener;
protected NavButton(String text) {
super(text);
}
@Override public void updateUI() {
removeMouseListener(mouseListener);
super.updateUI();
setContentAreaFilled(false);
setFocusPainted(false);
setBorderPainted(false);
setForeground(TEXT_COLOR);
setHorizontalAlignment(LEFT);
setFont(getFont().deriveFont(Font.PLAIN, 14f));
setBorder(BorderFactory.createEmptyBorder(10, 20, 10, 20));
setCursor(new Cursor(Cursor.HAND_CURSOR));
mouseListener = new MouseAdapter() {
@Override public void mouseEntered(MouseEvent e) {
setOpaque(true);
setBackground(MainPanel.HOVER_COLOR);
}
@Override public void mouseExited(MouseEvent e) {
setOpaque(false);
}
@Override public void mousePressed(MouseEvent e) {
setBackground(MainPanel.HOVER_COLOR.darker());
}
@Override public void mouseReleased(MouseEvent e) {
setBackground(MainPanel.HOVER_COLOR);
}
};
addMouseListener(mouseListener);
}
@Override public Dimension getMaximumSize() {
return new Dimension(MainPanel.DRAWER_WIDTH, 50);
}
@Override protected void paintComponent(Graphics g) {
if (isOpaque()) {
g.setColor(getBackground());
g.fillRect(0, 0, getWidth(), getHeight());
}
super.paintComponent(g);
}
}