Advertisement
Guest User

Client

a guest
Mar 19th, 2017
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 18.62 KB | None | 0 0
  1. package auto_crit_client;
  2.  
  3. import com.mindgene.d20.common.dice.Dice;
  4. import java.net.*;
  5. import java.io.*;
  6. import java.util.*;
  7.  
  8. public class Auto_Crit_VTT extends javax.swing.JFrame {
  9.  
  10.     String username, address = "localhost";
  11.     ArrayList<String> users = new ArrayList();
  12.     int port = 2222;
  13.     Boolean isConnected = false;
  14.  
  15.     Socket sock;
  16.     BufferedReader reader;
  17.     PrintWriter writer;
  18.    
  19.     String dicetype = "";
  20.     Random generator = new Random();
  21.  
  22.     //--------------------------//
  23.     public void ListenThread() {
  24.         Thread IncomingReader = new Thread(new IncomingReader());
  25.         IncomingReader.start();
  26.     }
  27.  
  28.     //--------------------------//
  29.     public void userAdd(String data) {
  30.         users.add(data);
  31.     }
  32.  
  33.     //--------------------------//
  34.     public void userRemove(String data) {
  35.         ta_chat.append(data + " is now offline.\n");
  36.     }
  37.  
  38.     //--------------------------//
  39.     public void writeUsers() {
  40.         String[] tempList = new String[(users.size())];
  41.         users.toArray(tempList);
  42.         for (String token : tempList) {
  43.             //users.append(token + "\n");
  44.         }
  45.     }
  46.  
  47.     //--------------------------//
  48.     public void sendDisconnect() {
  49.         String bye = (username + ": :Disconnect");
  50.         try {
  51.             writer.println(bye);
  52.             writer.flush();
  53.         } catch (Exception e) {
  54.             ta_chat.append("Could not send Disconnect message.\n");
  55.         }
  56.     }
  57.  
  58.     //--------------------------//
  59.     public void Disconnect() {
  60.         try {
  61.             ta_chat.append("Disconnected.\n");
  62.             sock.close();
  63.         } catch (Exception ex) {
  64.             ta_chat.append("Failed to disconnect. \n");
  65.         }
  66.         isConnected = false;
  67.         tf_username.setEditable(true);
  68.  
  69.     }
  70.    
  71.     private int roll( int min, int max){
  72.         return generator.nextInt( max) + min;
  73.     }
  74.  
  75.     public int diceRoll( String roll){
  76.         String minMax[] = roll.split( "d");
  77.         return roll( Integer.parseInt( minMax[0]), Integer.parseInt( minMax[1]));
  78.     }
  79.  
  80.     public Auto_Crit_VTT() {
  81.         initComponents();
  82.     }
  83.  
  84.     //--------------------------//
  85.     public class IncomingReader implements Runnable {
  86.  
  87.         @Override
  88.         public void run() {
  89.             String[] data;
  90.             String stream, done = "Done", connect = "Connect", disconnect = "Disconnect", chat = "Chat";
  91.  
  92.             try {
  93.                 while ((stream = reader.readLine()) != null) {
  94.                     data = stream.split(":");
  95.  
  96.                     if (data[2].equals(chat)) {
  97.                         ta_chat.append(data[0] + ": " + data[1] + "\n");
  98.                         ta_chat.setCaretPosition(ta_chat.getDocument().getLength());
  99.                     } else if (data[2].equals(connect)) {
  100.                         ta_chat.removeAll();
  101.                         userAdd(data[0]);
  102.                     } else if (data[2].equals(disconnect)) {
  103.                         userRemove(data[0]);
  104.                     } else if (data[2].equals(done)) {
  105.                         //users.setText("");
  106.                         writeUsers();
  107.                         users.clear();
  108.                     }
  109.                 }
  110.             } catch (Exception ex) {
  111.             }
  112.         }
  113.     }
  114.  
  115.     //--------------------------//
  116.     @SuppressWarnings("unchecked")
  117.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  118.     private void initComponents() {
  119.  
  120.         lb_address = new javax.swing.JLabel();
  121.         tf_address = new javax.swing.JTextField();
  122.         lb_port = new javax.swing.JLabel();
  123.         tf_port = new javax.swing.JTextField();
  124.         lb_username = new javax.swing.JLabel();
  125.         tf_username = new javax.swing.JTextField();
  126.         lb_password = new javax.swing.JLabel();
  127.         tf_password = new javax.swing.JTextField();
  128.         b_connect = new javax.swing.JButton();
  129.         b_disconnect = new javax.swing.JButton();
  130.         b_anonymous = new javax.swing.JButton();
  131.         jScrollPane1 = new javax.swing.JScrollPane();
  132.         ta_chat = new javax.swing.JTextArea();
  133.         tf_chat = new javax.swing.JTextField();
  134.         b_send = new javax.swing.JButton();
  135.         jPanel1 = new javax.swing.JPanel();
  136.  
  137.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  138.         setTitle("Chat - Client's frame");
  139.         setName("client"); // NOI18N
  140.         setResizable(false);
  141.  
  142.         lb_address.setText("Address : ");
  143.  
  144.         tf_address.setText("localhost");
  145.         tf_address.addActionListener(new java.awt.event.ActionListener() {
  146.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  147.                 tf_addressActionPerformed(evt);
  148.             }
  149.         });
  150.  
  151.         lb_port.setText("Port :");
  152.  
  153.         tf_port.setText("2222");
  154.         tf_port.addActionListener(new java.awt.event.ActionListener() {
  155.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  156.                 tf_portActionPerformed(evt);
  157.             }
  158.         });
  159.  
  160.         lb_username.setText("Username :");
  161.  
  162.         tf_username.addActionListener(new java.awt.event.ActionListener() {
  163.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  164.                 tf_usernameActionPerformed(evt);
  165.             }
  166.         });
  167.  
  168.         lb_password.setText("Password : ");
  169.  
  170.         b_connect.setText("Connect");
  171.         b_connect.addActionListener(new java.awt.event.ActionListener() {
  172.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  173.                 b_connectActionPerformed(evt);
  174.             }
  175.         });
  176.  
  177.         b_disconnect.setText("Disconnect");
  178.         b_disconnect.addActionListener(new java.awt.event.ActionListener() {
  179.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  180.                 b_disconnectActionPerformed(evt);
  181.             }
  182.         });
  183.  
  184.         b_anonymous.setText("Anonymous Login");
  185.         b_anonymous.addActionListener(new java.awt.event.ActionListener() {
  186.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  187.                 b_anonymousActionPerformed(evt);
  188.             }
  189.         });
  190.  
  191.         ta_chat.setColumns(20);
  192.         ta_chat.setRows(5);
  193.         jScrollPane1.setViewportView(ta_chat);
  194.  
  195.         tf_chat.addActionListener(new java.awt.event.ActionListener() {
  196.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  197.                 tf_chatActionPerformed(evt);
  198.             }
  199.         });
  200.  
  201.         b_send.setText("SEND");
  202.         b_send.addActionListener(new java.awt.event.ActionListener() {
  203.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  204.                 b_sendActionPerformed(evt);
  205.             }
  206.         });
  207.  
  208.         jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
  209.  
  210.         javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
  211.         jPanel1.setLayout(jPanel1Layout);
  212.         jPanel1Layout.setHorizontalGroup(
  213.             jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  214.             .addGap(0, 0, Short.MAX_VALUE)
  215.         );
  216.         jPanel1Layout.setVerticalGroup(
  217.             jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  218.             .addGap(0, 0, Short.MAX_VALUE)
  219.         );
  220.  
  221.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  222.         getContentPane().setLayout(layout);
  223.         layout.setHorizontalGroup(
  224.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  225.             .addGroup(layout.createSequentialGroup()
  226.                 .addContainerGap()
  227.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  228.                     .addGroup(layout.createSequentialGroup()
  229.                         .addGap(0, 290, Short.MAX_VALUE)
  230.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
  231.                             .addGroup(layout.createSequentialGroup()
  232.                                 .addComponent(lb_username, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)
  233.                                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  234.                                 .addComponent(tf_username, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
  235.                                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  236.                                 .addComponent(lb_password)
  237.                                 .addGap(18, 18, 18)
  238.                                 .addComponent(tf_password, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
  239.                             .addGroup(layout.createSequentialGroup()
  240.                                 .addComponent(lb_address, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)
  241.                                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  242.                                 .addComponent(tf_address, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
  243.                                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  244.                                 .addComponent(lb_port, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
  245.                                 .addGap(18, 18, 18)
  246.                                 .addComponent(tf_port, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)))
  247.                         .addGap(18, 18, 18)
  248.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
  249.                             .addGroup(layout.createSequentialGroup()
  250.                                 .addComponent(b_connect)
  251.                                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  252.                                 .addComponent(b_disconnect))
  253.                             .addComponent(b_anonymous, javax.swing.GroupLayout.PREFERRED_SIZE, 164, javax.swing.GroupLayout.PREFERRED_SIZE)))
  254.                     .addGroup(layout.createSequentialGroup()
  255.                         .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  256.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  257.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  258.                             .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 240, javax.swing.GroupLayout.PREFERRED_SIZE)
  259.                             .addGroup(layout.createSequentialGroup()
  260.                                 .addComponent(tf_chat, javax.swing.GroupLayout.PREFERRED_SIZE, 175, javax.swing.GroupLayout.PREFERRED_SIZE)
  261.                                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  262.                                 .addComponent(b_send)))))
  263.                 .addContainerGap())
  264.         );
  265.         layout.setVerticalGroup(
  266.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  267.             .addGroup(layout.createSequentialGroup()
  268.                 .addContainerGap()
  269.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  270.                     .addComponent(lb_address)
  271.                     .addComponent(tf_address, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  272.                     .addComponent(lb_port)
  273.                     .addComponent(tf_port, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  274.                     .addComponent(b_anonymous))
  275.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  276.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  277.                     .addComponent(lb_username)
  278.                     .addComponent(lb_password)
  279.                     .addComponent(b_connect)
  280.                     .addComponent(b_disconnect)
  281.                     .addComponent(tf_password, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  282.                     .addComponent(tf_username))
  283.                 .addGap(8, 8, 8)
  284.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  285.                     .addGroup(layout.createSequentialGroup()
  286.                         .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 310, javax.swing.GroupLayout.PREFERRED_SIZE)
  287.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  288.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  289.                             .addComponent(tf_chat)
  290.                             .addComponent(b_send)))
  291.                     .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  292.                 .addGap(27, 27, 27))
  293.         );
  294.  
  295.         pack();
  296.     }// </editor-fold>                        
  297.  
  298.     private void tf_addressActionPerformed(java.awt.event.ActionEvent evt) {                                          
  299.  
  300.     }                                          
  301.  
  302.     private void tf_portActionPerformed(java.awt.event.ActionEvent evt) {                                        
  303.  
  304.     }                                      
  305.  
  306.     private void tf_usernameActionPerformed(java.awt.event.ActionEvent evt) {                                            
  307.  
  308.     }                                          
  309.  
  310.     private void b_connectActionPerformed(java.awt.event.ActionEvent evt) {                                          
  311.         if (isConnected == false) {
  312.             username = tf_username.getText();
  313.             tf_username.setEditable(false);
  314.  
  315.             try {
  316.                 sock = new Socket(address, port);
  317.                 InputStreamReader streamreader = new InputStreamReader(sock.getInputStream());
  318.                 reader = new BufferedReader(streamreader);
  319.                 writer = new PrintWriter(sock.getOutputStream());
  320.                 writer.println(username + ":has connected.:Connect");
  321.                 writer.flush();
  322.                 isConnected = true;
  323.             } catch (Exception ex) {
  324.                 ta_chat.append("Cannot Connect! Try Again. \n");
  325.                 tf_username.setEditable(true);
  326.             }
  327.  
  328.             ListenThread();
  329.  
  330.         } else if (isConnected == true) {
  331.             ta_chat.append("You are already connected. \n");
  332.         }
  333.     }                                        
  334.  
  335.     private void b_disconnectActionPerformed(java.awt.event.ActionEvent evt) {                                            
  336.         sendDisconnect();
  337.         Disconnect();
  338.     }                                            
  339.  
  340.     private void b_anonymousActionPerformed(java.awt.event.ActionEvent evt) {                                            
  341.         tf_username.setText("");
  342.         if (isConnected == false) {
  343.             String anon = "anon";
  344.             Random generator = new Random();
  345.             int i = generator.nextInt(999) + 1;
  346.             String is = String.valueOf(i);
  347.             anon = anon.concat(is);
  348.             username = anon;
  349.  
  350.             tf_username.setText(anon);
  351.             tf_username.setEditable(false);
  352.  
  353.             try {
  354.                 sock = new Socket(address, port);
  355.                 InputStreamReader streamreader = new InputStreamReader(sock.getInputStream());
  356.                 reader = new BufferedReader(streamreader);
  357.                 writer = new PrintWriter(sock.getOutputStream());
  358.                 writer.println(anon + ":has connected.:Connect");
  359.                 writer.flush();
  360.                 isConnected = true;
  361.             } catch (Exception ex) {
  362.                 ta_chat.append("Cannot Connect! Try Again. \n");
  363.                 tf_username.setEditable(true);
  364.             }
  365.  
  366.             ListenThread();
  367.  
  368.         } else if (isConnected == true) {
  369.             ta_chat.append("You are already connected. \n");
  370.         }
  371.     }                                          
  372.  
  373.     private void b_sendActionPerformed(java.awt.event.ActionEvent evt) {                                      
  374.         String nothing = "";
  375.         if ((tf_chat.getText()).equals(nothing)) {
  376.             tf_chat.setText("");
  377.             tf_chat.requestFocus();
  378.         } else {
  379.             try {
  380.                 writer.println(username + ":" + tf_chat.getText() + ":" + "Chat");
  381.                 writer.flush(); // flushes the buffer
  382.             } catch (Exception ex) {
  383.                 ta_chat.append("Message was not sent. \n");
  384.             }
  385.             tf_chat.setText("");
  386.             tf_chat.requestFocus();
  387.         }
  388.        
  389.         if ((tf_chat.getText()).equals(dicetype)) {
  390.             Auto_Crit_VTT d = new Auto_Crit_VTT();
  391.             System.out.println( d.diceRoll( "1d20"));
  392.         }
  393.  
  394.         tf_chat.setText("");
  395.         tf_chat.requestFocus();
  396.     }                                      
  397.  
  398.     private void tf_chatActionPerformed(java.awt.event.ActionEvent evt) {                                        
  399.         // TODO add your handling code here:
  400.     }                                      
  401.  
  402.     public static void main(String args[]) {
  403.         java.awt.EventQueue.invokeLater(new Runnable() {
  404.             @Override
  405.             public void run() {
  406.                 new Auto_Crit_VTT().setVisible(true);
  407.             }
  408.         });
  409.     }
  410.  
  411.     // Variables declaration - do not modify                    
  412.     private javax.swing.JButton b_anonymous;
  413.     private javax.swing.JButton b_connect;
  414.     private javax.swing.JButton b_disconnect;
  415.     private javax.swing.JButton b_send;
  416.     private javax.swing.JPanel jPanel1;
  417.     private javax.swing.JScrollPane jScrollPane1;
  418.     private javax.swing.JLabel lb_address;
  419.     private javax.swing.JLabel lb_password;
  420.     private javax.swing.JLabel lb_port;
  421.     private javax.swing.JLabel lb_username;
  422.     private javax.swing.JTextArea ta_chat;
  423.     private javax.swing.JTextField tf_address;
  424.     private javax.swing.JTextField tf_chat;
  425.     private javax.swing.JTextField tf_password;
  426.     private javax.swing.JTextField tf_port;
  427.     private javax.swing.JTextField tf_username;
  428.     // End of variables declaration                  
  429. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement