From 4cf43a74049f6b4e277153a8464ac7a865d63f2f Mon Sep 17 00:00:00 2001 From: cisl Date: Sun, 7 May 2017 05:37:43 -0700 Subject: [PATCH 1/2] --- nbproject/private/private.properties | 4 +- src/chatGUI/ChatView.form | 35 ++++++++++++ src/chatGUI/ChatView.java | 83 ++++++++++++++++++++++++++++ 3 files changed, 120 insertions(+), 2 deletions(-) create mode 100644 src/chatGUI/ChatView.form create mode 100644 src/chatGUI/ChatView.java diff --git a/nbproject/private/private.properties b/nbproject/private/private.properties index 1b6c36a..bd22d7e 100644 --- a/nbproject/private/private.properties +++ b/nbproject/private/private.properties @@ -1,2 +1,2 @@ -compile.on.save=true -user.properties.file=C:\\Users\\ANDITYAARIFIANTO\\AppData\\Roaming\\NetBeans\\8.1\\build.properties +compile.on.save=true +user.properties.file=C:\\Users\\Ncisl\\AppData\\Roaming\\NetBeans\\8.1\\build.properties diff --git a/src/chatGUI/ChatView.form b/src/chatGUI/ChatView.form new file mode 100644 index 0000000..769eecd --- /dev/null +++ b/src/chatGUI/ChatView.form @@ -0,0 +1,35 @@ + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/src/chatGUI/ChatView.java b/src/chatGUI/ChatView.java new file mode 100644 index 0000000..065d2a6 --- /dev/null +++ b/src/chatGUI/ChatView.java @@ -0,0 +1,83 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package chatGUI; + +/** + * + * @author Ncisl + */ +public class ChatView extends javax.swing.JFrame { + + /** + * Creates new form ChatView + */ + public ChatView() { + 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") + // //GEN-BEGIN:initComponents + private void initComponents() { + + setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); + + javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); + getContentPane().setLayout(layout); + layout.setHorizontalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGap(0, 400, Short.MAX_VALUE) + ); + layout.setVerticalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGap(0, 300, Short.MAX_VALUE) + ); + + pack(); + }// //GEN-END:initComponents + + /** + * @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(ChatView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } catch (InstantiationException ex) { + java.util.logging.Logger.getLogger(ChatView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } catch (IllegalAccessException ex) { + java.util.logging.Logger.getLogger(ChatView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } catch (javax.swing.UnsupportedLookAndFeelException ex) { + java.util.logging.Logger.getLogger(ChatView.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 ChatView().setVisible(true); + } + }); + } + + // Variables declaration - do not modify//GEN-BEGIN:variables + // End of variables declaration//GEN-END:variables +} From 9065281ca68d0f28c5bf96efc45b4401fb50ea49 Mon Sep 17 00:00:00 2001 From: cisl Date: Sun, 7 May 2017 06:17:56 -0700 Subject: [PATCH 2/2] --- src/ConsoleApp/ConnectionThread.java | 53 ++++++++++++++++++++ src/ConsoleApp/ConsoleApplication.java | 63 +++++++++++++++++++++++ src/Driver/DriverClientConsole.java | 20 ++++++++ src/Driver/DriverClientGUI.java | 19 +++++++ src/Driver/DriverServer.java | 33 ++++++++++++ src/chatGUI/ChatController.java | 69 ++++++++++++++++++++++++++ src/chatGUI/ChatView.form | 41 ++++++++++++++- src/chatGUI/ChatView.java | 53 ++++++++++++++++++-- 8 files changed, 345 insertions(+), 6 deletions(-) create mode 100644 src/ConsoleApp/ConnectionThread.java create mode 100644 src/ConsoleApp/ConsoleApplication.java create mode 100644 src/Driver/DriverClientConsole.java create mode 100644 src/Driver/DriverClientGUI.java create mode 100644 src/Driver/DriverServer.java create mode 100644 src/chatGUI/ChatController.java diff --git a/src/ConsoleApp/ConnectionThread.java b/src/ConsoleApp/ConnectionThread.java new file mode 100644 index 0000000..a2c12cf --- /dev/null +++ b/src/ConsoleApp/ConnectionThread.java @@ -0,0 +1,53 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package ConsoleApp; + + +import java.io.IOException; +import java.net.Socket; +import java.util.logging.Level; +import java.util.logging.Logger; +import javaChat.Connection; + + + +/** + * + * @author lailis + */ +public class ConnectionThread { +private Socket client; +private Connection connection; + + public ConnectionThread (Socket newClient ) throws IOException{ + this.client = newClient; + connection = new Connection(client); + } + + public void run () { + try { + connection.startChat("Strat the Chat-----"); + System.out.println("-----------"); + System.out.println("new client connected"); + System.out.println("client information : "); + System.out.println(connection.getClientInformation()); + String inputan; + String message; + while ((inputan = connection.readStream())!= null && !inputan.equals("quit")){ + message = "Client " + connection.getIpClient() + "said : " + inputan; + System.out.println(message); + connection.sendToAll(message); + } + message = "Client from IP : " + connection.getIpClient() + "Quit from chat room "; + System.out.println(message); + connection.sendToAll(message); + connection.disconnect(); + } catch (IOException ex) { System.out.println("ERROR"); + } + } +} + diff --git a/src/ConsoleApp/ConsoleApplication.java b/src/ConsoleApp/ConsoleApplication.java new file mode 100644 index 0000000..8fb1a53 --- /dev/null +++ b/src/ConsoleApp/ConsoleApplication.java @@ -0,0 +1,63 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package ConsoleApp; +import java.io.IOException; +import java.util.logging.Level; +import java.util.logging.Logger; +import javaChat.ClientConnection; +/** + * + * @author lailis + */ +public class ConsoleApplication { + private ClientConnection client; + + public class ReadInput extends Thread{ + public void run (){ + try { + String inputKeyboard; + do { + System.out.println(">> "); + inputKeyboard = client.inputString(); + client.writeStream(inputKeyboard); + } while (! inputKeyboard.equals("quit")); + client.disconnect(); + } catch (IOException ex) { + System.out.println("Error"); + } + } + } + + public class WriteOutput extends Thread{ + public void run (){ + try { + String inputan; + while ((inputan = client.readStream()) != null){ + System.out.println(inputan); + System.out.println(">> "); + } + } catch (IOException ex) { + System.out.println("Error"); + } + } + } + + public void startChat() { + try { + client = new ClientConnection(); + System.out.println("input server IP : "); + String ip = client.inputString(); + client.connect(ip); + } catch (IOException ex) { + System.out.println("ERROR"); + } + ReadInput in = new ReadInput(); + WriteOutput out = new WriteOutput(); + in.start(); + out.start(); + } +} + diff --git a/src/Driver/DriverClientConsole.java b/src/Driver/DriverClientConsole.java new file mode 100644 index 0000000..ff4c916 --- /dev/null +++ b/src/Driver/DriverClientConsole.java @@ -0,0 +1,20 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package Driver; + +import ConsoleApp.ConsoleApplication; + +/** + * + * @author lailis + */ +public class DriverClientConsole { + public static void main(String[] args) { + ConsoleApplication app = new ConsoleApplication(); + app.startChat(); + } +} + diff --git a/src/Driver/DriverClientGUI.java b/src/Driver/DriverClientGUI.java new file mode 100644 index 0000000..5ea04dd --- /dev/null +++ b/src/Driver/DriverClientGUI.java @@ -0,0 +1,19 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package Driver; + +import chatGUI.ChatController; + +/** + * + * @author lailis + */ +public class DriverClientGUI { + public static void main(String[] args) { + new ChatController(); + } +} + diff --git a/src/Driver/DriverServer.java b/src/Driver/DriverServer.java new file mode 100644 index 0000000..82e55bd --- /dev/null +++ b/src/Driver/DriverServer.java @@ -0,0 +1,33 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package Driver; + +import ConsoleApp.ConnectionThread; +import java.io.IOException; +import javaChat.ServerConnection; + +/** + * + * @author lailis + * + */ +public class DriverServer { + public static void main(String[] args) { + try { + ServerConnection server = new ServerConnection(); + System.out.println("Server Information"); + System.out.println(server.getServerInformation()); + while (true){ + ConnectionThread connection = new ConnectionThread(server.getClient()); + connection.run(); + } + } catch (IOException ex) { + System.out.println("Error"); + } + } +} + + diff --git a/src/chatGUI/ChatController.java b/src/chatGUI/ChatController.java new file mode 100644 index 0000000..416273f --- /dev/null +++ b/src/chatGUI/ChatController.java @@ -0,0 +1,69 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package chatGUI; + +import ConsoleApp.ConsoleApplication.WriteOutput; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.io.IOException; +import javaChat.ClientConnection; +import javax.swing.JOptionPane; + +/** + * + * @author lailis + */ +public class ChatController implements ActionListener { + private ChatView view; + private ClientConnection client; + + public ChatController() { + view = new ChatView(); + view.setVisible(true); + view.addListener(this); + client = null; + } + + @Override + public void actionPerformed(ActionEvent e) { + Object source = e.getSource(); + if (source.equals(view.getTextField())){ + if (client == null){ + try { + client = new ClientConnection(); + view.setTxAreaCaht(view.getStringChat()); + String ip = view.getStringChat(); + client.connect(ip); + WriteOutput w = new WriteOutput(); + w.start(); + } catch (IOException ex) { + JOptionPane.showMessageDialog(null, "Error"); + } + } + else { + String input = view.getStringChat(); + client.writeStream(input); + view.setTextFieldChat(""); + } + } + } + + public class WriteOutput extends Thread { + + public void run() { + try { + String inp; + while ((inp = client.readStream()) != null) { + view.setTxAreaCaht(inp); + } + } catch (IOException ex) { + System.out.println("Error"); + } + } + } +} + + diff --git a/src/chatGUI/ChatView.form b/src/chatGUI/ChatView.form index 769eecd..7d02e8d 100644 --- a/src/chatGUI/ChatView.form +++ b/src/chatGUI/ChatView.form @@ -23,13 +23,50 @@ - + + + + + + + + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/chatGUI/ChatView.java b/src/chatGUI/ChatView.java index 065d2a6..0874cee 100644 --- a/src/chatGUI/ChatView.java +++ b/src/chatGUI/ChatView.java @@ -5,9 +5,11 @@ */ package chatGUI; +import java.awt.event.ActionListener; + /** * - * @author Ncisl + * @author lailis */ public class ChatView extends javax.swing.JFrame { @@ -27,22 +29,62 @@ public ChatView() { // //GEN-BEGIN:initComponents private void initComponents() { + jScrollPane1 = new javax.swing.JScrollPane(); + txAreaChat = new javax.swing.JTextArea(); + txFieldChat = new javax.swing.JTextField(); + setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); + txAreaChat.setEditable(false); + txAreaChat.setColumns(20); + txAreaChat.setRows(5); + txAreaChat.setText("Input Server IP Address"); + jScrollPane1.setViewportView(txAreaChat); + + txFieldChat.setText("jTextField1"); + javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addGap(0, 400, Short.MAX_VALUE) + .addGroup(layout.createSequentialGroup() + .addContainerGap() + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE) + .addComponent(txFieldChat)) + .addContainerGap()) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addGap(0, 300, Short.MAX_VALUE) + .addGroup(layout.createSequentialGroup() + .addContainerGap() + .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 202, javax.swing.GroupLayout.PREFERRED_SIZE) + .addGap(18, 18, 18) + .addComponent(txFieldChat, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) + .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); pack(); }// //GEN-END:initComponents - + public void setTxAreaCaht(String Message) { + txAreaChat.append(Message + "\n"); + } + + public Object getTextField() { + return txFieldChat; + } + + public String getStringChat() { + return txFieldChat.getText(); + } + + public void setTextFieldChat(String Message) { + this.txFieldChat.setText(Message); + } + + public void addListener (ActionListener e){ + txFieldChat.addActionListener(e); + } /** * @param args the command line arguments */ @@ -79,5 +121,8 @@ public void run() { } // Variables declaration - do not modify//GEN-BEGIN:variables + private javax.swing.JScrollPane jScrollPane1; + private javax.swing.JTextArea txAreaChat; + private javax.swing.JTextField txFieldChat; // End of variables declaration//GEN-END:variables }