-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient1.java
More file actions
189 lines (161 loc) · 6.26 KB
/
Copy pathClient1.java
File metadata and controls
189 lines (161 loc) · 6.26 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
package SocketProgramming;
import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.net.Socket;
import java.util.Scanner;
public class Client1 extends Thread {
InputStream readerStream;
OutputStream writerStream;
PrintWriter writer;
JTextArea textArea;
JTextField textField;
public Client1(InputStream r, OutputStream o) {
this.readerStream = r;
this.writerStream = o;
this.writer = new PrintWriter(writerStream, true);
}
@Override
public void run() {
// Start GUI
new createGuI();
// Start Reader and Writer threads
Reader r = new Reader(readerStream, textArea);
Writer w = new Writer(writerStream, textField, textArea);
r.start();
w.start();
try {
r.join();
w.join();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
class createGuI {
JFrame frame;
JButton button;
JPanel inputPanel;
JLabel headingLabel;
public createGuI() {
frame = new JFrame("Client Chat");
frame.setSize(500, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout(10, 10));
frame.getContentPane().setBackground(Color.WHITE);
ImageIcon icon1Raw = new ImageIcon("E:/ExceptionHandling(W3RESOURCE)/Practise random/src/SocketProgramming/chat.png");
Image icon1Image = icon1Raw.getImage().getScaledInstance(30, 30, Image.SCALE_SMOOTH);
ImageIcon icon1 = new ImageIcon(icon1Image);
ImageIcon icon2Raw = new ImageIcon("E:\\ExceptionHandling(W3RESOURCE)\\Practise random\\src\\SocketProgramming\\send.png");
Image icon2Image = icon2Raw.getImage().getScaledInstance(30, 30, Image.SCALE_SMOOTH);
ImageIcon icon2 = new ImageIcon(icon2Image);
headingLabel = new JLabel(" Chat Application",icon1, JLabel.LEFT);
headingLabel.setFont(new Font("Arial Black", Font.BOLD, 22));
headingLabel.setOpaque(true);
headingLabel.setBackground(new Color(30, 144, 255));
headingLabel.setForeground(Color.WHITE);
headingLabel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
frame.add(headingLabel, BorderLayout.NORTH);
// Text Area
textArea = new JTextArea();
textArea.setEditable(false);
textArea.setFont(new Font("SansSerif", Font.PLAIN, 16));
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
JScrollPane scrollPane = new JScrollPane(textArea);
scrollPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
frame.add(scrollPane, BorderLayout.CENTER);
// Bottom Panel
inputPanel = new JPanel(new BorderLayout(10, 10));
inputPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
textField = new JTextField("Type your message...");
textField.setFont(new Font("SansSerif", Font.PLAIN, 16));
textField.setForeground(Color.GRAY);
textField.addFocusListener(new java.awt.event.FocusAdapter() {
public void focusGained(java.awt.event.FocusEvent e) {
if (textField.getText().equals("Type your message...")) {
textField.setText("");
textField.setForeground(Color.BLACK);
}
}
public void focusLost(java.awt.event.FocusEvent e) {
if (textField.getText().isEmpty()) {
textField.setText("Type your message...");
textField.setForeground(Color.GRAY);
}
}
});
button = new JButton(icon2);
button.setPreferredSize(new Dimension(60, 40));
button.setBackground(new Color(30, 144, 255));
button.setToolTipText("Send message");
button.setFocusable(false);
inputPanel.add(textField, BorderLayout.CENTER);
inputPanel.add(button, BorderLayout.EAST);
frame.add(inputPanel, BorderLayout.SOUTH);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
button.addActionListener(e -> sendMessage());
// Enter key also sends message
textField.addActionListener(e -> sendMessage());
}
public void sendMessage() {
String msg = textField.getText().trim();
if (!msg.isEmpty() && !msg.equalsIgnoreCase("Type your message...")) {
writer.println(msg);
textArea.append("Me: " + msg + "\n");
textField.setText("");
}
}
}
public static void main(String[] args) {
System.out.println("Client started ......");
try {
Socket s = new Socket("localhost", 9999);
Client1 c = new Client1(s.getInputStream(), s.getOutputStream());
c.start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
class Reader extends Thread {
BufferedReader br;
JTextArea textArea;
public Reader(InputStream r, JTextArea textArea) {
br = new BufferedReader(new InputStreamReader(r));
this.textArea = textArea;
}
@Override
public void run() {
while (true) {
try {
String lines = br.readLine();
if (lines == null || lines.equalsIgnoreCase("exit")) {
break;
}
textArea.append("Server: " + lines + "\n");
} catch (IOException e) {
break;
}
}
}
}
class Writer extends Thread {
Scanner sc = new Scanner(System.in);
PrintWriter p;
JTextField input;
JTextArea output;
public Writer(OutputStream o, JTextField input, JTextArea output) {
p = new PrintWriter(o, true);
this.input = input;
this.output = output;
}
@Override
public void run() {
while (true) {
String msg = sc.nextLine();
p.println(msg);
if (msg.equalsIgnoreCase("exit")) break;
}
}
}