-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatClient2.java
More file actions
136 lines (119 loc) · 5.02 KB
/
ChatClient2.java
File metadata and controls
136 lines (119 loc) · 5.02 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
import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
public class ChatClient2 extends JFrame{
String serverAddress;
Scanner in;
PrintWriter out;
JFrame frame = new JFrame("Chatter");
JTextField textField = new JTextField(50);
JTextPane messageArea = new JTextPane();
public ChatClient2() {
this.serverAddress = "localhost";
centerFrame();
messageArea.setPreferredSize(new Dimension(700, 700) );
textField.setEditable(false);
messageArea.setEditable(false);
frame.getContentPane().add(textField, BorderLayout.SOUTH);
frame.setSize(700,700);
frame.getContentPane().add(new JScrollPane(messageArea), BorderLayout.CENTER);
frame.pack();
// Send on enter then clear to prepare for next message
textField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
out.println(textField.getText());
textField.setText("");
}
});
}
private String getUserName() {
return JOptionPane.showInputDialog(frame, "Escolha um nome de usuário:", "Nome de Usuário",
JOptionPane.PLAIN_MESSAGE);
}
private String getColor() {
return String.valueOf(JColorChooser.showDialog(frame, "Escolha uma cor", Color.red).getRGB());
}
private void run() throws IOException {
try {
Socket socket = new Socket(serverAddress, 59898);
in = new Scanner(socket.getInputStream());
out = new PrintWriter(socket.getOutputStream(), true);
while (in.hasNextLine()) {
String line = in.nextLine();
if (line.startsWith("SUBMITNAME")) {
out.println(getUserName());
} else if(line.startsWith("USERCOLOR")){
out.println(getColor());
} else if (line.startsWith("NAMEACCEPTED")) {
this.frame.setTitle("Chatter - " + line.substring(13));
textField.setEditable(true);
} else if (line.startsWith("MESSAGE")) {
Color c = new Color(Integer.valueOf(getServerMessageUserColor(line)));
appendToPane(getServerMessageUserName(line), getServerUserMessage(line), c);
}
}
} catch (BadLocationException e) {
throw new RuntimeException(e);
} finally {
frame.setVisible(false);
frame.dispose();
}
}
public static void main(String[] args) throws Exception {
ChatClient2 client = new ChatClient2();
client.frame.setSize(700, 700);
client.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
client.frame.setVisible(true);
client.run();
}
private String getServerUserMessage(String line) {
// Mensagem do Servidor - [0]
// Cor do usuário - [1]
// Nome - [2]
// Mensagem - [3]
String msg[] = line.split(";");
return msg[3];
}
private String getServerMessageUserColor (String line){
// Mensagem do Servidor - [0]
// Cor do usuário - [1]
// Nome - [2]
// Mensagem - [3]
String msg[] = line.split(";");
return msg[1];
}
private String getServerMessageUserName (String line){
// Mensagem do Servidor - [0]
// Cor do usuário - [1]
// Nome - [2]
// Mensagem - [3]
String msg[] = line.split(";");
return msg[2];
}
public void appendToPane(String name, String msg, Color c) throws BadLocationException {
StyleContext sc = StyleContext.getDefaultStyleContext();
AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, c); //cor do nome
aset = sc.addAttribute( aset, StyleConstants.FontFamily, "Lucida Console" );
aset = sc.addAttribute( aset, StyleConstants.Bold, true);
messageArea.getStyledDocument().insertString(messageArea.getDocument().getLength(), name, aset);
StyleContext sc2 = StyleContext.getDefaultStyleContext();
AttributeSet aset2 = sc2.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, Color.BLACK);
aset2 = sc2.addAttribute( aset2, StyleConstants.FontFamily, "Lucida Console" );
aset2 = sc2.addAttribute( aset2, StyleConstants.Italic, true);
messageArea.getStyledDocument().insertString(messageArea.getDocument().getLength(), msg + "\n", aset2);
}
public void centerFrame() {
Dimension windowSize = getSize();
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
Point centerPoint = ge.getCenterPoint();
int dx = (centerPoint.x - (windowSize.width / 2))-350;
int dy = (centerPoint.y - (windowSize.height / 2))-350;
frame.setLocation(dx, dy);
}
}