-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainMenu.java
More file actions
265 lines (228 loc) · 10.5 KB
/
Copy pathMainMenu.java
File metadata and controls
265 lines (228 loc) · 10.5 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
261
262
263
264
265
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import java.sql.*;
public class MainMenu extends JFrame {
private final String loggedInEmail;
private String loggedInName = "User";
private JPanel profilePanel;
private boolean profileVisible = false;
private JLabel profilePicLabel;
private BufferedImage profileImage;
public MainMenu(String email) {
this.loggedInEmail = email;
this.loggedInName = fetchUserNameByEmail(email);
setTitle("ZEN Dashboard");
ImageIcon icon = new ImageIcon(getClass().getResource("/resources/icon.png"));
setIconImage(icon.getImage());
setSize(1200, 720);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new BorderLayout());
add(createTopBar(), BorderLayout.NORTH);
add(createMainContent(), BorderLayout.CENTER);
profilePanel = createProfilePanel();
add(profilePanel, BorderLayout.EAST);
setVisible(true);
}
private String fetchUserNameByEmail(String email) {
String name = "User";
try (Connection conn = DriverManager.getConnection("jdbc:ucanaccess://database/users.accdb");
PreparedStatement stmt = conn.prepareStatement("SELECT name FROM users WHERE email = ?")) {
stmt.setString(1, email);
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
name = rs.getString("name");
}
} catch (Exception e) {
e.printStackTrace();
}
return name;
}
private JPanel createTopBar() {
JPanel topBar = new JPanel(new BorderLayout());
topBar.setBackground(new Color(44, 62, 80));
topBar.setPreferredSize(new Dimension(getWidth(), 60));
JLabel title = new JLabel("ZEN | Travel with Ease");
title.setForeground(Color.WHITE);
title.setFont(new Font("Segoe UI", Font.BOLD, 24));
title.setBorder(BorderFactory.createEmptyBorder(0, 30, 0, 0));
JButton menuBtn = new JButton("\u2630");
menuBtn.setFont(new Font("SansSerif", Font.BOLD, 26));
menuBtn.setForeground(Color.WHITE);
menuBtn.setBackground(new Color(44, 62, 80));
menuBtn.setBorder(null);
menuBtn.setFocusPainted(false);
menuBtn.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
menuBtn.setPreferredSize(new Dimension(60, 60));
menuBtn.addActionListener(e -> toggleProfilePanel());
topBar.add(title, BorderLayout.WEST);
topBar.add(menuBtn, BorderLayout.EAST);
return topBar;
}
private JPanel createMainContent() {
JPanel panel = new JPanel(new GridBagLayout());
panel.setBackground(new Color(245, 245, 245));
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(30, 30, 30, 30);
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 0.5;
gbc.weighty = 0.5;
JButton busBtn = createBookingButton("Book Bus Ticket", "/resources/bus.png");
JButton trainBtn = createBookingButton("Book Train Ticket", "/resources/train.png");
JButton carBtn = createBookingButton("Book Car Ride", "/resources/car.png");
JButton bikeBtn = createBookingButton("Book Bike Ride", "/resources/bike.png");
gbc.gridx = 0; gbc.gridy = 0;
panel.add(busBtn, gbc);
gbc.gridx = 1;
panel.add(trainBtn, gbc);
gbc.gridx = 0; gbc.gridy = 1;
panel.add(carBtn, gbc);
gbc.gridx = 1;
panel.add(bikeBtn, gbc);
return panel;
}
private JButton createBookingButton(String text, String iconPath) {
JButton btn = new JButton();
btn.setPreferredSize(new Dimension(600, 280));
btn.setFont(new Font("Segoe UI", Font.BOLD, 15));
btn.setForeground(Color.DARK_GRAY);
btn.setBackground(Color.WHITE);
btn.setFocusPainted(false);
btn.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
btn.setLayout(new BorderLayout());
btn.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(new Color(220, 220, 220), 1),
BorderFactory.createEmptyBorder(10, 10, 10, 10)
));
try {
java.net.URL imgURL = getClass().getResource(iconPath);
if (imgURL != null) {
Image img = ImageIO.read(imgURL);
ImageIcon icon = new ImageIcon(img.getScaledInstance(150, 150, Image.SCALE_SMOOTH));
JLabel iconLabel = new JLabel(icon);
iconLabel.setHorizontalAlignment(SwingConstants.CENTER);
btn.add(iconLabel, BorderLayout.CENTER);
} else {
System.err.println("Couldn't find image resource: " + iconPath);
}
} catch (Exception e) {
e.printStackTrace();
}
JLabel textLabel = new JLabel(text, SwingConstants.CENTER);
textLabel.setFont(new Font("Segoe UI", Font.BOLD, 14));
btn.add(textLabel, BorderLayout.SOUTH);
btn.addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent evt) {
btn.setBackground(new Color(230, 240, 255));
}
public void mouseExited(MouseEvent evt) {
btn.setBackground(Color.WHITE);
}
});
// Pass loggedInName and loggedInEmail to BookingManager on button click
btn.addActionListener(e -> new BookingManager(text, loggedInName, loggedInEmail));
return btn;
}
private JPanel createProfilePanel() {
JPanel panel = new JPanel(null);
panel.setPreferredSize(new Dimension(300, getHeight()));
panel.setBackground(new Color(236, 240, 241));
panel.setVisible(false);
profilePicLabel = new JLabel("U", SwingConstants.CENTER);
profilePicLabel.setBounds(100, 30, 100, 100);
profilePicLabel.setOpaque(true);
profilePicLabel.setBackground(new Color(149, 165, 166));
profilePicLabel.setForeground(Color.WHITE);
profilePicLabel.setFont(new Font("Segoe UI", Font.BOLD, 36));
profilePicLabel.setBorder(BorderFactory.createLineBorder(Color.WHITE, 3));
makeCircular(profilePicLabel);
panel.add(profilePicLabel);
JButton changePicBtn = new JButton("Change Picture");
changePicBtn.setBounds(80, 140, 140, 35);
changePicBtn.setBackground(new Color(41, 128, 185));
changePicBtn.setForeground(Color.WHITE);
changePicBtn.setFocusPainted(false);
changePicBtn.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
changePicBtn.setFont(new Font("Segoe UI", Font.PLAIN, 14));
changePicBtn.addActionListener(e -> selectProfilePicture());
panel.add(changePicBtn);
JLabel nameLabel = new JLabel("Name: " + loggedInName);
nameLabel.setBounds(30, 190, 240, 30);
nameLabel.setFont(new Font("Segoe UI", Font.PLAIN, 16));
panel.add(nameLabel);
JLabel emailLabel = new JLabel("Email: " + loggedInEmail);
emailLabel.setBounds(30, 220, 240, 30);
emailLabel.setFont(new Font("Segoe UI", Font.PLAIN, 16));
panel.add(emailLabel);
JButton logoutBtn = new JButton("Logout");
logoutBtn.setBounds(80, 280, 140, 40);
logoutBtn.setBackground(new Color(231, 76, 60));
logoutBtn.setForeground(Color.WHITE);
logoutBtn.setFocusPainted(false);
logoutBtn.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
logoutBtn.setFont(new Font("Segoe UI", Font.BOLD, 16));
logoutBtn.addActionListener(e -> {
dispose();
SwingUtilities.invokeLater(AuthSystem::new);
});
panel.add(logoutBtn);
return panel;
}
private void makeCircular(JLabel label) {
label.setUI(new javax.swing.plaf.basic.BasicLabelUI() {
@Override
public void paint(Graphics g, JComponent c) {
Graphics2D g2 = (Graphics2D) g.create();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
Shape circle = new Ellipse2D.Float(0, 0, c.getWidth(), c.getHeight());
g2.setClip(circle);
if (profileImage != null) {
g2.drawImage(profileImage.getScaledInstance(c.getWidth(), c.getHeight(), Image.SCALE_SMOOTH), 0, 0, null);
} else {
super.paint(g2, c);
}
g2.dispose();
}
});
label.repaint();
}
private void selectProfilePicture() {
FileDialog fileDialog = new FileDialog(this, "Select Profile Picture", FileDialog.LOAD);
fileDialog.setVisible(true);
String directory = fileDialog.getDirectory();
String file = fileDialog.getFile();
if (directory != null && file != null) {
File selectedFile = new File(directory, file);
String fileName = selectedFile.getName().toLowerCase();
if (!(fileName.endsWith(".png") || fileName.endsWith(".jpg") ||
fileName.endsWith(".jpeg") || fileName.endsWith(".gif"))) {
JOptionPane.showMessageDialog(this, "Please select a valid image file.", "Invalid File", JOptionPane.ERROR_MESSAGE);
return;
}
try {
BufferedImage img = ImageIO.read(selectedFile);
if (img != null) {
profileImage = img;
profilePicLabel.setText("");
profilePicLabel.repaint();
} else {
JOptionPane.showMessageDialog(this, "Failed to load image.", "Error", JOptionPane.ERROR_MESSAGE);
}
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "Error loading image: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
}
}
private void toggleProfilePanel() {
profileVisible = !profileVisible;
profilePanel.setVisible(profileVisible);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new MainMenu("user@example.com"));
}
}