diff options
Diffstat (limited to 'src/chatApp')
24 files changed, 458 insertions, 0 deletions
diff --git a/src/chatApp/ChatGUI$1.class b/src/chatApp/ChatGUI$1.class Binary files differnew file mode 100644 index 0000000..af760ba --- /dev/null +++ b/src/chatApp/ChatGUI$1.class diff --git a/src/chatApp/ChatGUI$2.class b/src/chatApp/ChatGUI$2.class Binary files differnew file mode 100644 index 0000000..9c726a5 --- /dev/null +++ b/src/chatApp/ChatGUI$2.class diff --git a/src/chatApp/ChatGUI$3.class b/src/chatApp/ChatGUI$3.class Binary files differnew file mode 100644 index 0000000..6c7c945 --- /dev/null +++ b/src/chatApp/ChatGUI$3.class diff --git a/src/chatApp/ChatGUI.class b/src/chatApp/ChatGUI.class Binary files differnew file mode 100644 index 0000000..72f3bc7 --- /dev/null +++ b/src/chatApp/ChatGUI.class 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") + // <editor-fold defaultstate="collapsed" desc="Generated Code"> + 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); + }// </editor-fold> + + 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); + } + //</editor-fold> + + /* 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 Binary files differnew file mode 100644 index 0000000..db63e6b --- /dev/null +++ b/src/chatApp/ChattingUI$1.class diff --git a/src/chatApp/ChattingUI$2.class b/src/chatApp/ChattingUI$2.class Binary files differnew file mode 100644 index 0000000..e764ea1 --- /dev/null +++ b/src/chatApp/ChattingUI$2.class diff --git a/src/chatApp/ChattingUI$3.class b/src/chatApp/ChattingUI$3.class Binary files differnew file mode 100644 index 0000000..a319de8 --- /dev/null +++ b/src/chatApp/ChattingUI$3.class diff --git a/src/chatApp/ChattingUI.class b/src/chatApp/ChattingUI.class Binary files differnew file mode 100644 index 0000000..770f2e4 --- /dev/null +++ b/src/chatApp/ChattingUI.class diff --git a/src/chatApp/ClientGUI$1.class b/src/chatApp/ClientGUI$1.class Binary files differnew file mode 100644 index 0000000..081ae66 --- /dev/null +++ b/src/chatApp/ClientGUI$1.class diff --git a/src/chatApp/ClientGUI$2.class b/src/chatApp/ClientGUI$2.class Binary files differnew file mode 100644 index 0000000..0cf7cc8 --- /dev/null +++ b/src/chatApp/ClientGUI$2.class diff --git a/src/chatApp/ClientGUI.class b/src/chatApp/ClientGUI.class Binary files differnew file mode 100644 index 0000000..9419057 --- /dev/null +++ b/src/chatApp/ClientGUI.class diff --git a/src/chatApp/ClientGUI.java b/src/chatApp/ClientGUI.java new file mode 100644 index 0000000..cfb7726 --- /dev/null +++ b/src/chatApp/ClientGUI.java @@ -0,0 +1,109 @@ +package chatApp; + +import java.net.* ; +import java.io.* ; + +public class ClientGUI + { + + static LoginGUI login = new LoginGUI() ; + static ChatGUI chatRoom = new ChatGUI() ; + + @SuppressWarnings( "javadoc" ) + public static void main( String[] args ) throws IOException + { + + login.setVisible( true ); + + boolean done = false ; + while( !done ) + { + done = login.isDone() ; + System.out.print(""); + } + + String ip = login.getIP() ; + String username = login.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/chatApp/LoginGUI$1.class b/src/chatApp/LoginGUI$1.class Binary files differnew file mode 100644 index 0000000..2a7bd26 --- /dev/null +++ b/src/chatApp/LoginGUI$1.class diff --git a/src/chatApp/LoginGUI$2.class b/src/chatApp/LoginGUI$2.class Binary files differnew file mode 100644 index 0000000..d2fe6ce --- /dev/null +++ b/src/chatApp/LoginGUI$2.class diff --git a/src/chatApp/LoginGUI$3.class b/src/chatApp/LoginGUI$3.class Binary files differnew file mode 100644 index 0000000..1b71f0a --- /dev/null +++ b/src/chatApp/LoginGUI$3.class diff --git a/src/chatApp/LoginGUI$4.class b/src/chatApp/LoginGUI$4.class Binary files differnew file mode 100644 index 0000000..7aaabcb --- /dev/null +++ b/src/chatApp/LoginGUI$4.class diff --git a/src/chatApp/LoginGUI$5.class b/src/chatApp/LoginGUI$5.class Binary files differnew file mode 100644 index 0000000..56d6eec --- /dev/null +++ b/src/chatApp/LoginGUI$5.class diff --git a/src/chatApp/LoginGUI$6.class b/src/chatApp/LoginGUI$6.class Binary files differnew file mode 100644 index 0000000..1102a99 --- /dev/null +++ b/src/chatApp/LoginGUI$6.class diff --git a/src/chatApp/LoginGUI.class b/src/chatApp/LoginGUI.class Binary files differnew file mode 100644 index 0000000..ae0c7ee --- /dev/null +++ b/src/chatApp/LoginGUI.class 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") +// <editor-fold defaultstate="collapsed" desc="Generated Code"> +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); + }// </editor-fold> + + 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); + } + //</editor-fold> + + /* 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 Binary files differnew file mode 100644 index 0000000..1dd8a39 --- /dev/null +++ b/src/chatApp/loginDEMO$1.class diff --git a/src/chatApp/loginDEMO$2.class b/src/chatApp/loginDEMO$2.class Binary files differnew file mode 100644 index 0000000..887c167 --- /dev/null +++ b/src/chatApp/loginDEMO$2.class diff --git a/src/chatApp/loginDEMO.class b/src/chatApp/loginDEMO.class Binary files differnew file mode 100644 index 0000000..76e8169 --- /dev/null +++ b/src/chatApp/loginDEMO.class |
