-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserThird.java
More file actions
141 lines (111 loc) · 4.14 KB
/
Copy pathUserThird.java
File metadata and controls
141 lines (111 loc) · 4.14 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
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.swing.*;
import javax.swing.border.*;
public class UserThird implements ActionListener, Runnable {
JTextField text;
JPanel a1;
static Box vertical = Box.createVerticalBox();
static JFrame f = new JFrame();
BufferedReader reader;
BufferedWriter writer;
String name = "Babblu Bhaiya";
UserThird() {
f.setLayout(null);
JPanel p1 = new JPanel();
p1.setBackground(new Color(7, 94, 84));
p1.setBounds(0, 0, 450, 70);
p1.setLayout(null);
f.add(p1);
JLabel nameLabel = new JLabel("Mirzapur");
nameLabel.setBounds(110, 15, 200, 18);
nameLabel.setForeground(Color.WHITE);
nameLabel.setFont(new Font("SAN_SERIF", Font.BOLD, 18));
p1.add(nameLabel);
a1 = new JPanel();
a1.setBounds(5, 75, 440, 570);
a1.setBackground(Color.WHITE);
a1.setLayout(new BorderLayout()); // IMPORTANT
f.add(a1);
text = new JTextField();
text.setBounds(5, 655, 310, 40);
text.setFont(new Font("SAN_SERIF", Font.PLAIN, 16));
f.add(text);
JButton send = new JButton("Send");
send.setBounds(320, 655, 123, 40);
send.setBackground(new Color(7, 94, 84));
send.setForeground(Color.WHITE);
send.addActionListener(this);
f.add(send);
f.setSize(450, 700);
f.setLocation(950, 50);
f.setUndecorated(true);
f.setVisible(true);
try {
Socket socket = new Socket("localhost", 6001);
writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
System.out.println("UserThird connected");
} catch (Exception e) {
e.printStackTrace();
}
}
public void actionPerformed(ActionEvent ae) {
try {
String out = name + ": " + text.getText(); // SIMPLIFIED
JPanel p2 = formatLabel(out);
JPanel right = new JPanel(new BorderLayout());
right.setBackground(Color.WHITE);
right.add(p2, BorderLayout.LINE_END);
vertical.add(right);
vertical.add(Box.createVerticalStrut(15));
a1.add(vertical, BorderLayout.PAGE_START);
writer.write(out);
writer.write("\r\n");
writer.flush();
text.setText("");
f.repaint();
f.validate();
} catch (Exception e) {
e.printStackTrace();
}
}
public static JPanel formatLabel(String out) {
JPanel panel = new JPanel();
panel.setBackground(Color.WHITE);
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
JLabel output = new JLabel(out);
output.setFont(new Font("Tahoma", Font.PLAIN, 16));
output.setBackground(new Color(37, 211, 102));
output.setOpaque(true);
output.setBorder(new EmptyBorder(5, 10, 5, 10));
panel.add(output);
return panel;
}
public void run() {
try {
String msg;
while(true) {
msg = reader.readLine();
if (msg.startsWith(name)) continue;
JPanel panel = formatLabel(msg);
JPanel left = new JPanel(new BorderLayout());
left.setBackground(Color.WHITE);
left.add(panel, BorderLayout.LINE_START);
vertical.add(left);
a1.add(vertical, BorderLayout.PAGE_START);
f.repaint();
f.validate();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
UserThird third = new UserThird();
Thread t1 = new Thread(third);
t1.start();
}
}