-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClientGUI.java
More file actions
320 lines (274 loc) · 11.1 KB
/
ClientGUI.java
File metadata and controls
320 lines (274 loc) · 11.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
import java.awt.*;
import java.io.*;
import java.net.Socket;
import javax.swing.*;
public class ClientGUI extends JFrame {
private JTextField serverIdField;
private JPasswordField passwordField;
private JTextField inputField;
private JTextArea chatArea;
private JButton sendButton, attachButton, voiceCallButton, videoCallButton;
private JCheckBox showPasswordCheckbox;
private Socket socket;
private BufferedReader br;
private PrintStream ps;
private boolean isConnected;
private JFrame videoCallFrame;
private JButton receiveCallButton, endCallButton, cancelCallButton;
private CameraPanel cameraPanel;
public ClientGUI() {
createLoginUI();
}
private void createLoginUI() {
setTitle("Chat Application - Login");
setSize(400, 300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(new BorderLayout());
JPanel loginPanel = new JPanel(new GridBagLayout());
loginPanel.setBackground(new Color(240, 248, 255));
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(5, 5, 5, 5);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 0;
gbc.gridy = 0;
JLabel serverLabel = createLabel("Server ID:");
loginPanel.add(serverLabel, gbc);
gbc.gridx = 1;
serverIdField = new JTextField(15);
loginPanel.add(serverIdField, gbc);
gbc.gridx = 0;
gbc.gridy = 1;
JLabel passwordLabel = createLabel("Password:");
loginPanel.add(passwordLabel, gbc);
gbc.gridx = 1;
passwordField = new JPasswordField(15);
loginPanel.add(passwordField, gbc);
gbc.gridy = 2;
showPasswordCheckbox = new JCheckBox("Show Password");
showPasswordCheckbox.setBackground(new Color(240, 248, 255));
showPasswordCheckbox.addActionListener(e -> passwordField.setEchoChar(
showPasswordCheckbox.isSelected() ? '\0' : '●'));
loginPanel.add(showPasswordCheckbox, gbc);
gbc.gridy = 3;
JButton loginButton = createButton("Login", new Color(70, 130, 180));
loginButton.addActionListener(e -> attemptLogin());
loginPanel.add(loginButton, gbc);
add(loginPanel, BorderLayout.CENTER);
setVisible(true);
}
private JLabel createLabel(String text) {
JLabel label = new JLabel(text);
label.setForeground(new Color(25, 25, 112));
return label;
}
private JButton createButton(String text, Color color) {
JButton button = new JButton(text);
button.setBackground(color);
button.setForeground(Color.WHITE);
button.setFont(new Font("Segoe UI", Font.BOLD, 14));
return button;
}
private void attemptLogin() {
String serverId = serverIdField.getText().trim();
String password = new String(passwordField.getPassword()).trim();
if (serverId.isEmpty() || password.isEmpty()) {
JOptionPane.showMessageDialog(this, "Please fill out all fields.");
return;
}
try {
socket = new Socket("localhost", 2100);
ps = new PrintStream(socket.getOutputStream(), true);
br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
ps.println(serverId);
ps.println(password);
String response = br.readLine();
if ("SUCCESS".equals(response)) {
isConnected = true;
JOptionPane.showMessageDialog(this, "Server connected successfully!");
createMainUI();
} else {
JOptionPane.showMessageDialog(this, "Login failed.");
}
} catch (IOException e) {
JOptionPane.showMessageDialog(this, "Unable to connect to server.");
}
}
private void createMainUI() {
setTitle("Chat Application");
setSize(800, 600);
getContentPane().removeAll();
setLayout(new BorderLayout());
chatArea = new JTextArea();
chatArea.setEditable(false);
chatArea.setLineWrap(true);
chatArea.setWrapStyleWord(true);
chatArea.setFont(new Font("Segoe UI", Font.PLAIN, 14));
chatArea.setBackground(new Color(255, 255, 255));
chatArea.setForeground(new Color(0, 51, 102));
JScrollPane chatScroll = new JScrollPane(chatArea);
inputField = new JTextField();
inputField.setFont(new Font("Segoe UI", Font.PLAIN, 14));
inputField.addActionListener(e -> sendMessage());
JPanel bottomPanel = new JPanel(new BorderLayout());
JPanel buttonPanel = new JPanel(new FlowLayout());
sendButton = createButton("Send", new Color(34, 139, 34));
sendButton.addActionListener(e -> sendMessage());
attachButton = createButton("Attach", new Color(255, 165, 0));
attachButton.addActionListener(e -> sendFile());
voiceCallButton = createButton("Voice Call", new Color(30, 144, 255));
voiceCallButton.addActionListener(e -> initiateVoiceCall());
videoCallButton = createButton("Video Call", new Color(138, 43, 226));
videoCallButton.addActionListener(e -> initiateVideoCall());
buttonPanel.add(sendButton);
buttonPanel.add(attachButton);
buttonPanel.add(voiceCallButton);
buttonPanel.add(videoCallButton);
bottomPanel.add(inputField, BorderLayout.CENTER);
bottomPanel.add(buttonPanel, BorderLayout.EAST);
add(chatScroll, BorderLayout.CENTER);
add(bottomPanel, BorderLayout.SOUTH);
new Thread(this::receiveMessages).start();
setVisible(true);
}
private void sendMessage() {
String msg = inputField.getText().trim();
if (!msg.isEmpty() && isConnected) {
ps.println(msg);
chatArea.append("You: " + msg + "\n");
inputField.setText("");
}
}
private void initiateVideoCall() {
chatArea.append("Starting video call...\n");
SwingUtilities.invokeLater(() -> {
videoCallFrame = new JFrame("Video Call");
videoCallFrame.setSize(800, 600);
videoCallFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
videoCallFrame.setLayout(new BorderLayout());
JLabel callStatus = new JLabel("Connecting...");
callStatus.setHorizontalAlignment(SwingConstants.CENTER);
callStatus.setFont(new Font("Segoe UI", Font.BOLD, 16));
videoCallFrame.add(callStatus, BorderLayout.CENTER);
JPanel callControlPanel = new JPanel(new FlowLayout());
receiveCallButton = createButton("Receive Call", new Color(34, 139, 34));
receiveCallButton.addActionListener(e -> {
callStatus.setText("Call Connected");
startCamera();
});
endCallButton = createButton("End Call", new Color(255, 69, 0));
endCallButton.addActionListener(e -> {
callStatus.setText("Call Ended");
videoCallFrame.dispose();
});
cancelCallButton = createButton("Cancel Call", new Color(255, 165, 0));
cancelCallButton.addActionListener(e -> {
callStatus.setText("Call Canceled");
videoCallFrame.dispose();
});
callControlPanel.add(receiveCallButton);
callControlPanel.add(endCallButton);
callControlPanel.add(cancelCallButton);
videoCallFrame.add(callControlPanel, BorderLayout.SOUTH);
videoCallFrame.setVisible(true);
});
}
private void startCamera() {
cameraPanel = new CameraPanel();
videoCallFrame.getContentPane().add(cameraPanel, BorderLayout.CENTER);
cameraPanel.startCamera();
}
private class CameraPanel extends JPanel {
private Dimension cameraResolution = new Dimension(640, 480);
private boolean isRunning = false;
public void startCamera() {
isRunning = true;
new Thread(() -> {
while (isRunning) {
repaint();
try {
Thread.sleep(40);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (isRunning) {
g.setColor(Color.BLACK);
g.fillRect(0, 0, getWidth(), getHeight());
}
}
}
private void sendFile() {
JFileChooser fileChooser = new JFileChooser();
if (fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
try {
ps.println("[FILE_TRANSFER]");
ps.println(file.getName());
FileInputStream fis = new FileInputStream(file);
OutputStream os = socket.getOutputStream();
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = fis.read(buffer)) != -1) {
os.write(buffer, 0, bytesRead);
}
os.flush();
fis.close();
chatArea.append("File sent: " + file.getName() + "\n");
} catch (IOException e) {
chatArea.append("Failed to send file.\n");
}
}
}
private void receiveMessages() {
try {
String line;
while (isConnected && (line = br.readLine()) != null) {
String msg = line;
if ("[FILE_TRANSFER]".equals(msg)) {
receiveFile();
} else {
SwingUtilities.invokeLater(() -> chatArea.append("Server: " + msg + "\n"));
}
}
} catch (IOException e) {
JOptionPane.showMessageDialog(this, "Connection lost.");
isConnected = false;
}
}
private void receiveFile() {
try {
String fileName = br.readLine();
File receivedFile = new File(fileName);
FileOutputStream fos = new FileOutputStream(receivedFile);
InputStream is = socket.getInputStream();
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = is.read(buffer)) != -1) {
fos.write(buffer, 0, bytesRead);
}
fos.close();
chatArea.append("File received: " + fileName + "\n");
} catch (IOException e) {
chatArea.append("Failed to receive file.\n");
}
}
private void initiateVoiceCall() {
chatArea.append("Starting voice call...\n");
new Thread(() -> {
try {
// Implement voice call logic here
} catch (Exception e) {
chatArea.append("Voice call failed: " + e.getMessage() + "\n");
}
}).start();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(ClientGUI::new);
}
}