-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.java
218 lines (170 loc) · 6.31 KB
/
server.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
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
package chat_server;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import java.util.Calendar;
import java.text.SimpleDateFormat;
class Server implements ActionListener{
JPanel p1;
JTextField t1;
JButton b1;
static JPanel a1;
static JFrame f1 = new JFrame();
static Box vertical = Box.createVerticalBox();
static ServerSocket skt;
static Socket s;
static DataInputStream din;
static DataOutputStream dout;
Boolean typing;
Server(){
f1.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
p1 = new JPanel();
p1.setLayout(null);
p1.setBackground(new Color(7, 94, 84));
p1.setBounds(0, 0, 450, 70);
f1.add(p1);
ImageIcon i1 = new ImageIcon(ClassLoader.getSystemResource("chat_server/3.png"));
Image i2 = i1.getImage().getScaledInstance(30, 30, Image.SCALE_DEFAULT);
ImageIcon i3 = new ImageIcon(i2);
JLabel l1 = new JLabel(i3);
l1.setBounds(5, 17, 30, 30);
p1.add(l1);
l1.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent ae){
System.exit(0);
}
});
ImageIcon i4 = new ImageIcon(ClassLoader.getSystemResource("chat_server/1.png"));
Image i5 = i4.getImage().getScaledInstance(60, 60, Image.SCALE_DEFAULT);
ImageIcon i6 = new ImageIcon(i5);
JLabel l2 = new JLabel(i6);
l2.setBounds(40, 5, 60, 60);
p1.add(l2);
ImageIcon i7 = new ImageIcon(ClassLoader.getSystemResource("chat_server/video.png"));
Image i8 = i7.getImage().getScaledInstance(30, 30, Image.SCALE_DEFAULT);
ImageIcon i9 = new ImageIcon(i8);
JLabel l5 = new JLabel(i9);
l5.setBounds(290, 20, 30, 30);
p1.add(l5);
ImageIcon i11 = new ImageIcon(ClassLoader.getSystemResource("chat_server/phone.png"));
Image i12 = i11.getImage().getScaledInstance(35, 30, Image.SCALE_DEFAULT);
ImageIcon i13 = new ImageIcon(i12);
JLabel l6 = new JLabel(i13);
l6.setBounds(350, 20, 35, 30);
p1.add(l6);
ImageIcon i14 = new ImageIcon(ClassLoader.getSystemResource("chat_server/3icon.png"));
Image i15 = i14.getImage().getScaledInstance(13, 25, Image.SCALE_DEFAULT);
ImageIcon i16 = new ImageIcon(i15);
JLabel l7 = new JLabel(i16);
l7.setBounds(410, 20, 13, 25);
p1.add(l7);
JLabel l3 = new JLabel("Gaitonde");
l3.setFont(new Font("SAN_SERIF", Font.BOLD, 18));
l3.setForeground(Color.WHITE);
l3.setBounds(110, 15, 100, 18);
p1.add(l3);
JLabel l4 = new JLabel("Active Now");
l4.setFont(new Font("SAN_SERIF", Font.PLAIN, 14));
l4.setForeground(Color.WHITE);
l4.setBounds(110, 35, 100, 20);
p1.add(l4);
Timer t = new Timer(1, new ActionListener(){
public void actionPerformed(ActionEvent ae){
if(!typing){
l4.setText("Active Now");
}
}
});
t.setInitialDelay(2000);
a1 = new JPanel();
a1.setBounds(5, 75, 440, 570);
a1.setFont(new Font("SAN_SERIF", Font.PLAIN, 16));
f1.add(a1);
t1 = new JTextField();
t1.setBounds(5, 655, 310, 40);
t1.setFont(new Font("SAN_SERIF", Font.PLAIN, 16));
f1.add(t1);
t1.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent ke){
l4.setText("typing...");
t.stop();
typing = true;
}
public void keyReleased(KeyEvent ke){
typing = false;
if(!t.isRunning()){
t.start();
}
}
});
b1 = new JButton("Send");
b1.setBounds(320, 655, 123, 40);
b1.setBackground(new Color(7, 94, 84));
b1.setForeground(Color.WHITE);
b1.setFont(new Font("SAN_SERIF", Font.PLAIN, 16));
b1.addActionListener(this);
f1.add(b1);
f1.getContentPane().setBackground(Color.WHITE);
f1.setLayout(null);
f1.setSize(450, 700);
f1.setLocation(100, 100);
f1.setUndecorated(true);
f1.setVisible(true);
}
public void actionPerformed(ActionEvent ae){
try{
String out = t1.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);
//a1.add(p2);
dout.writeUTF(out);
t1.setText("");
}catch(Exception e){
System.out.println(e);
}
}
public static JPanel formatLabel(String out){
JPanel p3 = new JPanel();
p3.setLayout(new BoxLayout(p3, BoxLayout.Y_AXIS));
JLabel l1 = new JLabel("<html><p style = \"width : 150px\">"+out+"</p></html>");
l1.setFont(new Font("Tahoma", Font.PLAIN, 16));
l1.setBackground(new Color(37, 211, 102));
l1.setOpaque(true);
l1.setBorder(new EmptyBorder(15,15,15,50));
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
JLabel l2 = new JLabel();
l2.setText(sdf.format(cal.getTime()));
p3.add(l1);
p3.add(l2);
return p3;
}
public static void main(String[] args){
new Server().f1.setVisible(true);
String msginput = "";
try{
skt = new ServerSocket(6001);
while(true){
s = skt.accept();
din = new DataInputStream(s.getInputStream());
dout = new DataOutputStream(s.getOutputStream());
while(true){
msginput = din.readUTF();
JPanel p2 = formatLabel(msginput);
JPanel left = new JPanel(new BorderLayout());
left.add(p2, BorderLayout.LINE_START);
vertical.add(left);
f1.validate();
}
}
}catch(Exception e){}
}
}