Advertisement
Guest User

Auto_Crit_Client

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