├── .gitignore ├── Client.java ├── ClientGui.java ├── LICENSE ├── README.md ├── Server.java └── screen.png /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled class file 2 | *.class 3 | 4 | # Log file 5 | *.log 6 | 7 | # BlueJ files 8 | *.ctxt 9 | 10 | # Mobile Tools for Java (J2ME) 11 | .mtj.tmp/ 12 | 13 | # Package Files # 14 | *.jar 15 | *.war 16 | *.ear 17 | *.zip 18 | *.tar.gz 19 | *.rar 20 | 21 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 22 | hs_err_pid* 23 | -------------------------------------------------------------------------------- /Client.java: -------------------------------------------------------------------------------- 1 | import java.io.IOException; 2 | import java.util.ArrayList; 3 | import java.util.Arrays; 4 | import java.io.InputStream; 5 | import java.io.PrintStream; 6 | import java.net.Socket; 7 | import java.net.UnknownHostException; 8 | import java.util.Scanner; 9 | import java.io.StringReader; 10 | 11 | public class Client { 12 | 13 | private String host; 14 | private int port; 15 | 16 | public static void main(String[] args) throws UnknownHostException, IOException { 17 | new Client("127.0.0.1", 12345).run(); 18 | } 19 | 20 | public Client(String host, int port) { 21 | this.host = host; 22 | this.port = port; 23 | } 24 | 25 | public void run() throws UnknownHostException, IOException { 26 | // connect client to server 27 | Socket client = new Socket(host, port); 28 | System.out.println("Client successfully connected to server!"); 29 | 30 | // Get Socket output stream (where the client send her mesg) 31 | PrintStream output = new PrintStream(client.getOutputStream()); 32 | 33 | // ask for a nickname 34 | Scanner sc = new Scanner(System.in); 35 | System.out.print("Enter a nickname: "); 36 | String nickname = sc.nextLine(); 37 | 38 | // send nickname to server 39 | output.println(nickname); 40 | 41 | // create a new thread for server messages handling 42 | new Thread(new ReceivedMessagesHandler(client.getInputStream())).start(); 43 | 44 | // read messages from keyboard and send to server 45 | System.out.println("Messages: \n"); 46 | 47 | // while new messages 48 | while (sc.hasNextLine()) { 49 | output.println(sc.nextLine()); 50 | } 51 | 52 | // end ctrl D 53 | output.close(); 54 | sc.close(); 55 | client.close(); 56 | } 57 | } 58 | 59 | class ReceivedMessagesHandler implements Runnable { 60 | 61 | private InputStream server; 62 | 63 | public ReceivedMessagesHandler(InputStream server) { 64 | this.server = server; 65 | } 66 | 67 | public void run() { 68 | // receive server messages and print out to screen 69 | Scanner s = new Scanner(server); 70 | String tmp = ""; 71 | while (s.hasNextLine()) { 72 | tmp = s.nextLine(); 73 | if (tmp.charAt(0) == '[') { 74 | tmp = tmp.substring(1, tmp.length()-1); 75 | System.out.println( 76 | "\nUSERS LIST: " + 77 | new ArrayList(Arrays.asList(tmp.split(","))) + "\n" 78 | ); 79 | }else{ 80 | try { 81 | System.out.println("\n" + getTagValue(tmp)); 82 | // System.out.println(tmp); 83 | } catch(Exception ignore){} 84 | } 85 | } 86 | s.close(); 87 | } 88 | 89 | // I could use a javax.xml.parsers but the goal of Client.java is to keep everything tight and simple 90 | public static String getTagValue(String xml){ 91 | return xml.split(">")[2].split("<")[0] + xml.split("")[1].split("")[0]; 92 | } 93 | 94 | } 95 | -------------------------------------------------------------------------------- /ClientGui.java: -------------------------------------------------------------------------------- 1 | import java.awt.*; 2 | import java.awt.event.ActionEvent; 3 | import java.awt.event.ActionListener; 4 | import java.awt.event.KeyAdapter; 5 | import java.awt.event.KeyEvent; 6 | import java.net.*; 7 | import java.io.*; 8 | import javax.swing.*; 9 | import javax.swing.text.*; 10 | import javax.swing.event.DocumentEvent; 11 | import javax.swing.event.DocumentListener; 12 | import javax.swing.text.html.*; 13 | 14 | import java.util.ArrayList; 15 | import java.util.Arrays; 16 | 17 | 18 | public class ClientGui extends Thread{ 19 | 20 | final JTextPane jtextFilDiscu = new JTextPane(); 21 | final JTextPane jtextListUsers = new JTextPane(); 22 | final JTextField jtextInputChat = new JTextField(); 23 | private String oldMsg = ""; 24 | private Thread read; 25 | private String serverName; 26 | private int PORT; 27 | private String name; 28 | BufferedReader input; 29 | PrintWriter output; 30 | Socket server; 31 | 32 | public ClientGui() { 33 | this.serverName = "localhost"; 34 | this.PORT = 12345; 35 | this.name = "nickname"; 36 | 37 | String fontfamily = "Arial, sans-serif"; 38 | Font font = new Font(fontfamily, Font.PLAIN, 15); 39 | 40 | final JFrame jfr = new JFrame("Chat"); 41 | jfr.getContentPane().setLayout(null); 42 | jfr.setSize(700, 500); 43 | jfr.setResizable(false); 44 | jfr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 45 | 46 | // Module du fil de discussion 47 | jtextFilDiscu.setBounds(25, 25, 490, 320); 48 | jtextFilDiscu.setFont(font); 49 | jtextFilDiscu.setMargin(new Insets(6, 6, 6, 6)); 50 | jtextFilDiscu.setEditable(false); 51 | JScrollPane jtextFilDiscuSP = new JScrollPane(jtextFilDiscu); 52 | jtextFilDiscuSP.setBounds(25, 25, 490, 320); 53 | 54 | jtextFilDiscu.setContentType("text/html"); 55 | jtextFilDiscu.putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, true); 56 | 57 | // Module de la liste des utilisateurs 58 | jtextListUsers.setBounds(520, 25, 156, 320); 59 | jtextListUsers.setEditable(true); 60 | jtextListUsers.setFont(font); 61 | jtextListUsers.setMargin(new Insets(6, 6, 6, 6)); 62 | jtextListUsers.setEditable(false); 63 | JScrollPane jsplistuser = new JScrollPane(jtextListUsers); 64 | jsplistuser.setBounds(520, 25, 156, 320); 65 | 66 | jtextListUsers.setContentType("text/html"); 67 | jtextListUsers.putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, true); 68 | 69 | // Field message user input 70 | jtextInputChat.setBounds(0, 350, 400, 50); 71 | jtextInputChat.setFont(font); 72 | jtextInputChat.setMargin(new Insets(6, 6, 6, 6)); 73 | final JScrollPane jtextInputChatSP = new JScrollPane(jtextInputChat); 74 | jtextInputChatSP.setBounds(25, 350, 650, 50); 75 | 76 | // button send 77 | final JButton jsbtn = new JButton("Send"); 78 | jsbtn.setFont(font); 79 | jsbtn.setBounds(575, 410, 100, 35); 80 | 81 | // button Disconnect 82 | final JButton jsbtndeco = new JButton("Disconnect"); 83 | jsbtndeco.setFont(font); 84 | jsbtndeco.setBounds(25, 410, 130, 35); 85 | 86 | jtextInputChat.addKeyListener(new KeyAdapter() { 87 | // send message on Enter 88 | public void keyPressed(KeyEvent e) { 89 | if (e.getKeyCode() == KeyEvent.VK_ENTER) { 90 | sendMessage(); 91 | } 92 | 93 | // Get last message typed 94 | if (e.getKeyCode() == KeyEvent.VK_UP) { 95 | String currentMessage = jtextInputChat.getText().trim(); 96 | jtextInputChat.setText(oldMsg); 97 | oldMsg = currentMessage; 98 | } 99 | 100 | if (e.getKeyCode() == KeyEvent.VK_DOWN) { 101 | String currentMessage = jtextInputChat.getText().trim(); 102 | jtextInputChat.setText(oldMsg); 103 | oldMsg = currentMessage; 104 | } 105 | } 106 | }); 107 | 108 | // Click on send button 109 | jsbtn.addActionListener(new ActionListener() { 110 | public void actionPerformed(ActionEvent ae) { 111 | sendMessage(); 112 | } 113 | }); 114 | 115 | // Connection view 116 | final JTextField jtfName = new JTextField(this.name); 117 | final JTextField jtfport = new JTextField(Integer.toString(this.PORT)); 118 | final JTextField jtfAddr = new JTextField(this.serverName); 119 | final JButton jcbtn = new JButton("Connect"); 120 | 121 | // check if those field are not empty 122 | jtfName.getDocument().addDocumentListener(new TextListener(jtfName, jtfport, jtfAddr, jcbtn)); 123 | jtfport.getDocument().addDocumentListener(new TextListener(jtfName, jtfport, jtfAddr, jcbtn)); 124 | jtfAddr.getDocument().addDocumentListener(new TextListener(jtfName, jtfport, jtfAddr, jcbtn)); 125 | 126 | // position des Modules 127 | jcbtn.setFont(font); 128 | jtfAddr.setBounds(25, 380, 135, 40); 129 | jtfName.setBounds(375, 380, 135, 40); 130 | jtfport.setBounds(200, 380, 135, 40); 131 | jcbtn.setBounds(575, 380, 100, 40); 132 | 133 | // couleur par defaut des Modules fil de discussion et liste des utilisateurs 134 | jtextFilDiscu.setBackground(Color.LIGHT_GRAY); 135 | jtextListUsers.setBackground(Color.LIGHT_GRAY); 136 | 137 | // ajout des éléments 138 | jfr.add(jcbtn); 139 | jfr.add(jtextFilDiscuSP); 140 | jfr.add(jsplistuser); 141 | jfr.add(jtfName); 142 | jfr.add(jtfport); 143 | jfr.add(jtfAddr); 144 | jfr.setVisible(true); 145 | 146 | 147 | // info sur le Chat 148 | appendToPane(jtextFilDiscu, "

