-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChat.java
More file actions
187 lines (155 loc) · 4.17 KB
/
Chat.java
File metadata and controls
187 lines (155 loc) · 4.17 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
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.*;
import javax.swing.*;
import java.net.*;
import java.io.*;
public class Chat implements ActionListener
{
JFrame mainframe;
JPanel panel1,panel2,panel3;
JTextArea textarea;
JTextArea contactarea;
JTextArea chatarea;
JButton sendbutton,logoutbutton;
Socket soc;
DataInputStream din;
DataOutputStream dout;
JScrollPane chatscroll,contactscroll;
InetAddress i;
Border blackline;
String name,ss="";
int flag=-1;
public Chat(String s,String name)
{
try
{
soc = new Socket("localhost",1034);
dout = new DataOutputStream(soc.getOutputStream());
}catch(Exception e)
{ System.out.println(e); }
this.name=name;
sendbutton=new JButton("Send");
logoutbutton=new JButton("Logout");
mainframe=new JFrame(s);
textarea = new JTextArea("Enter Text here.");
contactarea = new JTextArea();
chatarea = new JTextArea();
panel1=new JPanel();
panel2=new JPanel();
panel3=new JPanel();
sendbutton.setBounds(20,50,175,75);
logoutbutton.setBounds(380,50,175,75);
textarea.setBounds(40,50,500,100);
sendbutton.addActionListener(this);
logoutbutton.addActionListener(this);
mainframe.setLayout(new GridLayout(3,1));
mainframe.add(panel1);
mainframe.add(panel2);
mainframe.add(panel3);
panel1.setLayout(new GridLayout(1,2));
My m=new My(chatarea,contactarea,soc);
Thread t1=new Thread(m);
t1.start();
chatscroll= new JScrollPane(chatarea);
contactscroll=new JScrollPane(contactarea);
panel1.add(chatscroll);
chatarea.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder("Chat"),BorderFactory.createEmptyBorder(5,5,5,5)),chatarea.getBorder()));
panel1.add(contactscroll);
contactarea.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder("Chat Log"),BorderFactory.createEmptyBorder(5,5,5,5)),contactarea.getBorder()));
contactarea.setLineWrap(true);
contactarea.setWrapStyleWord(true);
chatarea.setEditable(false);
contactarea.setEditable(false);
panel2.setLayout(null);
textarea.setLineWrap(true);
textarea.setWrapStyleWord(true);
panel2.add(textarea);
textarea.addKeyListener(new KeyListener() {
public void keyTyped(KeyEvent e) {
}
public void keyPressed(KeyEvent e) {
if ((e.getKeyCode() == KeyEvent.VK_ENTER) && ((e.getModifiers() & KeyEvent.CTRL_MASK) != 0)) {
try{
clientChat();}catch(Exception ex){}
}
}
public void keyReleased(KeyEvent e) {
}
});
panel2.setBackground(Color.PINK);
panel3.setLayout(null);
panel3.setBackground(Color.BLACK);
panel3.add(sendbutton);
panel3.add(logoutbutton);
mainframe.setSize(600,600);
mainframe.setVisible(true);
mainframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==sendbutton)
{try{
clientChat();
}catch(Exception ef){}
}
if(e.getSource()==logoutbutton)
{
try{
dout.writeUTF(name+" logged out");
dout.flush();
System.exit(0);}catch(Exception el){}
}
}
public void clientChat() throws IOException
{
if(flag==-1)
{
ss=name+" logged in";
dout.writeUTF(ss);
dout.flush();
flag=0;
}
String s1="";
s1=name+":"+textarea.getText();
dout.writeUTF(s1);
textarea.setText(null);
dout.flush();
}
}
class My implements Runnable
{
DataInputStream din;
JTextArea chatarea,contactarea;
//Socket soc;
public My(JTextArea chatarea,JTextArea contactarea,Socket soc)
{try{
//soc = new Socket("localhost",1034);
din= new DataInputStream(soc.getInputStream());}
catch(Exception ed){}
this.chatarea=chatarea;
this.contactarea=contactarea;
}
public void run()
{
String s2="";
String s1;
try{
do
{
s1=din.readUTF();
if(s1.endsWith("logged out"))
{
contactarea.append(s1+"\n");}
if(s1.endsWith("logged in"))
{
contactarea.append(s1+"\n");
}
else
{
chatarea.append(s1+"\n");
}
}while(true);
}catch(Exception e){}
}
}