-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNexusApp.java
More file actions
427 lines (368 loc) · 17.1 KB
/
Copy pathNexusApp.java
File metadata and controls
427 lines (368 loc) · 17.1 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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Point2D;
import java.io.*;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Scanner;
/**
* NexusApp - Улучшенный мессенджер на Java Swing.
* Добавлено: Валидация пароля, профили, список пользователей, смена темы и градиентный фон.
*/
public class NexusApp extends JFrame {
private static final String GITHUB_RAW_URL = "https://raw.githubusercontent.com/userscode/nexusapp/refs/heads/main/NexusApp.java";
private static final String CURRENT_FILE_NAME = "NexusApp.java";
private CardLayout cardLayout;
private JPanel mainPanel;
private LoadingOverlay loadingOverlay;
// Цветовые палитры
public static Color BG_DARKER = new Color(30, 31, 34);
public static Color BG_DARK = new Color(43, 45, 49);
public static Color BG_MEDIUM = new Color(49, 51, 56);
public static Color BG_LIGHT = new Color(56, 58, 64);
public static Color ACCENT = new Color(88, 101, 242);
public static Color TEXT_MAIN = new Color(219, 222, 225);
public static Color TEXT_MUTED = new Color(148, 155, 164);
private boolean isDarkTheme = true;
// Данные текущего пользователя
private String currentUsername = "User123";
private String currentStatus = "Пишу код...";
private Color userAvatarColor = ACCENT;
public NexusApp() {
setTitle("Nexus | Современный мессенджер");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(1100, 700);
setMinimumSize(new Dimension(900, 600));
setLocationRelativeTo(null);
cardLayout = new CardLayout();
mainPanel = new JPanel(cardLayout);
loadingOverlay = new LoadingOverlay();
mainPanel.add(loadingOverlay, "LOADING");
mainPanel.add(createAuthPanel(), "AUTH");
mainPanel.add(createMessengerPanel(), "MESSENGER");
add(mainPanel);
cardLayout.show(mainPanel, "LOADING");
new Thread(this::checkForUpdates).start();
}
private void checkForUpdates() {
try {
Thread.sleep(2000);
// В реальных условиях здесь будет логика чтения GitHub
showAuth();
} catch (Exception e) {
showAuth();
}
}
private void showAuth() {
SwingUtilities.invokeLater(() -> {
loadingOverlay.stopAnimation();
cardLayout.show(mainPanel, "AUTH");
});
}
// --- ПАНЕЛЬ АВТОРИЗАЦИИ С ГРАДИЕНТОМ ---
private JPanel createAuthPanel() {
JPanel authPanel = new JPanel(new GridBagLayout()) {
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
int w = getWidth();
int h = getHeight();
GradientPaint gp = new GradientPaint(0, 0, new Color(40, 42, 54), w, h, ACCENT);
g2d.setPaint(gp);
g2d.fillRect(0, 0, w, h);
}
};
JPanel card = new RoundedPanel(24, BG_MEDIUM);
card.setLayout(new BoxLayout(card, BoxLayout.Y_AXIS));
card.setBorder(new EmptyBorder(40, 40, 40, 40));
card.setPreferredSize(new Dimension(450, 550));
JLabel title = new JLabel("Создать аккаунт");
title.setFont(new Font("SansSerif", Font.BOLD, 28));
title.setForeground(Color.WHITE);
title.setAlignmentX(Component.CENTER_ALIGNMENT);
JTextField emailF = createInput("ЭЛЕКТРОННАЯ ПОЧТА", false);
JTextField userF = createInput("ИМЯ ПОЛЬЗОВАТЕЛЯ", false);
JPasswordField passF = (JPasswordField) createInput("ПАРОЛЬ (Мин. 6 символов)", true);
JButton registerBtn = new JButton("Продолжить");
styleButton(registerBtn);
registerBtn.addActionListener(e -> {
String pass = new String(passF.getPassword());
String user = userF.getText().trim();
if (user.isEmpty()) {
JOptionPane.showMessageDialog(this, "Введите имя пользователя!");
return;
}
if (pass.length() < 6) {
JOptionPane.showMessageDialog(this, "Пароль слишком короткий! Минимум 6 символов.");
return;
}
currentUsername = user;
cardLayout.show(mainPanel, "MESSENGER");
});
card.add(title);
card.add(Box.createVerticalStrut(30));
card.add(emailF.getParent());
card.add(Box.createVerticalStrut(20));
card.add(userF.getParent());
card.add(Box.createVerticalStrut(20));
card.add(passF.getParent());
card.add(Box.createVerticalStrut(35));
card.add(registerBtn);
authPanel.add(card);
return authPanel;
}
// --- ГЛАВНЫЙ ИНТЕРФЕЙС МЕССЕНДЖЕРА ---
private JPanel createMessengerPanel() {
JPanel messenger = new JPanel(new BorderLayout());
// Левая панель серверов
JPanel serverBar = new JPanel();
serverBar.setBackground(BG_DARKER);
serverBar.setPreferredSize(new Dimension(72, 0));
serverBar.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 15));
serverBar.add(createSquircleIcon("DM", ACCENT));
serverBar.add(createSquircleIcon("N", BG_LIGHT));
// Сайдбар каналов
JPanel sidebar = new JPanel(new BorderLayout());
sidebar.setBackground(BG_DARK);
sidebar.setPreferredSize(new Dimension(240, 0));
JLabel header = new JLabel("Nexus Community");
header.setBorder(new EmptyBorder(20, 15, 20, 15));
header.setForeground(Color.WHITE);
header.setFont(new Font("SansSerif", Font.BOLD, 16));
sidebar.add(header, BorderLayout.NORTH);
// Кнопка смены темы
JButton themeBtn = new JButton("Сменить тему");
themeBtn.setFocusPainted(false);
themeBtn.setBackground(BG_LIGHT);
themeBtn.setForeground(Color.WHITE);
themeBtn.addActionListener(e -> toggleTheme());
sidebar.add(themeBtn, BorderLayout.CENTER);
// Панель профиля внизу
JPanel profilePanel = new JPanel(new BorderLayout(10, 0));
profilePanel.setBackground(new Color(35, 36, 40));
profilePanel.setBorder(new EmptyBorder(10, 10, 10, 10));
profilePanel.setCursor(new Cursor(Cursor.HAND_CURSOR));
profilePanel.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) { showProfileSettings(); }
});
JLabel avatarLabel = new JLabel() {
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setColor(userAvatarColor);
g2.fillOval(0, 0, 32, 32);
}
};
avatarLabel.setPreferredSize(new Dimension(32, 32));
JPanel textInfo = new JPanel(new GridLayout(2, 1));
textInfo.setOpaque(false);
JLabel uName = new JLabel(currentUsername);
uName.setForeground(Color.WHITE);
JLabel uStatus = new JLabel(currentStatus);
uStatus.setFont(new Font("SansSerif", Font.PLAIN, 10));
uStatus.setForeground(TEXT_MUTED);
textInfo.add(uName);
textInfo.add(uStatus);
profilePanel.add(avatarLabel, BorderLayout.WEST);
profilePanel.add(textInfo, BorderLayout.CENTER);
sidebar.add(profilePanel, BorderLayout.SOUTH);
// Чат
JPanel chatArea = new JPanel(new BorderLayout());
chatArea.setBackground(BG_MEDIUM);
JPanel msgCont = new JPanel();
msgCont.setLayout(new BoxLayout(msgCont, BoxLayout.Y_AXIS));
msgCont.setBackground(BG_MEDIUM);
JScrollPane scroll = new JScrollPane(msgCont);
scroll.setBorder(null);
JPanel inputWrap = new JPanel(new BorderLayout());
inputWrap.setBackground(BG_MEDIUM);
inputWrap.setBorder(new EmptyBorder(10, 20, 20, 20));
JTextField input = new JTextField();
input.setBackground(BG_LIGHT);
input.setForeground(Color.WHITE);
input.setCaretColor(Color.WHITE);
input.setBorder(BorderFactory.createCompoundBorder(new RoundedBorder(12, BG_LIGHT), new EmptyBorder(12, 15, 12, 15)));
input.addActionListener(e -> {
if(!input.getText().isEmpty()){
addMessage(msgCont, currentUsername, input.getText());
input.setText("");
msgCont.revalidate();
}
});
inputWrap.add(input, BorderLayout.CENTER);
chatArea.add(scroll, BorderLayout.CENTER);
chatArea.add(inputWrap, BorderLayout.SOUTH);
// Список пользователей (справа)
JPanel memberList = new JPanel();
memberList.setBackground(BG_DARK);
memberList.setPreferredSize(new Dimension(200, 0));
memberList.setLayout(new BoxLayout(memberList, BoxLayout.Y_AXIS));
memberList.setBorder(new EmptyBorder(20, 10, 10, 10));
JLabel mTitle = new JLabel("УЧАСТНИКИ — 3");
mTitle.setForeground(TEXT_MUTED);
mTitle.setFont(new Font("SansSerif", Font.BOLD, 11));
memberList.add(mTitle);
memberList.add(Box.createVerticalStrut(15));
memberList.add(createMemberItem("Admin", Color.RED, "В сети"));
memberList.add(createMemberItem("Alice", Color.PINK, "Спит"));
memberList.add(createMemberItem(currentUsername, userAvatarColor, "Вы"));
messenger.add(serverBar, BorderLayout.WEST);
JPanel rightSide = new JPanel(new BorderLayout());
rightSide.add(sidebar, BorderLayout.WEST);
rightSide.add(chatArea, BorderLayout.CENTER);
rightSide.add(memberList, BorderLayout.EAST);
messenger.add(rightSide, BorderLayout.CENTER);
return messenger;
}
private void showProfileSettings() {
String newStatus = JOptionPane.showInputDialog(this, "Введите новое описание профиля:", currentStatus);
if (newStatus != null) {
currentStatus = newStatus;
cardLayout.show(mainPanel, "MESSENGER"); // Обновляем
}
}
private void toggleTheme() {
isDarkTheme = !isDarkTheme;
if (isDarkTheme) {
BG_MEDIUM = new Color(49, 51, 56);
BG_DARK = new Color(43, 45, 49);
TEXT_MAIN = Color.WHITE;
} else {
BG_MEDIUM = new Color(242, 243, 245);
BG_DARK = new Color(227, 229, 232);
TEXT_MAIN = Color.BLACK;
}
JOptionPane.showMessageDialog(this, "Тема будет применена после перезагрузки UI (в разработке)");
}
private JPanel createMemberItem(String name, Color col, String sub) {
JPanel p = new JPanel(new BorderLayout(10, 0));
p.setBackground(BG_DARK);
p.setMaximumSize(new Dimension(180, 45));
JLabel av = new JLabel() {
@Override protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setColor(col);
g2.fillOval(0, 0, 24, 24);
}
};
av.setPreferredSize(new Dimension(24, 24));
JPanel info = new JPanel(new GridLayout(2, 1));
info.setOpaque(false);
JLabel n = new JLabel(name); n.setForeground(TEXT_MAIN);
JLabel s = new JLabel(sub); s.setForeground(TEXT_MUTED); s.setFont(new Font("SansSerif", Font.PLAIN, 9));
info.add(n); info.add(s);
p.add(av, BorderLayout.WEST);
p.add(info, BorderLayout.CENTER);
return p;
}
private JTextField createInput(String labelStr, boolean isPass) {
JPanel panel = new JPanel(new BorderLayout(0, 5));
panel.setBackground(BG_MEDIUM);
panel.setMaximumSize(new Dimension(360, 65));
JLabel label = new JLabel(labelStr);
label.setForeground(TEXT_MUTED);
label.setFont(new Font("SansSerif", Font.BOLD, 11));
JTextField field = isPass ? new JPasswordField() : new JTextField();
field.setBackground(BG_DARKER);
field.setForeground(Color.WHITE);
field.setCaretColor(Color.WHITE);
field.setBorder(BorderFactory.createCompoundBorder(new RoundedBorder(10, BG_DARKER), new EmptyBorder(10, 12, 10, 12)));
panel.add(label, BorderLayout.NORTH);
panel.add(field, BorderLayout.CENTER);
return field;
}
private void addMessage(JPanel panel, String user, String text) {
JPanel msg = new JPanel(new BorderLayout(10, 0));
msg.setBackground(BG_MEDIUM);
msg.setBorder(new EmptyBorder(10, 20, 10, 20));
msg.setMaximumSize(new Dimension(Integer.MAX_VALUE, 80));
JLabel info = new JLabel(user + " — Сегодня в 14:55");
info.setForeground(TEXT_MUTED);
info.setFont(new Font("SansSerif", Font.BOLD, 12));
JLabel body = new JLabel("<html><body style='width: 400px'>" + text + "</body></html>");
body.setForeground(TEXT_MAIN);
msg.add(info, BorderLayout.NORTH);
msg.add(body, BorderLayout.CENTER);
panel.add(msg);
panel.revalidate();
}
private void styleButton(JButton btn) {
btn.setBackground(ACCENT);
btn.setForeground(Color.WHITE);
btn.setFocusPainted(false);
btn.setFont(new Font("SansSerif", Font.BOLD, 15));
btn.setBorder(new EmptyBorder(14, 0, 14, 0));
btn.setCursor(new Cursor(Cursor.HAND_CURSOR));
btn.setAlignmentX(Component.CENTER_ALIGNMENT);
btn.setMaximumSize(new Dimension(360, 50));
}
private JPanel createSquircleIcon(String text, Color bg) {
JPanel p = new JPanel() {
@Override protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setColor(bg);
g2.fillRoundRect(0, 0, 48, 48, 16, 16);
g2.setColor(Color.WHITE);
g2.setFont(new Font("SansSerif", Font.BOLD, 16));
FontMetrics fm = g2.getFontMetrics();
g2.drawString(text, (48-fm.stringWidth(text))/2, (48-fm.getHeight())/2 + fm.getAscent());
}
};
p.setPreferredSize(new Dimension(48, 48));
p.setBackground(BG_DARKER);
return p;
}
// --- КЛАССЫ ОБЕРТКИ ---
class LoadingOverlay extends JPanel {
private double angleX = 0, angleY = 0;
private Timer timer;
LoadingOverlay() {
setBackground(BG_DARKER);
timer = new Timer(16, e -> { angleX += 0.03; angleY += 0.04; repaint(); });
timer.start();
}
void stopAnimation() { timer.stop(); }
@Override protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
int size = 80, cx = getWidth()/2, cy = getHeight()/2;
g2.setColor(ACCENT);
g2.setStroke(new BasicStroke(3));
g2.drawRect(cx-size/2, cy-size/2, size, size); // Упрощенный куб для примера
g2.setColor(Color.WHITE);
String t = "Проверка безопасности...";
g2.drawString(t, cx - g2.getFontMetrics().stringWidth(t)/2, cy + 100);
}
}
class RoundedPanel extends JPanel {
private int r; Color c;
RoundedPanel(int r, Color c) { this.r = r; this.c = c; setOpaque(false); }
@Override protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setColor(c);
g2.fillRoundRect(0, 0, getWidth(), getHeight(), r, r);
}
}
class RoundedBorder extends javax.swing.border.AbstractBorder {
private int r; Color c;
RoundedBorder(int r, Color c) { this.r = r; this.c = c; }
@Override public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) {
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setColor(this.c);
g2.drawRoundRect(x, y, w-1, h-1, r, r);
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new NexusApp().setVisible(true));
}
}