Les commandes possibles dans le chat sont:

" 149 | +"
"); 155 | 156 | // On connect 157 | jcbtn.addActionListener(new ActionListener() { 158 | public void actionPerformed(ActionEvent ae) { 159 | try { 160 | name = jtfName.getText(); 161 | String port = jtfport.getText(); 162 | serverName = jtfAddr.getText(); 163 | PORT = Integer.parseInt(port); 164 | 165 | appendToPane(jtextFilDiscu, "Connecting to " + serverName + " on port " + PORT + "..."); 166 | server = new Socket(serverName, PORT); 167 | 168 | appendToPane(jtextFilDiscu, "Connected to " + 169 | server.getRemoteSocketAddress()+""); 170 | 171 | input = new BufferedReader(new InputStreamReader(server.getInputStream())); 172 | output = new PrintWriter(server.getOutputStream(), true); 173 | 174 | // send nickname to server 175 | output.println(name); 176 | 177 | // create new Read Thread 178 | read = new Read(); 179 | read.start(); 180 | jfr.remove(jtfName); 181 | jfr.remove(jtfport); 182 | jfr.remove(jtfAddr); 183 | jfr.remove(jcbtn); 184 | jfr.add(jsbtn); 185 | jfr.add(jtextInputChatSP); 186 | jfr.add(jsbtndeco); 187 | jfr.revalidate(); 188 | jfr.repaint(); 189 | jtextFilDiscu.setBackground(Color.WHITE); 190 | jtextListUsers.setBackground(Color.WHITE); 191 | } catch (Exception ex) { 192 | appendToPane(jtextFilDiscu, "Could not connect to Server"); 193 | JOptionPane.showMessageDialog(jfr, ex.getMessage()); 194 | } 195 | } 196 | 197 | }); 198 | 199 | // on deco 200 | jsbtndeco.addActionListener(new ActionListener() { 201 | public void actionPerformed(ActionEvent ae) { 202 | jfr.add(jtfName); 203 | jfr.add(jtfport); 204 | jfr.add(jtfAddr); 205 | jfr.add(jcbtn); 206 | jfr.remove(jsbtn); 207 | jfr.remove(jtextInputChatSP); 208 | jfr.remove(jsbtndeco); 209 | jfr.revalidate(); 210 | jfr.repaint(); 211 | read.interrupt(); 212 | jtextListUsers.setText(null); 213 | jtextFilDiscu.setBackground(Color.LIGHT_GRAY); 214 | jtextListUsers.setBackground(Color.LIGHT_GRAY); 215 | appendToPane(jtextFilDiscu, "Connection closed."); 216 | output.close(); 217 | } 218 | }); 219 | 220 | } 221 | 222 | // check if if all field are not empty 223 | public class TextListener implements DocumentListener{ 224 | JTextField jtf1; 225 | JTextField jtf2; 226 | JTextField jtf3; 227 | JButton jcbtn; 228 | 229 | public TextListener(JTextField jtf1, JTextField jtf2, JTextField jtf3, JButton jcbtn){ 230 | this.jtf1 = jtf1; 231 | this.jtf2 = jtf2; 232 | this.jtf3 = jtf3; 233 | this.jcbtn = jcbtn; 234 | } 235 | 236 | public void changedUpdate(DocumentEvent e) {} 237 | 238 | public void removeUpdate(DocumentEvent e) { 239 | if(jtf1.getText().trim().equals("") || 240 | jtf2.getText().trim().equals("") || 241 | jtf3.getText().trim().equals("") 242 | ){ 243 | jcbtn.setEnabled(false); 244 | }else{ 245 | jcbtn.setEnabled(true); 246 | } 247 | } 248 | public void insertUpdate(DocumentEvent e) { 249 | if(jtf1.getText().trim().equals("") || 250 | jtf2.getText().trim().equals("") || 251 | jtf3.getText().trim().equals("") 252 | ){ 253 | jcbtn.setEnabled(false); 254 | }else{ 255 | jcbtn.setEnabled(true); 256 | } 257 | } 258 | 259 | } 260 | 261 | // envoi des messages 262 | public void sendMessage() { 263 | try { 264 | String message = jtextInputChat.getText().trim(); 265 | if (message.equals("")) { 266 | return; 267 | } 268 | this.oldMsg = message; 269 | output.println(message); 270 | jtextInputChat.requestFocus(); 271 | jtextInputChat.setText(null); 272 | } catch (Exception ex) { 273 | JOptionPane.showMessageDialog(null, ex.getMessage()); 274 | System.exit(0); 275 | } 276 | } 277 | 278 | public static void main(String[] args) throws Exception { 279 | ClientGui client = new ClientGui(); 280 | } 281 | 282 | // read new incoming messages 283 | class Read extends Thread { 284 | public void run() { 285 | String message; 286 | while(!Thread.currentThread().isInterrupted()){ 287 | try { 288 | message = input.readLine(); 289 | if(message != null){ 290 | if (message.charAt(0) == '[') { 291 | message = message.substring(1, message.length()-1); 292 | ArrayList ListUser = new ArrayList( 293 | Arrays.asList(message.split(", ")) 294 | ); 295 | jtextListUsers.setText(null); 296 | for (String user : ListUser) { 297 | appendToPane(jtextListUsers, "@" + user); 298 | } 299 | }else{ 300 | appendToPane(jtextFilDiscu, message); 301 | } 302 | } 303 | } 304 | catch (IOException ex) { 305 | System.err.println("Failed to parse incoming message"); 306 | } 307 | } 308 | } 309 | } 310 | 311 | // send html to pane 312 | private void appendToPane(JTextPane tp, String msg){ 313 | HTMLDocument doc = (HTMLDocument)tp.getDocument(); 314 | HTMLEditorKit editorKit = (HTMLEditorKit)tp.getEditorKit(); 315 | try { 316 | editorKit.insertHTML(doc, doc.getLength(), msg, 0, 0, null); 317 | tp.setCaretPosition(doc.getLength()); 318 | } catch(Exception e){ 319 | e.printStackTrace(); 320 | } 321 | } 322 | } 323 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Pierre Champion 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # java-chat 2 | A simple java chat application 3 | 4 | > Java Chat is a simple chat program 5 | > which allows for a server with multiple 6 | > client programs to join. 7 |
    8 |
  • @nickname pour envoyer un Message privé à l'utilisateur 'nickname'
  • 9 |
  • #d3961b pour changer la couleur de son pseudo au code hexadécimal indiquer
  • 10 |
  • ;) quelques smileys sont implémentés
  • 11 |
  • flèche du haut pour reprendre le dernier message tapé
  • 12 |

