-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClient.java
180 lines (142 loc) · 5.67 KB
/
Client.java
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
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.text.*;
import java.net.*;
import java.io.*;
public class Client implements ActionListener {
JTextField text;
static JPanel a1;
static Box vertical = Box.createVerticalBox();
static JFrame f = new JFrame();
static DataOutputStream dout;
Client() { // constructor
f.setLayout(null);
// panel p1 for the header
JPanel p1 = new JPanel();
p1.setBackground(Color.GREEN);
p1.setBounds(0, 0, 450, 70);
p1.setLayout(null);
f.add(p1);
// back button
ImageIcon i1 = new ImageIcon(ClassLoader.getSystemResource("images/254427.png"));
Image i2 = i1.getImage().getScaledInstance(25, 25, Image.SCALE_DEFAULT);
ImageIcon i3 = new ImageIcon(i2);
JLabel back = new JLabel(i3);
back.setBounds(5, 20, 25, 25);
p1.add(back);
back.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent ae) {
f.setVisible(false);
}
});
// addtional icons
addIcon(p1, "images/video.jpg", 300, 20, 35, 30);
addIcon(p1, "images/call.png", 340, 20, 35, 30);
addIcon(p1, "images/3151505.png", 380, 20, 35, 30);
// name and status labels
JLabel name = new JLabel("Smith");
name.setBounds(50, 15, 200, 15);
name.setForeground(Color.black);
name.setFont(new Font("SAN_SERIF", Font.BOLD, 18));
p1.add(name);
JLabel status = new JLabel("Active Now");
status.setBounds(50, 30, 200, 15);
status.setForeground(Color.black);
status.setFont(new Font("SAN_SERIF", Font.BOLD, 14));
p1.add(status);
// main panel for messages
a1 = new JPanel();
a1.setBounds(5, 75, 425, 570);
a1.setLayout(new BorderLayout());
f.add(a1);
// text field for input
text = new JTextField();
text.setBounds(5, 655, 310, 40);
text.setFont(new Font("SAN_SERIF", Font.PLAIN, 16));
f.add(text);
// send button
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);
send.setFont(new Font("SAN_SERIF", Font.PLAIN, 18));
f.add(send);
f.setSize(450, 700);
f.setLocation(200, 50);
f.setUndecorated(true);
f.getContentPane().setBackground(Color.white);
f.setVisible(true);
}
private void addIcon(JPanel panel, String path, int x, int y, int width, int height) {
ImageIcon icon = new ImageIcon(ClassLoader.getSystemResource(path));
Image img = icon.getImage().getScaledInstance(width, height, Image.SCALE_DEFAULT);
ImageIcon scaledIcon = new ImageIcon(img);
JLabel label = new JLabel(scaledIcon);
label.setBounds(x, y, width, height);
panel.add(label);
}
public void actionPerformed(ActionEvent ae) {
try {
String out = text.getText();
JPanel p2 = formatLabel(out);
a1.setLayout(new BorderLayout());
JPanel right = new JPanel(new BorderLayout());
right.add(p2, BorderLayout.LINE_END);
vertical.add(right);
vertical.add(Box.createVerticalStrut(15));
a1.add(vertical, BorderLayout.PAGE_START);
dout.writeUTF(out);
text.setText(" ");
f.repaint();
f.revalidate();
} 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 : 150 px\">" + out + "</p></html>");
output.setFont(new Font("Tahoma", Font.PLAIN, 16));
output.setBackground(new Color(37, 211, 102));
output.setOpaque(true);
output.setBorder(new EmptyBorder(15, 15, 15, 15));
panel.add(output);
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
JLabel time = new JLabel();
time.setText(sdf.format(cal.getTime()));
panel.add(time);
return panel;
}
public static void main(String[] agrs) {
new Client(); // craete the GUI
// start server in a separate thread to avoid blocking EDT
new Thread(() -> {
String serverAddress = "127.0.0.1";
int port = 5500;
try {
Socket s = new Socket(serverAddress, port);
DataInputStream din = new DataInputStream(s.getInputStream());
dout = new DataOutputStream(s.getOutputStream());
while (true) {
a1.setLayout(new BorderLayout());
String msg = din.readUTF();
JPanel panel = formatLabel(msg);
JPanel left = new JPanel(new BorderLayout());
left.add(panel, BorderLayout.LINE_START);
vertical.add(left);
vertical.add(Box.createVerticalStrut(15));
a1.add(vertical, BorderLayout.PAGE_START);
f.revalidate();
}
} catch (Exception e) {
e.printStackTrace();
}
}).start();
}
}