-
Notifications
You must be signed in to change notification settings - Fork 0
/
Server.java
174 lines (149 loc) · 5.54 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
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class Server extends JFrame implements ActionListener {
JPanel p1;
JTextField t1;
JButton b1;
static JButton b2;
static JTextArea a1;
static JTextArea a2;
static ServerSocket serverSocket;
static Socket socket;
static DataOutputStream ous;
static DataInputStream ois;
Server() {
p1 = new JPanel();
p1.setLayout(null); // default border layout removed and set to null.
p1.setBackground(new Color(7, 94, 84));
p1.setBounds(0, 0, 450, 50);
add(p1);
ImageIcon i1 = new ImageIcon(ClassLoader.getSystemResource("icons/icons/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, 12, 30, 30);
p1.add(l1);
l1.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
System.exit(0);
}
});
ImageIcon i4 = new ImageIcon(ClassLoader.getSystemResource("icons/icons/1.png"));
Image i5 = i4.getImage().getScaledInstance(40, 40, Image.SCALE_DEFAULT);
ImageIcon i6 = new ImageIcon(i5);
JLabel l2 = new JLabel(i6);
l2.setBounds(40, 5, 40, 40);
p1.add(l2);
JLabel l3 = new JLabel("Server");
l3.setFont(new Font("Font.SERIF", Font.BOLD, 15));
l3.setForeground(Color.WHITE);
l3.setBounds(85, 5, 100, 30);
p1.add(l3);
JLabel l4 = new JLabel("Active now");
l4.setFont(new Font("Font.SERIF", Font.PLAIN, 10));
l4.setForeground(Color.WHITE);
l4.setBounds(85, 25, 100, 20);
p1.add(l4);
ImageIcon i7 = new ImageIcon(ClassLoader.getSystemResource("icons/icons/video.png"));
Image i8 = i7.getImage().getScaledInstance(30, 30, Image.SCALE_DEFAULT);
ImageIcon i9 = new ImageIcon(i8);
JLabel l5 = new JLabel(i9);
l5.setBounds(335, 10, 30, 30);
p1.add(l5);
ImageIcon i10 = new ImageIcon(ClassLoader.getSystemResource("icons/icons/phone.png"));
Image i11 = i10.getImage().getScaledInstance(30, 30, Image.SCALE_DEFAULT);
ImageIcon i12 = new ImageIcon(i11);
JLabel l6 = new JLabel(i12);
l6.setBounds(380, 10, 30, 30);
p1.add(l6);
ImageIcon i13 = new ImageIcon(ClassLoader.getSystemResource("icons/icons/3icon.png"));
Image i14 = i13.getImage().getScaledInstance(10, 20, Image.SCALE_DEFAULT);
ImageIcon i15 = new ImageIcon(i14);
JLabel l7 = new JLabel(i15);
l7.setBounds(425, 15, 10, 20);
p1.add(l7);
a1 = new JTextArea();
a1.setBounds(5, 55, 220, 595);
a1.setBackground(Color.CYAN);
a1.setFont(new Font(Font.SANS_SERIF, Font.ITALIC, 20));
a1.setEditable(false);
a1.setLineWrap(true);
a1.setWrapStyleWord(true);
add(a1);
a2 = new JTextArea();
a2.setBounds(225, 55, 220, 595);
a2.setBackground(Color.CYAN);
a2.setFont(new Font(Font.SANS_SERIF, Font.ITALIC, 20));
a2.setEditable(false);
a2.setLineWrap(true);
a2.setWrapStyleWord(true);
add(a2);
t1 = new JTextField();
t1.setBounds(90, 655, 270, 40);
t1.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 20));
add(t1);
b1 = new JButton("Send");
b1.setBounds(365, 655, 80, 40);
b1.setBackground(new Color(7, 94, 84));
b1.setForeground(Color.WHITE);
b1.addActionListener(this);
add(b1);
b2 = new JButton("File");
b2.setBounds(5, 655, 80, 40);
b2.setBackground(new Color(7, 94, 84));
b2.setForeground(Color.WHITE);
b2.addActionListener(this);
add(b2);
getContentPane().setBackground(Color.WHITE);
setLayout(null);
setSize(450, 700);
setLocation(300, 50);
setUndecorated(true);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
try {
String msg = t1.getText();
if (!msg.isEmpty()) {
a2.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
a2.setText(a2.getText() + "\n" + msg);
long u = (long) Math.ceil((double) msg.length() / 20);
for (long i = 0; i < u; i++) {
a1.setText(a1.getText() + "\n");
}
ous.writeUTF(msg);
}
t1.setText("");
} catch (IOException ex) {
ex.printStackTrace();
}
}
public static void main(String[] args) {
new Server();
try {
serverSocket = new ServerSocket(6001);
socket = serverSocket.accept();
ois = new DataInputStream(socket.getInputStream());
ous = new DataOutputStream(socket.getOutputStream());
while (true) {
String cMsg = ois.readUTF();
long z = (long) Math.ceil((double) cMsg.length() / 20);
a1.setText(a1.getText() + "\n" + cMsg);
for (long i = 0; i < z; i++) {
a2.setText(a2.getText() + "\n");
}
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}