-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChatClient.java
135 lines (113 loc) · 4.43 KB
/
ChatClient.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
package assignment7;
import java.io.*;
import java.net.*;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.swing.*;
import javafx.application.Application;
import javafx.scene.control.TextField;
import java.awt.*;
import java.awt.event.*;
public class ChatClient {
private JTextArea incoming;
private JTextField outgoing;
private JTextField newserver;
private BufferedReader reader;
private PrintWriter writer;
public int serverport = 0;
public String ipaddy = Login.ipaddress;
public void run() throws Exception {
serverport = Login.serverport;
initView();
setUpNetworking(serverport);
}
private void initView() {
JFrame frame = new JFrame(Login.user);
JPanel mainPanel = new JPanel();
//mainPanel.getRootPane().setBackground(Color.green);
incoming = new JTextArea(15, 50);
incoming.setLineWrap(true);
incoming.setWrapStyleWord(true);
incoming.setEditable(false);
JScrollPane qScroller = new JScrollPane(incoming);
qScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
qScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
outgoing = new JTextField(20);
newserver = new JTextField(20);
JButton sendButton = new JButton("Send");
sendButton.addActionListener(new SendButtonListener());
mainPanel.add(qScroller);
JButton serverButton = new JButton("Change Server");
sendButton.addActionListener(new ServerButtonListener());
mainPanel.add(outgoing);
mainPanel.add(serverButton);
mainPanel.add(sendButton);
mainPanel.add(newserver);
mainPanel.add(serverButton);
frame.getContentPane().add(BorderLayout.CENTER, mainPanel);
frame.setSize(650, 500);
frame.setVisible(true);
}
private void setUpNetworking(int i) throws Exception {
@SuppressWarnings("resource")
Socket sock = new Socket(ipaddy, i);
InputStreamReader streamReader = new InputStreamReader(sock.getInputStream());
reader = new BufferedReader(streamReader);
writer = new PrintWriter(sock.getOutputStream());
System.out.println("networking established");
Thread readerThread = new Thread(new IncomingReader());
readerThread.start();
}
class SendButtonListener implements ActionListener {
public void actionPerformed(ActionEvent ev) {
try {
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File("src/assignment7/Toggle-SoundBible.com-231290292.wav").getAbsoluteFile());
Clip clip = AudioSystem.getClip();
clip.open(audioInputStream);
clip.start();
} catch(Exception ex) {
System.out.println("Error with playing sound.");
ex.printStackTrace();
}
writer.println(Login.user + ": " + outgoing.getText());
writer.flush();
outgoing.setText("");
outgoing.requestFocus();
}
}
class ServerButtonListener implements ActionListener {
public void actionPerformed(ActionEvent ev) {
//writer.println(Login.user + " is leaving the chat");
try {
setUpNetworking(Integer.parseInt(newserver.getText()));
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String[] args) {
try {
Application.launch(Login.class);
new ChatClient().run();
} catch (Exception e) {
e.printStackTrace();
}
}
class IncomingReader implements Runnable {
public void run() {
String message;
try {
while ((message = reader.readLine()) != null) {
incoming.append(message + "\n");
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}