-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.java
More file actions
192 lines (149 loc) · 5.79 KB
/
Copy pathclient.java
File metadata and controls
192 lines (149 loc) · 5.79 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
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.SimpleDateFormat;
import java.util.*;
import java.net.*;
import java.io.*;
class Client implements ActionListener {
JTextField text;
static JPanel p2;
static Box vertical = Box.createVerticalBox();
static JFrame f = new JFrame();
static DataOutputStream dout;
Client() {
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);
ImageIcon i1 = new ImageIcon("icons/3 (1).png");
Image i2 = i1.getImage().getScaledInstance(25,25,Image.SCALE_DEFAULT);
JLabel back = new JLabel(new ImageIcon(i2));
back.setBounds(5,20,25,25);
p1.add(back);
ImageIcon i7 = new ImageIcon("icons/video (1).png");
Image i8 = i7.getImage().getScaledInstance(50,50,Image.SCALE_DEFAULT);
ImageIcon i9 = new ImageIcon (i8);
JLabel video = new JLabel (i9);
video.setBounds(300,20,30,30);
p1.add(video);
ImageIcon i10 = new ImageIcon("icons/phone (1).png");
Image i11 = i10.getImage().getScaledInstance(50,50,Image.SCALE_DEFAULT);
ImageIcon i12 = new ImageIcon (i11);
JLabel phone = new JLabel (i12);
phone.setBounds(360,20,35,30);
p1.add(phone);
ImageIcon i13 = new ImageIcon("icons/3icon (1).png");
Image i14 = i13.getImage().getScaledInstance(50,50,Image.SCALE_DEFAULT);
ImageIcon i15 = new ImageIcon (i14);
JLabel icon3 = new JLabel (i15);
icon3.setBounds(400,20,30,30);
p1.add(icon3);
back.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent ae){
System.exit(0);
}
});
ImageIcon i4 = new ImageIcon("icons/mirzapur.png");
Image i5 = i4.getImage().getScaledInstance(50,50,Image.SCALE_DEFAULT);
JLabel profile = new JLabel(new ImageIcon(i5));
profile.setBounds(40,10,50,50);
p1.add(profile);
JLabel name = new JLabel("Friend");
name.setBounds(110,15,100,18);
name.setForeground(Color.WHITE);
name.setFont(new Font ("SAN_SERIF", Font.BOLD,18));
p1.add(name);
JLabel status = new JLabel("Online");
status.setBounds(110,35,100,18);
status.setForeground(Color.WHITE);
status.setFont(new Font ("SAN_SERIF", Font.BOLD,14));
p1.add(status);
p2 = new JPanel();
p2.setLayout(new BorderLayout());
JScrollPane scroll = new JScrollPane(p2);
scroll.setBounds(5,75,425,510);
scroll.setBorder(null);
f.add(scroll);
text = new JTextField();
text.setBounds(5,600,310,40);
text.setFont(new Font("SAN_SERIF", Font.PLAIN,16));
f.add(text);
JButton send = new JButton("send");
send.setBounds(320,600,100,40);
send.setBackground(new Color(7,94,84));
send.setForeground(Color.WHITE);
send.setFont(new Font("SAN_SERIF", Font.PLAIN,16));
send.addActionListener(this);
f.add(send);
f.setSize(450,700);
f.setVisible(true);
f.setLocation(200,50);
f.getContentPane().setBackground(Color.WHITE);
}
public void actionPerformed(ActionEvent ae){
try{
String out = text.getText().trim();
if(out.isEmpty()) return;
// SEND TO SERVER
dout.writeUTF(out);
dout.flush();
JPanel p3 = formatLabel(out);
JPanel right = new JPanel(new BorderLayout());
right.add(p3, BorderLayout.LINE_END);
vertical.add(right);
vertical.add(Box.createVerticalStrut(15));
p2.add(vertical, BorderLayout.PAGE_START);
text.setText("");
vertical.revalidate();
f.repaint();
}catch(Exception e){
e.printStackTrace();
}
}
public static JPanel formatLabel(String out){
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
JLabel output = new JLabel("<html><p style=\"width:150px\">" + out + "</p></html>");
panel.add(output);
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
JLabel time = new JLabel(sdf.format(cal.getTime()));
panel.add(time);
return panel;
}
public static void main(String[] args){
new Client();
try{
Socket s = new Socket("127.0.0.1",6001);
System.out.println("Connected to server");
DataInputStream din = new DataInputStream(s.getInputStream());
dout = new DataOutputStream(s.getOutputStream());
// RECEIVING THREAD
new Thread(() -> {
try{
while(true){
String msg = din.readUTF();
System.out.println("Received: " + msg); //
JPanel panel = formatLabel(msg);
JPanel left = new JPanel(new BorderLayout());
left.add(panel, BorderLayout.LINE_START);
SwingUtilities.invokeLater(() -> {
vertical.add(left);
vertical.add(Box.createVerticalStrut(15));
p2.add(vertical, BorderLayout.PAGE_START);
vertical.revalidate();
f.repaint();
});
}
}catch(Exception e){
e.printStackTrace();
}
}).start();
}catch(Exception e){
e.printStackTrace();
}
}
}