13 | 14 | 15 | 16 |

17 | 18 | ScreenShot~ prompt 19 | 20 |

21 | -------------------------------------------------------------------------------- /Server.java: -------------------------------------------------------------------------------- 1 | import java.io.IOException; 2 | import java.io.ObjectOutputStream; 3 | import java.io.InputStream; 4 | import java.io.PrintStream; 5 | import java.net.ServerSocket; 6 | import java.net.Socket; 7 | import java.util.ArrayList; 8 | import java.util.List; 9 | import java.util.Scanner; 10 | import java.util.regex.Pattern; 11 | import java.util.regex.Matcher; 12 | import java.awt.Color; 13 | 14 | public class Server { 15 | 16 | private int port; 17 | private List clients; 18 | private ServerSocket server; 19 | 20 | public static void main(String[] args) throws IOException { 21 | new Server(12345).run(); 22 | } 23 | 24 | public Server(int port) { 25 | this.port = port; 26 | this.clients = new ArrayList(); 27 | } 28 | 29 | public void run() throws IOException { 30 | server = new ServerSocket(port) { 31 | protected void finalize() throws IOException { 32 | this.close(); 33 | } 34 | }; 35 | System.out.println("Port 12345 is now open."); 36 | 37 | while (true) { 38 | // accepts a new client 39 | Socket client = server.accept(); 40 | 41 | // get nickname of newUser 42 | String nickname = (new Scanner ( client.getInputStream() )).nextLine(); 43 | nickname = nickname.replace(",", ""); // ',' use for serialisation 44 | nickname = nickname.replace(" ", "_"); 45 | System.out.println("New Client: \"" + nickname + "\"\n\t Host:" + client.getInetAddress().getHostAddress()); 46 | 47 | // create new User 48 | User newUser = new User(client, nickname); 49 | 50 | // add newUser message to list 51 | this.clients.add(newUser); 52 | 53 | // Welcome msg 54 | newUser.getOutStream().println( 55 | "" 56 | + "Welcome " + newUser.toString() + 57 | "" 58 | ); 59 | 60 | // create a new thread for newUser incoming messages handling 61 | new Thread(new UserHandler(this, newUser)).start(); 62 | } 63 | } 64 | 65 | // delete a user from the list 66 | public void removeUser(User user){ 67 | this.clients.remove(user); 68 | } 69 | 70 | // send incoming msg to all Users 71 | public void broadcastMessages(String msg, User userSender) { 72 | for (User client : this.clients) { 73 | client.getOutStream().println( 74 | userSender.toString() + ": " + msg+""); 75 | } 76 | } 77 | 78 | // send list of clients to all Users 79 | public void broadcastAllUsers(){ 80 | for (User client : this.clients) { 81 | client.getOutStream().println(this.clients); 82 | } 83 | } 84 | 85 | // send message to a User (String) 86 | public void sendMessageToUser(String msg, User userSender, String user){ 87 | boolean find = false; 88 | for (User client : this.clients) { 89 | if (client.getNickname().equals(user) && client != userSender) { 90 | find = true; 91 | userSender.getOutStream().println(userSender.toString() + " -> " + client.toString() +": " + msg); 92 | client.getOutStream().println( 93 | "(Private)" + userSender.toString() + ": " + msg+""); 94 | } 95 | } 96 | if (!find) { 97 | userSender.getOutStream().println(userSender.toString() + " -> (no one!): " + msg); 98 | } 99 | } 100 | } 101 | 102 | class UserHandler implements Runnable { 103 | 104 | private Server server; 105 | private User user; 106 | 107 | public UserHandler(Server server, User user) { 108 | this.server = server; 109 | this.user = user; 110 | this.server.broadcastAllUsers(); 111 | } 112 | 113 | public void run() { 114 | String message; 115 | 116 | // when there is a new message, broadcast to all 117 | Scanner sc = new Scanner(this.user.getInputStream()); 118 | while (sc.hasNextLine()) { 119 | message = sc.nextLine(); 120 | 121 | // smiley 122 | message = message.replace(":)", ""); 123 | message = message.replace(":D", ""); 124 | message = message.replace(":d", ""); 125 | message = message.replace(":(", ""); 126 | message = message.replace("-_-", ""); 127 | message = message.replace(";)", ""); 128 | message = message.replace(":P", ""); 129 | message = message.replace(":p", ""); 130 | message = message.replace(":o", ""); 131 | message = message.replace(":O", ""); 132 | 133 | // Gestion des messages private 134 | if (message.charAt(0) == '@'){ 135 | if(message.contains(" ")){ 136 | System.out.println("private msg : " + message); 137 | int firstSpace = message.indexOf(" "); 138 | String userPrivate= message.substring(1, firstSpace); 139 | server.sendMessageToUser( 140 | message.substring( 141 | firstSpace+1, message.length() 142 | ), user, userPrivate 143 | ); 144 | } 145 | 146 | // Gestion du changement 147 | }else if (message.charAt(0) == '#'){ 148 | user.changeColor(message); 149 | // update color for all other users 150 | this.server.broadcastAllUsers(); 151 | }else{ 152 | // update user list 153 | server.broadcastMessages(message, user); 154 | } 155 | } 156 | // end of Thread 157 | server.removeUser(user); 158 | this.server.broadcastAllUsers(); 159 | sc.close(); 160 | } 161 | } 162 | 163 | class User { 164 | private static int nbUser = 0; 165 | private int userId; 166 | private PrintStream streamOut; 167 | private InputStream streamIn; 168 | private String nickname; 169 | private Socket client; 170 | private String color; 171 | 172 | // constructor 173 | public User(Socket client, String name) throws IOException { 174 | this.streamOut = new PrintStream(client.getOutputStream()); 175 | this.streamIn = client.getInputStream(); 176 | this.client = client; 177 | this.nickname = name; 178 | this.userId = nbUser; 179 | this.color = ColorInt.getColor(this.userId); 180 | nbUser += 1; 181 | } 182 | 183 | // change color user 184 | public void changeColor(String hexColor){ 185 | // check if it's a valid hexColor 186 | Pattern colorPattern = Pattern.compile("#([0-9a-f]{3}|[0-9a-f]{6}|[0-9a-f]{8})"); 187 | Matcher m = colorPattern.matcher(hexColor); 188 | if (m.matches()){ 189 | Color c = Color.decode(hexColor); 190 | // if the Color is too Bright don't change 191 | double luma = 0.2126 * c.getRed() + 0.7152 * c.getGreen() + 0.0722 * c.getBlue(); // per ITU-R BT.709 192 | if (luma > 160) { 193 | this.getOutStream().println("Color Too Bright"); 194 | return; 195 | } 196 | this.color = hexColor; 197 | this.getOutStream().println("Color changed successfully " + this.toString()); 198 | return; 199 | } 200 | this.getOutStream().println("Failed to change color"); 201 | } 202 | 203 | // getteur 204 | public PrintStream getOutStream(){ 205 | return this.streamOut; 206 | } 207 | 208 | public InputStream getInputStream(){ 209 | return this.streamIn; 210 | } 211 | 212 | public String getNickname(){ 213 | return this.nickname; 214 | } 215 | 216 | // print user with his color 217 | public String toString(){ 218 | 219 | return "" + this.getNickname() + ""; 221 | 222 | } 223 | } 224 | 225 | class ColorInt { 226 | public static String[] mColors = { 227 | "#3079ab", // dark blue 228 | "#e15258", // red 229 | "#f9845b", // orange 230 | "#7d669e", // purple 231 | "#53bbb4", // aqua 232 | "#51b46d", // green 233 | "#e0ab18", // mustard 234 | "#f092b0", // pink 235 | "#e8d174", // yellow 236 | "#e39e54", // orange 237 | "#d64d4d", // red 238 | "#4d7358", // green 239 | }; 240 | 241 | public static String getColor(int i) { 242 | return mColors[i % mColors.length]; 243 | } 244 | } 245 | -------------------------------------------------------------------------------- /screen.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pchampio/java-chat/8fd7d42a53e507933f7138ed7bad7deba9631094/screen.png --------------------------------------------------------------------------------