diff --git a/bin/javaChat/ChatClient$1.class b/bin/backend/ChatClientCMD$1.class similarity index 52% rename from bin/javaChat/ChatClient$1.class rename to bin/backend/ChatClientCMD$1.class index 63f587a..506a9ca 100644 Binary files a/bin/javaChat/ChatClient$1.class and b/bin/backend/ChatClientCMD$1.class differ diff --git a/bin/javaChat/ChatClient$2.class b/bin/backend/ChatClientCMD$2.class similarity index 59% rename from bin/javaChat/ChatClient$2.class rename to bin/backend/ChatClientCMD$2.class index 03c4556..17f87ed 100644 Binary files a/bin/javaChat/ChatClient$2.class and b/bin/backend/ChatClientCMD$2.class differ diff --git a/bin/javaChat/ChatClient.class b/bin/backend/ChatClientCMD.class similarity index 78% rename from bin/javaChat/ChatClient.class rename to bin/backend/ChatClientCMD.class index 54450fb..bf8fb21 100644 Binary files a/bin/javaChat/ChatClient.class and b/bin/backend/ChatClientCMD.class differ diff --git a/bin/javaChat/ChatServer.class b/bin/backend/ChatServer.class similarity index 61% rename from bin/javaChat/ChatServer.class rename to bin/backend/ChatServer.class index 21c4d11..0faed87 100644 Binary files a/bin/javaChat/ChatServer.class and b/bin/backend/ChatServer.class differ diff --git a/bin/backend/ClientHandler.class b/bin/backend/ClientHandler.class new file mode 100644 index 0000000..0586296 Binary files /dev/null and b/bin/backend/ClientHandler.class differ diff --git a/bin/chatApp/ChatGUI$1.class b/bin/chatApp/ChatGUI$1.class new file mode 100644 index 0000000..8a848c8 Binary files /dev/null and b/bin/chatApp/ChatGUI$1.class differ diff --git a/bin/chatApp/ChatGUI$2.class b/bin/chatApp/ChatGUI$2.class new file mode 100644 index 0000000..c5cb36d Binary files /dev/null and b/bin/chatApp/ChatGUI$2.class differ diff --git a/bin/chatApp/ChatGUI$3.class b/bin/chatApp/ChatGUI$3.class new file mode 100644 index 0000000..4d142b8 Binary files /dev/null and b/bin/chatApp/ChatGUI$3.class differ diff --git a/bin/chatApp/ChatGUI.class b/bin/chatApp/ChatGUI.class new file mode 100644 index 0000000..5bd3246 Binary files /dev/null and b/bin/chatApp/ChatGUI.class differ diff --git a/bin/chatApp/ClientGUI$1.class b/bin/chatApp/ClientGUI$1.class new file mode 100644 index 0000000..99fee34 Binary files /dev/null and b/bin/chatApp/ClientGUI$1.class differ diff --git a/bin/chatApp/ClientGUI$2.class b/bin/chatApp/ClientGUI$2.class new file mode 100644 index 0000000..0bf7974 Binary files /dev/null and b/bin/chatApp/ClientGUI$2.class differ diff --git a/bin/chatApp/ClientGUI.class b/bin/chatApp/ClientGUI.class new file mode 100644 index 0000000..36de037 Binary files /dev/null and b/bin/chatApp/ClientGUI.class differ diff --git a/bin/chatApp/LoginGUI$1.class b/bin/chatApp/LoginGUI$1.class new file mode 100644 index 0000000..9e89262 Binary files /dev/null and b/bin/chatApp/LoginGUI$1.class differ diff --git a/bin/chatApp/LoginGUI$2.class b/bin/chatApp/LoginGUI$2.class new file mode 100644 index 0000000..aa1d5ce Binary files /dev/null and b/bin/chatApp/LoginGUI$2.class differ diff --git a/bin/chatApp/LoginGUI$3.class b/bin/chatApp/LoginGUI$3.class new file mode 100644 index 0000000..da0c8b3 Binary files /dev/null and b/bin/chatApp/LoginGUI$3.class differ diff --git a/bin/chatApp/LoginGUI.class b/bin/chatApp/LoginGUI.class new file mode 100644 index 0000000..3640ee3 Binary files /dev/null and b/bin/chatApp/LoginGUI.class differ diff --git a/bin/demonstration/ChatDEMO$1.class b/bin/demonstration/ChatDEMO$1.class new file mode 100644 index 0000000..b75b86a Binary files /dev/null and b/bin/demonstration/ChatDEMO$1.class differ diff --git a/bin/demonstration/ChatDEMO$2.class b/bin/demonstration/ChatDEMO$2.class new file mode 100644 index 0000000..ce41a2a Binary files /dev/null and b/bin/demonstration/ChatDEMO$2.class differ diff --git a/bin/demonstration/ChatDEMO$3.class b/bin/demonstration/ChatDEMO$3.class new file mode 100644 index 0000000..8f3e73a Binary files /dev/null and b/bin/demonstration/ChatDEMO$3.class differ diff --git a/bin/demonstration/ChatDEMO.class b/bin/demonstration/ChatDEMO.class new file mode 100644 index 0000000..97b2106 Binary files /dev/null and b/bin/demonstration/ChatDEMO.class differ diff --git a/bin/demonstration/ClientDEMO$1.class b/bin/demonstration/ClientDEMO$1.class new file mode 100644 index 0000000..bf2057e Binary files /dev/null and b/bin/demonstration/ClientDEMO$1.class differ diff --git a/bin/demonstration/ClientDEMO$2.class b/bin/demonstration/ClientDEMO$2.class new file mode 100644 index 0000000..58b2dd6 Binary files /dev/null and b/bin/demonstration/ClientDEMO$2.class differ diff --git a/bin/demonstration/ClientDEMO.class b/bin/demonstration/ClientDEMO.class new file mode 100644 index 0000000..af4fcef Binary files /dev/null and b/bin/demonstration/ClientDEMO.class differ diff --git a/bin/demonstration/LoginDEMO$1.class b/bin/demonstration/LoginDEMO$1.class new file mode 100644 index 0000000..bb17b72 Binary files /dev/null and b/bin/demonstration/LoginDEMO$1.class differ diff --git a/bin/demonstration/LoginDEMO$2.class b/bin/demonstration/LoginDEMO$2.class new file mode 100644 index 0000000..ba2eee5 Binary files /dev/null and b/bin/demonstration/LoginDEMO$2.class differ diff --git a/bin/demonstration/LoginDEMO.class b/bin/demonstration/LoginDEMO.class new file mode 100644 index 0000000..d73a09a Binary files /dev/null and b/bin/demonstration/LoginDEMO.class differ diff --git a/bin/javaChat/ClientGUI$1.class b/bin/javaChat/ClientGUI$1.class deleted file mode 100644 index 9c0d6d3..0000000 Binary files a/bin/javaChat/ClientGUI$1.class and /dev/null differ diff --git a/bin/javaChat/ClientGUI$2.class b/bin/javaChat/ClientGUI$2.class deleted file mode 100644 index d3440f6..0000000 Binary files a/bin/javaChat/ClientGUI$2.class and /dev/null differ diff --git a/bin/javaChat/ClientGUI.class b/bin/javaChat/ClientGUI.class deleted file mode 100644 index adaa1d5..0000000 Binary files a/bin/javaChat/ClientGUI.class and /dev/null differ diff --git a/bin/javaChat/ClientHandler.class b/bin/javaChat/ClientHandler.class deleted file mode 100644 index b606840..0000000 Binary files a/bin/javaChat/ClientHandler.class and /dev/null differ diff --git a/bin/javaChat/UserInterface$1.class b/bin/javaChat/UserInterface$1.class deleted file mode 100644 index 1eb0b93..0000000 Binary files a/bin/javaChat/UserInterface$1.class and /dev/null differ diff --git a/bin/javaChat/UserInterface$2.class b/bin/javaChat/UserInterface$2.class deleted file mode 100644 index e082f5f..0000000 Binary files a/bin/javaChat/UserInterface$2.class and /dev/null differ diff --git a/bin/javaChat/UserInterface$3.class b/bin/javaChat/UserInterface$3.class deleted file mode 100644 index 9347be1..0000000 Binary files a/bin/javaChat/UserInterface$3.class and /dev/null differ diff --git a/bin/javaChat/UserInterface$4.class b/bin/javaChat/UserInterface$4.class deleted file mode 100644 index af911d4..0000000 Binary files a/bin/javaChat/UserInterface$4.class and /dev/null differ diff --git a/bin/javaChat/UserInterface$5.class b/bin/javaChat/UserInterface$5.class deleted file mode 100644 index bfcb669..0000000 Binary files a/bin/javaChat/UserInterface$5.class and /dev/null differ diff --git a/bin/javaChat/UserInterface$6.class b/bin/javaChat/UserInterface$6.class deleted file mode 100644 index 87e9a3f..0000000 Binary files a/bin/javaChat/UserInterface$6.class and /dev/null differ diff --git a/bin/javaChat/UserInterface.class b/bin/javaChat/UserInterface.class deleted file mode 100644 index 3487c2f..0000000 Binary files a/bin/javaChat/UserInterface.class and /dev/null differ diff --git a/executables/java chat DEMO.jar b/executables/java chat DEMO.jar new file mode 100644 index 0000000..a57df14 Binary files /dev/null and b/executables/java chat DEMO.jar differ diff --git a/executables/java chat server.jar b/executables/java chat server.jar new file mode 100644 index 0000000..5bf6662 Binary files /dev/null and b/executables/java chat server.jar differ diff --git a/executables/java chat.jar b/executables/java chat.jar new file mode 100644 index 0000000..ae24d1a Binary files /dev/null and b/executables/java chat.jar differ diff --git a/src/javaChat/ChatClient.java b/src/backend/ChatClientCMD.java similarity index 98% rename from src/javaChat/ChatClient.java rename to src/backend/ChatClientCMD.java index 9560b49..dbaa391 100644 --- a/src/javaChat/ChatClient.java +++ b/src/backend/ChatClientCMD.java @@ -1,4 +1,4 @@ -package javaChat; +package backend; import java.net.* ; @@ -6,7 +6,7 @@ import javax.swing.JFrame ; import java.io.* ; -public class ChatClient +public class ChatClientCMD { /** diff --git a/src/backend/ChatServer.class b/src/backend/ChatServer.class new file mode 100644 index 0000000..f3ea427 Binary files /dev/null and b/src/backend/ChatServer.class differ diff --git a/src/backend/ChatServer.java b/src/backend/ChatServer.java new file mode 100644 index 0000000..c8ebfef --- /dev/null +++ b/src/backend/ChatServer.java @@ -0,0 +1,309 @@ + +package backend ; + +import java.io.DataInputStream ; +import java.io.DataOutputStream ; +import java.io.IOException ; +import java.net.ServerSocket ; +import java.net.Socket ; +import java.util.ArrayList ; + +/** + * ChatServer class accepts Client requests to join + * and creates an individual thread for each client. + * The "brain" of the server. + * + * @author osheas1 + * @author gohringj + * @version Semi-Final Draft + */ +public class ChatServer + { + + // List of all active users in Server + public static ArrayList userList = new ArrayList<>() ; + + + /** + * @param args (unused) + * @throws IOException (is thrown if connection is unexpectedly closed) + */ + public static void main( final String[] args ) throws IOException + { + // Register service on port 6789 + ServerSocket serverSocket = new ServerSocket( 6789 ) ; + Socket socket = null ; + + System.out.println( "\nServer established. Awaiting connections..." ) ; + + // Loop to accept client requests + while ( true ) + { + socket = serverSocket.accept() ; + + DataOutputStream dataStreamOut = new DataOutputStream( socket.getOutputStream() ) ; + DataInputStream dataStreamIn = new DataInputStream( socket.getInputStream() ) ; + + System.out.println( "\nClient connection detected, requesting username..." ) ; + String username = dataStreamIn.readUTF() ; + + ClientHandler handle = new ClientHandler( socket, username, dataStreamOut, dataStreamIn ) ; + + System.out.println( "\nCreating new thread...\n" ) ; + Thread thread = new Thread( handle ) ; + + userList.add( handle ) ; + + thread.start() ; + } + } + } // end class ChatServer + + + + +/** + * ClientHandler class manages the actual messages being sent + * between the users. The "heart" of the server. + * + * @author osheas1 + * @author gohringj + * @version Semi-Final Draft + */ + +class ClientHandler implements Runnable + { + Socket socket ; + String username ; + DataOutputStream dataStreamOut ; + DataInputStream dataStreamIn ; + boolean loggedIn ; + int commandCheck ; + + /** + * Constructor + * @param s = client socket + * @param user = client's user-name + * @param dos = data stream to client + * @param dis = data stream from client + */ + public ClientHandler( Socket s, String user, DataOutputStream dos, DataInputStream dis ) + { + this.socket = s ; + this.username = user ; + this.dataStreamOut = dos ; + this.dataStreamIn = dis ; + this.loggedIn = true ; + } + + + @Override + public void run() + { + // Note the connection of the new user + String announcement = String.format( "\n>>> User \"%s\" has successfully connected (type /help for a list of commands) <<<\n", this.username ) ; + System.out.println( announcement ) ; + + // Distribute announcement to entire server + sendMessage( announcement ) ; + + // Message management + String message = "" ; + while ( true ) + { + // Try-Catch block interprets incoming messages + try + { + message = this.dataStreamIn.readUTF() ; + + if(message == "" || message == " ") + { + continue ; + } + } + catch ( IOException x ) + { + x.printStackTrace() ; + } + + // Check message for user commands, returns TRUE if user "/quit"s + if( commands( message, this ) ) + { + break ; + } + + // Print the message server-side + message = String.format( "%s: %s", this.username, message ) ; + System.out.println( message ) ; + + // Distribute the message to all users + if ( this.commandCheck == 0 ) + { + sendMessage( message ) ; + } + else + { + this.commandCheck = 0 ; + } + } // end while + + // close streams when user disconnects + try + { + this.loggedIn = false ; + this.dataStreamIn.close() ; + this.dataStreamOut.close() ; + this.socket.close() ; + } + catch ( IOException x ) + { + x.printStackTrace() ; + } + } // end run + + + /** + * The 'commands' method manages the usage of user-commands + * by distributing private messages, providing a user-list, + * etc. + * + * @param message = the command itself + * @param user = the user that + * @return = boolean that indicates that the user has disconnected + */ + public boolean commands( String message, final ClientHandler user ) + { + final String[] commands = new String[] { "/help", "/quit", "/userlist", "/pm" } ; + + try + { + // Quit command terminates client's connection, with an announcement + if ( message.equals( "/quit" ) ) + { + this.commandCheck++ ; + String announcement = String.format( "\n-= \"%s\" has disconnected =- \n", user.username ) ; + System.out.println( announcement ) ; + sendMessage( announcement ) ; + return true ; + } + + // User-List command provides the user a list of everyone else connected to the server + if ( message.equals( "/userlist" ) ) + { + this.commandCheck++ ; + user.dataStreamOut.writeUTF( "> Current list of users in the server: " ) ; + for ( ClientHandler users : ChatServer.userList ) + { + if ( !users.loggedIn ) + { + continue ; + } + + user.dataStreamOut.writeUTF( "> " + users.username ) ; + } + } + + // Help command lists all available commands + if ( message.equals( "/help" ) ) + { + this.commandCheck++ ; + user.dataStreamOut.writeUTF( "\n> All available commands: " ) ; + for ( String command : commands ) + { + user.dataStreamOut.writeUTF( "> " +command ) ; + } + + user.dataStreamOut.writeUTF( "\n" ) ; + } + + // Private Message Command allows users to send messages to a single recipient + if ( message.contains( "/pm" ) ) + { + this.commandCheck++ ; + user.dataStreamOut.writeUTF( "\n> Who would you like to private message?" ) ; + String recipient = this.dataStreamIn.readUTF() ; + user.dataStreamOut.writeUTF( "> \"" + recipient + "\"" ) ; + + ClientHandler sendTo = null ; + + // loop determines which ClientHandler object to send the message to + for ( ClientHandler users : ChatServer.userList ) + { + if ( !users.loggedIn ) + { + continue ; + } + else if ( users.username.equals( recipient ) ) + { + sendTo = users ; + } + } + + if ( sendTo == null ) + { + user.dataStreamOut.writeUTF( "\n> User not found, please try again" ) ; + return false ; + } + + user.dataStreamOut.writeUTF( "\n> What would you like to say?" ) ; + String privateMessage = this.dataStreamIn.readUTF() ; + user.dataStreamOut.writeUTF( "> sent: \"" + privateMessage + "\"" ) ; + sendPrivateMessage( privateMessage, user.username, sendTo ) ; + + } + } + + catch ( IOException x ) + { + while ( ChatServer.userList.size() != 0 ) + { + System.out.println( x ) ; + } + } + + return false ; + } // end commands + + + // Convenience method to send messages privately + private static void sendPrivateMessage( String message, final String sentfrom, final ClientHandler sendto ) + { + try + { + for ( ClientHandler users : ChatServer.userList ) + { + if ( users.equals( sendto ) ) + { + message = String.format( "\nFrom <%s>: %s", sentfrom, message ) ; + users.dataStreamOut.writeUTF( message ) ; + } + } + } + catch ( final IOException x ) + { + x.printStackTrace() ; + } + } + + + // Convenience method to distribute messages to all clients + private static void sendMessage( String message ) + { + try + { + for ( ClientHandler users : ChatServer.userList ) + { + if ( !users.loggedIn ) + { + continue ; + } + users.dataStreamOut.writeUTF( message ) ; + } + } + catch ( IOException x ) + { + x.printStackTrace() ; + } + } + } +// end class ClientHandler \ No newline at end of file diff --git a/src/backend/ClientHandler.class b/src/backend/ClientHandler.class new file mode 100644 index 0000000..d0dbaf0 Binary files /dev/null and b/src/backend/ClientHandler.class differ diff --git a/src/chatApp/ChatGUI$1.class b/src/chatApp/ChatGUI$1.class new file mode 100644 index 0000000..af760ba Binary files /dev/null and b/src/chatApp/ChatGUI$1.class differ diff --git a/src/chatApp/ChatGUI$2.class b/src/chatApp/ChatGUI$2.class new file mode 100644 index 0000000..9c726a5 Binary files /dev/null and b/src/chatApp/ChatGUI$2.class differ diff --git a/src/chatApp/ChatGUI$3.class b/src/chatApp/ChatGUI$3.class new file mode 100644 index 0000000..6c7c945 Binary files /dev/null and b/src/chatApp/ChatGUI$3.class differ diff --git a/src/chatApp/ChatGUI.class b/src/chatApp/ChatGUI.class new file mode 100644 index 0000000..72f3bc7 Binary files /dev/null and b/src/chatApp/ChatGUI.class differ diff --git a/src/chatApp/ChatGUI.java b/src/chatApp/ChatGUI.java new file mode 100644 index 0000000..a0494e6 --- /dev/null +++ b/src/chatApp/ChatGUI.java @@ -0,0 +1,167 @@ + +package chatApp ; + +@SuppressWarnings( "javadoc" ) +public class ChatGUI extends javax.swing.JFrame { + + public boolean messageReady = false ; + public String message ; + public String username ; + /* + public void giveTitle(String name) + { + this.username = name ; + setTitle("logged in as:" + name ) ; + } + */ + public String getMessageOut() + { + if( this.message.contentEquals( "" ) || this.message.contentEquals( " " ) ) + { + this.messageReady = false ; + return null ; + } + if( this.messageReady ) + { + this.messageReady = false ; + return this.message ; + } + return null ; + } + + + public void printMessage( String messageOut ) + { + messageOut = String.format( "%s\n%s", this.jTextArea1.getText(), messageOut ) ; + this.jTextArea1.setText( null ) ; + this.jTextArea1.setText( messageOut ) ; + jTextArea1.moveCaretPosition( jTextArea1.getDocument().getLength() ); + } + + + public ChatGUI() { + initComponents(); + } + + + @SuppressWarnings("unchecked") + // + private void initComponents() { + + jTextField1 = new javax.swing.JTextField(); + jButton1 = new javax.swing.JButton(); + jScrollPane2 = new javax.swing.JScrollPane(); + jTextArea1 = new javax.swing.JTextArea(); + + setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE); + setTitle("logged in as: " + this.username ); + + jButton1.setText("send"); + jButton1.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + jButton1ActionPerformed(evt); + } + }); + + jTextField1.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + jTextField1ActionPerformed(evt); + } + }); + + setResizable(false); + jTextArea1.setEditable(false); + jTextArea1.setColumns(20); + jTextArea1.setRows(5); + jTextArea1.setWrapStyleWord(true); + jTextArea1.setLineWrap( true ); + jTextArea1.moveCaretPosition( jTextArea1.getDocument().getLength() ); + jScrollPane2.setViewportView(jTextArea1); + + javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); + getContentPane().setLayout(layout); + layout.setHorizontalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() + .addContainerGap(50, Short.MAX_VALUE) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) + .addComponent(jScrollPane2) + .addGroup(layout.createSequentialGroup() + .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 610, javax.swing.GroupLayout.PREFERRED_SIZE) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(jButton1))) + .addGap(50, 50, 50)) + ); + layout.setVerticalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addContainerGap(50, Short.MAX_VALUE) + .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 311, javax.swing.GroupLayout.PREFERRED_SIZE) + .addGap(18, 18, 18) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) + .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) + .addComponent(jButton1)) + .addGap(50, 50, 50)) + ); + + pack(); + setLocationRelativeTo(null); + }// + + private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) + { + if( this.jTextField1.getText() != null ) + { + this.message = this.jTextField1.getText() ; + this.messageReady = true ; + this.jTextField1.setText( null ) ; + } + } + + private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { + + if( this.jTextField1.getText() != null ) + { + this.message = this.jTextField1.getText() ; + this.messageReady = true ; + this.jTextField1.setText( null ) ; + } + + } + + + public static void main(String args[]) { + + try { + for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { + if ("Nimbus".equals(info.getName())) { + javax.swing.UIManager.setLookAndFeel(info.getClassName()); + break; + } + } + } catch (ClassNotFoundException ex) { + java.util.logging.Logger.getLogger(ChatGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } catch (InstantiationException ex) { + java.util.logging.Logger.getLogger(ChatGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } catch (IllegalAccessException ex) { + java.util.logging.Logger.getLogger(ChatGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } catch (javax.swing.UnsupportedLookAndFeelException ex) { + java.util.logging.Logger.getLogger(ChatGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } + // + + /* Create and display the form */ + java.awt.EventQueue.invokeLater(new Runnable() { + public void run() { + new ChatGUI().setVisible(true); + } + }); + } + + // Variables declaration - do not modify + private javax.swing.JButton jButton1; + private javax.swing.JScrollPane jScrollPane2; + private javax.swing.JTextArea jTextArea1; + private javax.swing.JTextField jTextField1; + // End of variables declaration +} diff --git a/src/chatApp/ChattingUI$1.class b/src/chatApp/ChattingUI$1.class new file mode 100644 index 0000000..db63e6b Binary files /dev/null and b/src/chatApp/ChattingUI$1.class differ diff --git a/src/chatApp/ChattingUI$2.class b/src/chatApp/ChattingUI$2.class new file mode 100644 index 0000000..e764ea1 Binary files /dev/null and b/src/chatApp/ChattingUI$2.class differ diff --git a/src/chatApp/ChattingUI$3.class b/src/chatApp/ChattingUI$3.class new file mode 100644 index 0000000..a319de8 Binary files /dev/null and b/src/chatApp/ChattingUI$3.class differ diff --git a/src/chatApp/ChattingUI.class b/src/chatApp/ChattingUI.class new file mode 100644 index 0000000..770f2e4 Binary files /dev/null and b/src/chatApp/ChattingUI.class differ diff --git a/src/chatApp/ClientGUI$1.class b/src/chatApp/ClientGUI$1.class new file mode 100644 index 0000000..081ae66 Binary files /dev/null and b/src/chatApp/ClientGUI$1.class differ diff --git a/src/chatApp/ClientGUI$2.class b/src/chatApp/ClientGUI$2.class new file mode 100644 index 0000000..0cf7cc8 Binary files /dev/null and b/src/chatApp/ClientGUI$2.class differ diff --git a/src/chatApp/ClientGUI.class b/src/chatApp/ClientGUI.class new file mode 100644 index 0000000..9419057 Binary files /dev/null and b/src/chatApp/ClientGUI.class differ diff --git a/src/javaChat/ClientGUI.java b/src/chatApp/ClientGUI.java similarity index 57% rename from src/javaChat/ClientGUI.java rename to src/chatApp/ClientGUI.java index 3b98486..cfb7726 100644 --- a/src/javaChat/ClientGUI.java +++ b/src/chatApp/ClientGUI.java @@ -1,22 +1,18 @@ -package javaChat; +package chatApp; import java.net.* ; import java.io.* ; -/** - * - * - * @author osheas1 - * @version 1.0.0 2020-12-07 Initial implementation - * - */ -public class ClientGUI +public class ClientGUI { - + + static LoginGUI login = new LoginGUI() ; + static ChatGUI chatRoom = new ChatGUI() ; + + @SuppressWarnings( "javadoc" ) public static void main( String[] args ) throws IOException { - UserInterface login = new UserInterface() ; login.setVisible( true ); boolean done = false ; @@ -34,25 +30,43 @@ public class ClientGUI // Create communication streams DataInputStream dataStreamIn = new DataInputStream( socket.getInputStream() ) ; - DataOutputStream dataStreamOut = new DataOutputStream( socket.getOutputStream() ) ; - - BufferedReader keyboard = new BufferedReader( new InputStreamReader( System.in ) ) ; + DataOutputStream dataStreamOut = new DataOutputStream( socket.getOutputStream() ) ; dataStreamOut.writeUTF( username ) ; + chatRoom.setTitle( "logged in as: " + username ) ; + chatRoom.setVisible( true ) ; + // Thread for sending messages Thread sendMessage = new Thread(new Runnable() { @Override public void run() - { - System.out.print( "\n-= Type /help for a list of commands =-" ) ; + { while( true ) { try { - String messageOut = keyboard.readLine() ; - dataStreamOut.writeUTF( messageOut ) ; + // if-statement is only triggered if client closes window (disconnects) + if( !chatRoom.isVisible() ) + { + dataStreamOut.writeUTF( "/quit" ) ; + dataStreamIn.close() ; + dataStreamOut.close() ; + socket.close() ; + System.exit( 0 ) ; + } + // extract message information from GUI text field + if( chatRoom.messageReady ) + { + String messageOut = chatRoom.getMessageOut() ; + if( messageOut == null ) + { + continue ; + } + dataStreamOut.writeUTF( messageOut ) ; + } + System.out.print("") ; } catch( IOException x ) { @@ -73,9 +87,9 @@ public class ClientGUI while( true ) { try - { - String messageIn = dataStreamIn.readUTF() ; - System.out.printf( "%n%s\n", messageIn ) ; + { + String messageIn = dataStreamIn.readUTF() ; + chatRoom.printMessage( messageIn ) ; } catch( IOException x ) { @@ -89,9 +103,7 @@ public class ClientGUI // Begin threads sendMessage.start() ; receiveMessage.start() ; - - + } - } // end class ClientGUI \ No newline at end of file diff --git a/src/chatApp/LoginGUI$1.class b/src/chatApp/LoginGUI$1.class new file mode 100644 index 0000000..2a7bd26 Binary files /dev/null and b/src/chatApp/LoginGUI$1.class differ diff --git a/src/chatApp/LoginGUI$2.class b/src/chatApp/LoginGUI$2.class new file mode 100644 index 0000000..d2fe6ce Binary files /dev/null and b/src/chatApp/LoginGUI$2.class differ diff --git a/src/chatApp/LoginGUI$3.class b/src/chatApp/LoginGUI$3.class new file mode 100644 index 0000000..1b71f0a Binary files /dev/null and b/src/chatApp/LoginGUI$3.class differ diff --git a/src/chatApp/LoginGUI$4.class b/src/chatApp/LoginGUI$4.class new file mode 100644 index 0000000..7aaabcb Binary files /dev/null and b/src/chatApp/LoginGUI$4.class differ diff --git a/src/chatApp/LoginGUI$5.class b/src/chatApp/LoginGUI$5.class new file mode 100644 index 0000000..56d6eec Binary files /dev/null and b/src/chatApp/LoginGUI$5.class differ diff --git a/src/chatApp/LoginGUI$6.class b/src/chatApp/LoginGUI$6.class new file mode 100644 index 0000000..1102a99 Binary files /dev/null and b/src/chatApp/LoginGUI$6.class differ diff --git a/src/chatApp/LoginGUI.class b/src/chatApp/LoginGUI.class new file mode 100644 index 0000000..ae0c7ee Binary files /dev/null and b/src/chatApp/LoginGUI.class differ diff --git a/src/chatApp/LoginGUI.java b/src/chatApp/LoginGUI.java new file mode 100644 index 0000000..f6ad5b8 --- /dev/null +++ b/src/chatApp/LoginGUI.java @@ -0,0 +1,182 @@ +package chatApp; + +import java.awt.* ; +import java.util.* ; +import javax.swing.* ; + +public class LoginGUI extends javax.swing.JFrame { + + + String ip ; + String username ; + boolean done ; + + + public String getIP() + { + return ip ; + } + + public String getUsername() + { + return username ; + } + + public boolean isDone() + { + return done ; + } + + +public LoginGUI() { + initComponents(); +} + +@SuppressWarnings("unchecked") +// +private void initComponents() + { + + + jLabel1 = new javax.swing.JLabel(); + jTextField1 = new javax.swing.JTextField(); + jLabel2 = new javax.swing.JLabel(); + jLabel3 = new javax.swing.JLabel(); + ipBox = new javax.swing.JTextField(); + + setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); + setTitle("java chat login"); + setAlwaysOnTop(true); + setResizable(false); + + jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); + jLabel1.setText("Enter Server's IP:"); + + jTextField1.setHorizontalAlignment(javax.swing.JTextField.CENTER); + ipBox.setHorizontalAlignment(javax.swing.JTextField.CENTER); + jTextField1.setToolTipText(""); + jTextField1.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + jTextField1ActionPerformed(evt); + } + }); + + jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); + jLabel2.setText("Enter Username:"); + + jLabel3.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N + jLabel3.setText("inputs must be at least one character"); + jLabel3.setEnabled(false); + + ipBox.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + jTextField2ActionPerformed(evt); + } + }); + + javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); + getContentPane().setLayout(layout); + layout.setHorizontalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addContainerGap(43, Short.MAX_VALUE) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) + .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) + .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 132, Short.MAX_VALUE) + .addComponent(ipBox)) + .addGap(40, 40, 40)) + .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() + .addComponent(jLabel3) + .addContainerGap()))) + ); + layout.setVerticalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addGap(40, 40, 40) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) + .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE) + .addComponent(ipBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) + .addGap(18, 18, 18) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) + .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) + .addComponent(jLabel2)) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(jLabel3) + .addContainerGap(22, Short.MAX_VALUE)) + ); + + pack(); + setLocationRelativeTo(null); + }// + + private void jTextField2ActionPerformed(java.awt.event.ActionEvent evt) + { + if( ipBox.getText().isBlank() ) + { + jLabel3.setEnabled( true ); + } + else + { + jTextField1.grabFocus(); + } + } + + private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) + { + if( jTextField1.getText().isBlank() || ipBox.getText().isBlank() ) + { + jLabel3.setEnabled( true ); + } + else + { + this.ip = ipBox.getText() ; + this.username = jTextField1.getText() ; + this.done = true ; + dispose(); + } + } +/** + * @param args the command line arguments + */ +public static void main(String args[]) { + try { + for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { + if ("Nimbus".equals(info.getName())) { + javax.swing.UIManager.setLookAndFeel(info.getClassName()); + break; + } + } + } catch (ClassNotFoundException ex) { + java.util.logging.Logger.getLogger(LoginGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } catch (InstantiationException ex) { + java.util.logging.Logger.getLogger(LoginGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } catch (IllegalAccessException ex) { + java.util.logging.Logger.getLogger(LoginGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } catch (javax.swing.UnsupportedLookAndFeelException ex) { + java.util.logging.Logger.getLogger(LoginGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } + // + + /* Create and display the form */ + java.awt.EventQueue.invokeLater(new Runnable() { + public void run() { + new LoginGUI().setVisible(true); + } + }); +} + + + +// Variables declaration - do not modify +private javax.swing.JLabel jLabel1; +private javax.swing.JLabel jLabel2; +private javax.swing.JLabel jLabel3; +private javax.swing.JTextField jTextField1; +private javax.swing.JTextField ipBox; +// End of variables declaration +} diff --git a/src/chatApp/loginDEMO$1.class b/src/chatApp/loginDEMO$1.class new file mode 100644 index 0000000..1dd8a39 Binary files /dev/null and b/src/chatApp/loginDEMO$1.class differ diff --git a/src/chatApp/loginDEMO$2.class b/src/chatApp/loginDEMO$2.class new file mode 100644 index 0000000..887c167 Binary files /dev/null and b/src/chatApp/loginDEMO$2.class differ diff --git a/src/chatApp/loginDEMO.class b/src/chatApp/loginDEMO.class new file mode 100644 index 0000000..76e8169 Binary files /dev/null and b/src/chatApp/loginDEMO.class differ diff --git a/src/demonstration/ChatDEMO$1.class b/src/demonstration/ChatDEMO$1.class new file mode 100644 index 0000000..4acd2b3 Binary files /dev/null and b/src/demonstration/ChatDEMO$1.class differ diff --git a/src/demonstration/ChatDEMO$2.class b/src/demonstration/ChatDEMO$2.class new file mode 100644 index 0000000..bd4defe Binary files /dev/null and b/src/demonstration/ChatDEMO$2.class differ diff --git a/src/demonstration/ChatDEMO$3.class b/src/demonstration/ChatDEMO$3.class new file mode 100644 index 0000000..68a31bf Binary files /dev/null and b/src/demonstration/ChatDEMO$3.class differ diff --git a/src/demonstration/ChatDEMO.class b/src/demonstration/ChatDEMO.class new file mode 100644 index 0000000..abe7857 Binary files /dev/null and b/src/demonstration/ChatDEMO.class differ diff --git a/src/demonstration/ChatDEMO.java b/src/demonstration/ChatDEMO.java new file mode 100644 index 0000000..8156e62 --- /dev/null +++ b/src/demonstration/ChatDEMO.java @@ -0,0 +1,166 @@ +package demonstration; + +@SuppressWarnings( "javadoc" ) +public class ChatDEMO extends javax.swing.JFrame { + + public boolean messageReady = false ; + public String message ; + public String username ; + /* + public void giveTitle(String name) + { + this.username = name ; + setTitle("logged in as:" + name ) ; + } + */ + public String getMessageOut() + { + if( this.message.contentEquals( "" ) || this.message.contentEquals( " " ) ) + { + this.messageReady = false ; + return null ; + } + if( this.messageReady ) + { + this.messageReady = false ; + return this.message ; + } + return null ; + } + + + public void printMessage( String messageOut ) + { + messageOut = String.format( "%s\n%s", this.jTextArea1.getText(), messageOut ) ; + this.jTextArea1.setText( null ) ; + this.jTextArea1.setText( messageOut ) ; + jTextArea1.moveCaretPosition( jTextArea1.getDocument().getLength() ); + } + + + public ChatDEMO() { + initComponents(); + } + + + @SuppressWarnings("unchecked") + // + private void initComponents() { + + jTextField1 = new javax.swing.JTextField(); + jButton1 = new javax.swing.JButton(); + jScrollPane2 = new javax.swing.JScrollPane(); + jTextArea1 = new javax.swing.JTextArea(); + + setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE); + setTitle("logged in as: " + this.username ); + + jButton1.setText("send"); + jButton1.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + jButton1ActionPerformed(evt); + } + }); + + jTextField1.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + jTextField1ActionPerformed(evt); + } + }); + + setResizable(false); + jTextArea1.setEditable(false); + jTextArea1.setColumns(20); + jTextArea1.setRows(5); + jTextArea1.setWrapStyleWord(true); + jTextArea1.setLineWrap( true ); + jTextArea1.moveCaretPosition( jTextArea1.getDocument().getLength() ); + jScrollPane2.setViewportView(jTextArea1); + + javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); + getContentPane().setLayout(layout); + layout.setHorizontalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() + .addContainerGap(50, Short.MAX_VALUE) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) + .addComponent(jScrollPane2) + .addGroup(layout.createSequentialGroup() + .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 610, javax.swing.GroupLayout.PREFERRED_SIZE) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(jButton1))) + .addGap(50, 50, 50)) + ); + layout.setVerticalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addContainerGap(50, Short.MAX_VALUE) + .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 311, javax.swing.GroupLayout.PREFERRED_SIZE) + .addGap(18, 18, 18) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) + .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) + .addComponent(jButton1)) + .addGap(50, 50, 50)) + ); + + pack(); + setLocationRelativeTo(null); + }// + + private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) + { + if( this.jTextField1.getText() != null ) + { + this.message = this.jTextField1.getText() ; + this.messageReady = true ; + this.jTextField1.setText( null ) ; + } + } + + private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { + + if( this.jTextField1.getText() != null ) + { + this.message = this.jTextField1.getText() ; + this.messageReady = true ; + this.jTextField1.setText( null ) ; + } + + } + + + public static void main(String args[]) { + + try { + for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { + if ("Nimbus".equals(info.getName())) { + javax.swing.UIManager.setLookAndFeel(info.getClassName()); + break; + } + } + } catch (ClassNotFoundException ex) { + java.util.logging.Logger.getLogger(ChatDEMO.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } catch (InstantiationException ex) { + java.util.logging.Logger.getLogger(ChatDEMO.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } catch (IllegalAccessException ex) { + java.util.logging.Logger.getLogger(ChatDEMO.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } catch (javax.swing.UnsupportedLookAndFeelException ex) { + java.util.logging.Logger.getLogger(ChatDEMO.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } + // + + /* Create and display the form */ + java.awt.EventQueue.invokeLater(new Runnable() { + public void run() { + new ChatDEMO().setVisible(true); + } + }); + } + + // Variables declaration - do not modify + private javax.swing.JButton jButton1; + private javax.swing.JScrollPane jScrollPane2; + private javax.swing.JTextArea jTextArea1; + private javax.swing.JTextField jTextField1; + // End of variables declaration +} diff --git a/src/demonstration/ClientDEMO$1.class b/src/demonstration/ClientDEMO$1.class new file mode 100644 index 0000000..85074bf Binary files /dev/null and b/src/demonstration/ClientDEMO$1.class differ diff --git a/src/demonstration/ClientDEMO$2.class b/src/demonstration/ClientDEMO$2.class new file mode 100644 index 0000000..8973685 Binary files /dev/null and b/src/demonstration/ClientDEMO$2.class differ diff --git a/src/demonstration/ClientDEMO.class b/src/demonstration/ClientDEMO.class new file mode 100644 index 0000000..4b0c46c Binary files /dev/null and b/src/demonstration/ClientDEMO.class differ diff --git a/src/demonstration/ClientDEMO.java b/src/demonstration/ClientDEMO.java new file mode 100644 index 0000000..dbd91b3 --- /dev/null +++ b/src/demonstration/ClientDEMO.java @@ -0,0 +1,110 @@ +package demonstration; + +import java.net.* ; +import java.io.* ; + +@SuppressWarnings( "javadoc" ) +public class ClientDEMO + { + + static LoginDEMO loginDemo = new LoginDEMO() ; + static ChatDEMO chatRoom = new ChatDEMO() ; + + @SuppressWarnings( "javadoc" ) + public static void main( String[] args ) throws IOException + { + + loginDemo.setVisible( true ); + + boolean done = false ; + while( !done ) + { + done = loginDemo.isDone() ; + System.out.print(""); + } + + String ip = loginDemo.getIP() ; + String username = loginDemo.getUsername() ; + + // Open your connection to a server, at port 6789 + Socket socket = new Socket( ip , 6789 ) ; + + // Create communication streams + DataInputStream dataStreamIn = new DataInputStream( socket.getInputStream() ) ; + DataOutputStream dataStreamOut = new DataOutputStream( socket.getOutputStream() ) ; + + dataStreamOut.writeUTF( username ) ; + + chatRoom.setTitle( "logged in as: " + username ) ; + chatRoom.setVisible( true ) ; + + // Thread for sending messages + Thread sendMessage = new Thread(new Runnable() + { + @Override + public void run() + { + while( true ) + { + try + { + // if-statement is only triggered if client closes window (disconnects) + if( !chatRoom.isVisible() ) + { + dataStreamOut.writeUTF( "/quit" ) ; + dataStreamIn.close() ; + dataStreamOut.close() ; + socket.close() ; + System.exit( 0 ) ; + } + // extract message information from GUI text field + if( chatRoom.messageReady ) + { + String messageOut = chatRoom.getMessageOut() ; + if( messageOut == null ) + { + continue ; + } + dataStreamOut.writeUTF( messageOut ) ; + } + System.out.print("") ; + } + catch( IOException x ) + { + x.printStackTrace() ; + System.exit( 0 ) ; + } + } + } + }) ; + + // Thread for receiving messages + Thread receiveMessage = new Thread( new Runnable() + { + + @Override + public void run() + { + while( true ) + { + try + { + String messageIn = dataStreamIn.readUTF() ; + chatRoom.printMessage( messageIn ) ; + } + catch( IOException x ) + { + x.printStackTrace() ; + System.exit( 0 ) ; + } + } + } + }); + + // Begin threads + sendMessage.start() ; + receiveMessage.start() ; + + } + } + // end class ClientGUI \ No newline at end of file diff --git a/src/demonstration/LoginDEMO$1.class b/src/demonstration/LoginDEMO$1.class new file mode 100644 index 0000000..19951a3 Binary files /dev/null and b/src/demonstration/LoginDEMO$1.class differ diff --git a/src/demonstration/LoginDEMO$2.class b/src/demonstration/LoginDEMO$2.class new file mode 100644 index 0000000..339afeb Binary files /dev/null and b/src/demonstration/LoginDEMO$2.class differ diff --git a/src/demonstration/LoginDEMO.class b/src/demonstration/LoginDEMO.class new file mode 100644 index 0000000..0acbd60 Binary files /dev/null and b/src/demonstration/LoginDEMO.class differ diff --git a/src/demonstration/LoginDEMO.java b/src/demonstration/LoginDEMO.java new file mode 100644 index 0000000..d67f852 --- /dev/null +++ b/src/demonstration/LoginDEMO.java @@ -0,0 +1,158 @@ +package demonstration; + +@SuppressWarnings( "javadoc" ) +public class LoginDEMO extends javax.swing.JFrame { + + /** + * Creates new form loginGUI + */ + + public String ip ; + public String username ; + public boolean done ; + + public String getIP() + { + return this.ip ; + } + + public String getUsername() + { + return this.username ; + } + + public boolean isDone() + { + return this.done ; + } + + public LoginDEMO() { + initComponents(); + } + + @SuppressWarnings("unchecked") + // + private void initComponents() { + + jPasswordField1 = new javax.swing.JPasswordField(); + jLabel1 = new javax.swing.JLabel(); + jTextField1 = new javax.swing.JTextField(); + jLabel2 = new javax.swing.JLabel(); + jLabel3 = new javax.swing.JLabel(); + + setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); + setTitle("java chat login DEMO"); + setAlwaysOnTop(true); + setResizable(false); + + jPasswordField1.setEditable(false); + jPasswordField1.setHorizontalAlignment(javax.swing.JTextField.CENTER); + jPasswordField1.setText("12.34.56.78"); + + jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); + jLabel1.setText("Enter Server's IP:"); + + jTextField1.setHorizontalAlignment(javax.swing.JTextField.CENTER); + jTextField1.setToolTipText(""); + jTextField1.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + jTextField1ActionPerformed(evt); + } + }); + + jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); + jLabel2.setText("Enter Username:"); + + jLabel3.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N + jLabel3.setText("username must be at least one character"); + jLabel3.setEnabled(false); + + javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); + getContentPane().setLayout(layout); + layout.setHorizontalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addContainerGap(43, Short.MAX_VALUE) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) + .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) + .addComponent(jTextField1) + .addComponent(jPasswordField1, javax.swing.GroupLayout.DEFAULT_SIZE, 132, Short.MAX_VALUE)) + .addGap(40, 40, 40)) + .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() + .addComponent(jLabel3) + .addContainerGap()))) + ); + layout.setVerticalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addGap(40, 40, 40) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) + .addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) + .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)) + .addGap(18, 18, 18) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) + .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) + .addComponent(jLabel2)) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(jLabel3) + .addContainerGap(22, Short.MAX_VALUE)) + ); + + pack(); + setLocationRelativeTo(null); + }// + + private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) { + this.ip = "74.69.251.12" ; + if( jTextField1.getText().isBlank() ) + { + jLabel3.setEnabled( true ); + } + else + { + this.username = jTextField1.getText() ; + this.done = true ; + dispose(); + } + } + + public static void main(String args[]) { + try { + for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { + if ("Nimbus".equals(info.getName())) { + javax.swing.UIManager.setLookAndFeel(info.getClassName()); + break; + } + } + } catch (ClassNotFoundException ex) { + java.util.logging.Logger.getLogger(LoginDEMO.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } catch (InstantiationException ex) { + java.util.logging.Logger.getLogger(LoginDEMO.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } catch (IllegalAccessException ex) { + java.util.logging.Logger.getLogger(LoginDEMO.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } catch (javax.swing.UnsupportedLookAndFeelException ex) { + java.util.logging.Logger.getLogger(LoginDEMO.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } + // + + /* Create and display the form */ + java.awt.EventQueue.invokeLater(new Runnable() { + public void run() { + new LoginDEMO().setVisible(true); + } + }); + } + + // Variables declaration - do not modify + private javax.swing.JLabel jLabel1; + private javax.swing.JLabel jLabel2; + private javax.swing.JLabel jLabel3; + private javax.swing.JPasswordField jPasswordField1; + private javax.swing.JTextField jTextField1; + // End of variables declaration +} diff --git a/src/javaChat/ChatServer.java b/src/javaChat/ChatServer.java deleted file mode 100644 index 22c5575..0000000 --- a/src/javaChat/ChatServer.java +++ /dev/null @@ -1,237 +0,0 @@ -package javaChat; - -import java.net.* ; -import java.util.* ; -import java.io.* ; - -public class ChatServer - { - - // List of all active users in Server - static ArrayList userList = new ArrayList<>() ; - - /** - * Accepts Client requests to join chat server, and - * creates new thread for each individual client. - * - * @param args - * @throws IOException - */ - public static void main( String[] args ) throws IOException - { - - // Register service on port 6789 - ServerSocket serverSocket = new ServerSocket( 6789 ) ; - Socket socket = null ; - - System.out.println( "\nServer established. Awaiting connections..." ) ; - - // Accept client requests - while( true ) - { - socket = serverSocket.accept() ; - - DataOutputStream dataStreamOut = new DataOutputStream( socket.getOutputStream() ) ; - DataInputStream dataStreamIn = new DataInputStream ( socket.getInputStream() ) ; - - System.out.println( "\nClient connection detected, requesting username..." ) ; - String username = dataStreamIn.readUTF() ; - - ClientHandler handle = new ClientHandler( socket, username, dataStreamOut, dataStreamIn ) ; - - System.out.println( "\nCreating new thread...\n" ) ; - Thread thread = new Thread( handle ) ; - - userList.add( handle ) ; - - thread.start() ; - - } - } - - } - // end class ChatClient - - - - - // This class manages the actual messages being sent around - @SuppressWarnings( "javadoc" ) - class ClientHandler implements Runnable - { - - Socket socket ; - String username ; - final DataOutputStream dataStreamOut ; - final DataInputStream dataStreamIn ; - boolean loggedIn ; - private ArrayList userList ; - - - /** - * Constructor - * @param s : client socket - * @param user : client's user-name - * @param dos : data stream to client - * @param dis : data stream from client - */ - public ClientHandler( Socket s, String user, - DataOutputStream dos, - DataInputStream dis ) - { - this.username = user ; - this.socket = s ; - this.dataStreamOut = dos ; - this.dataStreamIn = dis ; - this.loggedIn = true ; - - } - - @Override - public void run() - { - - // Note the connection of the new user - String announcement = String.format( "-= User \"%s\" has successfully connected =-\n", this.username ) ; - System.out.println( announcement ) ; - - // Distribute announcement to entire server - distributeAll( announcement ) ; - - // Message management - String message = "" ; - while( true ) - { - // Read incoming messages - try - { - message = this.dataStreamIn.readUTF() ; - } - catch ( IOException x ) - { - x.printStackTrace() ; - } - - // Check message for user commands, returns TRUE is user quits - if( commands(message, this) ) - { - break ; - } - - // Print the message server-side - message = String.format( "%s: %s", this.username, message ) ; - System.out.println( message ) ; - - // Distribute the message to all users - sendMessage( message ) ; - // end try - - } - - try - { - this.loggedIn = false ; - this.socket.close() ; - } - catch( IOException x ) - { - x.printStackTrace() ; - } - } - - - // This method manages the usage of user-commands - public boolean commands(String message, ClientHandler user) - { - String[] commands = new String[] {"/help", "/quit", "/userlist"} ; - - try { - - // Quit command terminates connection, with an announcement - if( message.equals( "/quit" )) - { - String announcement = String.format( "\n-= \"%s\" has disconnected =- \n", user.username ) ; - System.out.println( announcement ) ; - distributeAll( announcement ) ; - return true ; - } - - if( message.equals( "/userlist" ) ) - { - user.dataStreamOut.writeUTF( "Current list of users in the server: " ) ; - for( ClientHandler users : ChatServer.userList ) - { - if( !users.loggedIn ) - { - continue ; - } - - user.dataStreamOut.writeUTF( users.username ) ; - - } - } - - if( message.contentEquals( "/help" ) ) - { - user.dataStreamOut.writeUTF( "\nAll available commands: " ); - for(int i = 0; i < commands.length; i++ ) - { - user.dataStreamOut.writeUTF( commands[i] ); - } - - user.dataStreamOut.writeUTF( "\n" ) ; - } - } - - catch( IOException x ) - { - System.out.println( x ) ; - } - - return false ; - } - - - // Convenience method to send messages (but not to the user that's sending the message) - private void sendMessage(String message) - { - try - { - for( ClientHandler users : ChatServer.userList ) - { - if( !users.loggedIn || users.equals( this ) ) - { - continue ; - } - users.dataStreamOut.writeUTF( "> " + message ) ; - } - } - catch ( IOException x ) - { - x.printStackTrace() ; - } - } - - - // Convenience method to distribute messages to all clients (announcements) - private void distributeAll(String message) - { - try - { - for( ClientHandler users : ChatServer.userList ) - { - if( !users.loggedIn ) - { - continue ; - } - users.dataStreamOut.writeUTF( message ) ; - } - } - catch ( IOException x ) - { - x.printStackTrace() ; - } - } - - } - // end class ClientHandler \ No newline at end of file diff --git a/src/javaChat/UserInterface.java b/src/javaChat/UserInterface.java deleted file mode 100644 index 1d8f906..0000000 --- a/src/javaChat/UserInterface.java +++ /dev/null @@ -1,288 +0,0 @@ -package javaChat; - -import java.awt.* ; -import java.util.* ; -import javax.swing.* ; - -public class UserInterface extends javax.swing.JFrame { - - - String ip ; - String username ; - boolean done ; - -/** - * Creates new form loginFrame - */ -public UserInterface() { - initComponents(); -} - -/** - * This method is called from within the constructor to initialize the form. - * WARNING: Do NOT modify this code. The content of this method is always - * regenerated by the Form Editor. - */ -@SuppressWarnings("unchecked") -// -private void initComponents() - { - - jTextField1 = new javax.swing.JTextField(); - jLabel1 = new javax.swing.JLabel(); - jLabel2 = new javax.swing.JLabel(); - jTextField2 = new javax.swing.JTextField(); - jButton1 = new javax.swing.JButton(); - jSlider1 = new javax.swing.JSlider(); - jSlider2 = new javax.swing.JSlider(); - jSlider3 = new javax.swing.JSlider(); - jSlider4 = new javax.swing.JSlider(); - - setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); - setTitle("Java Chat"); - setAlwaysOnTop(true); - setBackground(new java.awt.Color(255, 255, 255)); - setForeground(java.awt.Color.white); - setResizable(false); - - jTextField1.setEditable(false); - jTextField1.setHorizontalAlignment(javax.swing.JTextField.CENTER); - jTextField1.setText("0.0.0.0"); - - jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); - jLabel1.setText("Enter Server's I.P. Address:"); - - jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); - jLabel2.setText("Enter your desired username:"); - - jTextField2.setHorizontalAlignment(javax.swing.JTextField.CENTER); - jTextField2.setActionCommand(""); - - jButton1.setText("Connect"); - jButton1.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR)); - jButton1.setOpaque(false); - jButton1.addMouseListener(new java.awt.event.MouseAdapter() { - public void mouseClicked(java.awt.event.MouseEvent evt) { - jButton1MouseClicked(evt); - } - }); - - jSlider1.setMaximum(999); - jSlider1.setOrientation(javax.swing.JSlider.VERTICAL); - jSlider1.setValue(0); - jSlider1.addChangeListener(new javax.swing.event.ChangeListener() { - public void stateChanged(javax.swing.event.ChangeEvent evt) { - jSlider1StateChanged(evt); - } - }); - - jSlider2.setMaximum(999); - jSlider2.setOrientation(javax.swing.JSlider.VERTICAL); - jSlider2.setValue(0); - jSlider2.addChangeListener(new javax.swing.event.ChangeListener() { - public void stateChanged(javax.swing.event.ChangeEvent evt) { - jSlider2StateChanged(evt); - } - }); - - jSlider3.setMaximum(999); - jSlider3.setOrientation(javax.swing.JSlider.VERTICAL); - jSlider3.setValue(0); - jSlider3.addChangeListener(new javax.swing.event.ChangeListener() { - public void stateChanged(javax.swing.event.ChangeEvent evt) { - jSlider3StateChanged(evt); - } - }); - - jSlider4.setMaximum(999); - jSlider4.setOrientation(javax.swing.JSlider.VERTICAL); - jSlider4.setValue(0); - jSlider4.addChangeListener(new javax.swing.event.ChangeListener() { - public void stateChanged(javax.swing.event.ChangeEvent evt) { - jSlider4StateChanged(evt); - } - }); - - javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); - getContentPane().setLayout(layout); - layout.setHorizontalGroup( - layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addGroup(layout.createSequentialGroup() - .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addGroup(layout.createSequentialGroup() - .addGap(60, 60, 60) - .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) - .addGroup(layout.createSequentialGroup() - .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) - .addGap(18, 18, 18) - .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE)) - .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() - .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE) - .addGap(18, 18, 18) - .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE)))) - .addGroup(layout.createSequentialGroup() - .addGap(162, 162, 162) - .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE))) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 60, Short.MAX_VALUE) - .addComponent(jSlider1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) - .addComponent(jSlider2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) - .addComponent(jSlider3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) - .addComponent(jSlider4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) - .addGap(56, 56, 56)) - ); - layout.setVerticalGroup( - layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addGroup(layout.createSequentialGroup() - .addGap(24, 24, 24) - .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) - .addGroup(layout.createSequentialGroup() - .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) - .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) - .addComponent(jSlider3, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE) - .addComponent(jSlider2, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE) - .addComponent(jSlider1, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE)) - .addComponent(jSlider4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE)) - .addContainerGap(40, Short.MAX_VALUE)) - .addGroup(layout.createSequentialGroup() - .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) - .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) - .addComponent(jLabel1)) - .addGap(36, 36, 36) - .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) - .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) - .addComponent(jLabel2)) - .addGap(28, 28, 28) - .addComponent(jButton1) - .addGap(30, 30, 30)))) - ); - - pack(); - }// - -private void jButton1MouseClicked(java.awt.event.MouseEvent evt) - { - ip = jTextField1.getText() ; - username = jTextField2.getText() ; - //System.out.printf( "IP: %s \nUsername: %s\n", ip, username ) ; - - - done = true ; - setVisible( false ) ; - dispose() ; - - } - -private void jSlider1StateChanged(javax.swing.event.ChangeEvent evt) - { - Integer a = jSlider1.getValue() ; - Integer b = jSlider2.getValue() ; - Integer c = jSlider3.getValue() ; - Integer d = jSlider4.getValue() ; - - String ip = String.format("%s.%s.%s.%s", a, b, c, d) ; - - jTextField1.setText( ip.toString() ) ; - } - -private void jSlider2StateChanged(javax.swing.event.ChangeEvent evt) - { - Integer a = jSlider1.getValue() ; - Integer b = jSlider2.getValue() ; - Integer c = jSlider3.getValue() ; - Integer d = jSlider4.getValue() ; - - String ip = String.format("%s.%s.%s.%s", a, b, c, d) ; - - jTextField1.setText( ip.toString() ) ; - } - -private void jSlider3StateChanged(javax.swing.event.ChangeEvent evt) - { - Integer a = jSlider1.getValue() ; - Integer b = jSlider2.getValue() ; - Integer c = jSlider3.getValue() ; - Integer d = jSlider4.getValue() ; - - String ip = String.format("%s.%s.%s.%s", a, b, c, d) ; - - jTextField1.setText( ip.toString() ) ; - } - -private void jSlider4StateChanged(javax.swing.event.ChangeEvent evt) - { - Integer a = jSlider1.getValue() ; - Integer b = jSlider2.getValue() ; - Integer c = jSlider3.getValue() ; - Integer d = jSlider4.getValue() ; - - String ip = String.format("%s.%s.%s.%s", a, b, c, d) ; - - jTextField1.setText( ip.toString() ) ; - } - -/** - * @param args the command line arguments - */ -public static void main(String args[]) { - /* Set the Nimbus look and feel */ - // - /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. - * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html - */ - try { - for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { - if ("Nimbus".equals(info.getName())) { - javax.swing.UIManager.setLookAndFeel(info.getClassName()); - break; - } - } - } catch (ClassNotFoundException ex) { - java.util.logging.Logger.getLogger(UserInterface.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); - } catch (InstantiationException ex) { - java.util.logging.Logger.getLogger(UserInterface.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); - } catch (IllegalAccessException ex) { - java.util.logging.Logger.getLogger(UserInterface.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); - } catch (javax.swing.UnsupportedLookAndFeelException ex) { - java.util.logging.Logger.getLogger(UserInterface.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); - } - // - - /* Create and display the form */ - java.awt.EventQueue.invokeLater(new Runnable() { - public void run() { - new UserInterface().setVisible(true); - } - }); -} - - -public String getIP() - { - return ip ; - } - -public String getUsername() - { - return username ; - } - -public boolean isDone() - { - return done ; - } - -// Variables declaration - do not modify -private javax.swing.JButton jButton1; -private javax.swing.JLabel jLabel1; -private javax.swing.JLabel jLabel2; -private javax.swing.JSlider jSlider1; -private javax.swing.JSlider jSlider2; -private javax.swing.JSlider jSlider3; -private javax.swing.JSlider jSlider4; -private javax.swing.JTextField jTextField1; -private javax.swing.JTextField jTextField2; -// End of variables declaration -}