Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package auto_crit_client;
- import com.mindgene.d20.common.dice.Dice;
- import java.net.*;
- import java.io.*;
- import java.util.*;
- public class Auto_Crit_VTT extends javax.swing.JFrame {
- String username, address = "localhost";
- ArrayList<String> users = new ArrayList();
- int port = 2222;
- Boolean isConnected = false;
- Socket sock;
- BufferedReader reader;
- PrintWriter writer;
- String dicetype = "";
- Random generator = new Random();
- //--------------------------//
- public void ListenThread() {
- Thread IncomingReader = new Thread(new IncomingReader());
- IncomingReader.start();
- }
- //--------------------------//
- public void userAdd(String data) {
- users.add(data);
- }
- //--------------------------//
- public void userRemove(String data) {
- ta_chat.append(data + " is now offline.\n");
- }
- //--------------------------//
- public void writeUsers() {
- String[] tempList = new String[(users.size())];
- users.toArray(tempList);
- for (String token : tempList) {
- //users.append(token + "\n");
- }
- }
- //--------------------------//
- public void sendDisconnect() {
- String bye = (username + ": :Disconnect");
- try {
- writer.println(bye);
- writer.flush();
- } catch (Exception e) {
- ta_chat.append("Could not send Disconnect message.\n");
- }
- }
- //--------------------------//
- public void Disconnect() {
- try {
- ta_chat.append("Disconnected.\n");
- sock.close();
- } catch (Exception ex) {
- ta_chat.append("Failed to disconnect. \n");
- }
- isConnected = false;
- tf_username.setEditable(true);
- }
- private int roll( int min, int max){
- return generator.nextInt( max) + min;
- }
- public int diceRoll( String roll){
- String minMax[] = roll.split( "d");
- return roll( Integer.parseInt( minMax[0]), Integer.parseInt( minMax[1]));
- }
- public Auto_Crit_VTT() {
- initComponents();
- }
- //--------------------------//
- public class IncomingReader implements Runnable {
- @Override
- public void run() {
- String[] data;
- String stream, done = "Done", connect = "Connect", disconnect = "Disconnect", chat = "Chat";
- try {
- while ((stream = reader.readLine()) != null) {
- data = stream.split(":");
- if (data[2].equals(chat)) {
- ta_chat.append(data[0] + ": " + data[1] + "\n");
- ta_chat.setCaretPosition(ta_chat.getDocument().getLength());
- } else if (data[2].equals(connect)) {
- ta_chat.removeAll();
- userAdd(data[0]);
- } else if (data[2].equals(disconnect)) {
- userRemove(data[0]);
- } else if (data[2].equals(done)) {
- //users.setText("");
- writeUsers();
- users.clear();
- }
- }
- } catch (Exception ex) {
- }
- }
- }
- //--------------------------//
- @SuppressWarnings("unchecked")
- // <editor-fold defaultstate="collapsed" desc="Generated Code">
- private void initComponents() {
- lb_address = new javax.swing.JLabel();
- tf_address = new javax.swing.JTextField();
- lb_port = new javax.swing.JLabel();
- tf_port = new javax.swing.JTextField();
- lb_username = new javax.swing.JLabel();
- tf_username = new javax.swing.JTextField();
- lb_password = new javax.swing.JLabel();
- tf_password = new javax.swing.JTextField();
- b_connect = new javax.swing.JButton();
- b_disconnect = new javax.swing.JButton();
- b_anonymous = new javax.swing.JButton();
- jScrollPane1 = new javax.swing.JScrollPane();
- ta_chat = new javax.swing.JTextArea();
- tf_chat = new javax.swing.JTextField();
- b_send = new javax.swing.JButton();
- jPanel1 = new javax.swing.JPanel();
- setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
- setTitle("Chat - Client's frame");
- setName("client"); // NOI18N
- setResizable(false);
- lb_address.setText("Address : ");
- tf_address.setText("localhost");
- tf_address.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- tf_addressActionPerformed(evt);
- }
- });
- lb_port.setText("Port :");
- tf_port.setText("2222");
- tf_port.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- tf_portActionPerformed(evt);
- }
- });
- lb_username.setText("Username :");
- tf_username.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- tf_usernameActionPerformed(evt);
- }
- });
- lb_password.setText("Password : ");
- b_connect.setText("Connect");
- b_connect.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- b_connectActionPerformed(evt);
- }
- });
- b_disconnect.setText("Disconnect");
- b_disconnect.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- b_disconnectActionPerformed(evt);
- }
- });
- b_anonymous.setText("Anonymous Login");
- b_anonymous.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- b_anonymousActionPerformed(evt);
- }
- });
- ta_chat.setColumns(20);
- ta_chat.setRows(5);
- jScrollPane1.setViewportView(ta_chat);
- tf_chat.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- tf_chatActionPerformed(evt);
- }
- });
- b_send.setText("SEND");
- b_send.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- b_sendActionPerformed(evt);
- }
- });
- jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
- javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
- jPanel1.setLayout(jPanel1Layout);
- jPanel1Layout.setHorizontalGroup(
- jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGap(0, 0, Short.MAX_VALUE)
- );
- jPanel1Layout.setVerticalGroup(
- jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGap(0, 0, Short.MAX_VALUE)
- );
- 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()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addGap(0, 290, Short.MAX_VALUE)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
- .addGroup(layout.createSequentialGroup()
- .addComponent(lb_username, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(tf_username, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(lb_password)
- .addGap(18, 18, 18)
- .addComponent(tf_password, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGroup(layout.createSequentialGroup()
- .addComponent(lb_address, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(tf_address, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(lb_port, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGap(18, 18, 18)
- .addComponent(tf_port, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)))
- .addGap(18, 18, 18)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
- .addGroup(layout.createSequentialGroup()
- .addComponent(b_connect)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(b_disconnect))
- .addComponent(b_anonymous, javax.swing.GroupLayout.PREFERRED_SIZE, 164, javax.swing.GroupLayout.PREFERRED_SIZE)))
- .addGroup(layout.createSequentialGroup()
- .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 240, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGroup(layout.createSequentialGroup()
- .addComponent(tf_chat, javax.swing.GroupLayout.PREFERRED_SIZE, 175, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(b_send)))))
- .addContainerGap())
- );
- layout.setVerticalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addContainerGap()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(lb_address)
- .addComponent(tf_address, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(lb_port)
- .addComponent(tf_port, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(b_anonymous))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(lb_username)
- .addComponent(lb_password)
- .addComponent(b_connect)
- .addComponent(b_disconnect)
- .addComponent(tf_password, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(tf_username))
- .addGap(8, 8, 8)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 310, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(tf_chat)
- .addComponent(b_send)))
- .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
- .addGap(27, 27, 27))
- );
- pack();
- }// </editor-fold>
- private void tf_addressActionPerformed(java.awt.event.ActionEvent evt) {
- }
- private void tf_portActionPerformed(java.awt.event.ActionEvent evt) {
- }
- private void tf_usernameActionPerformed(java.awt.event.ActionEvent evt) {
- }
- private void b_connectActionPerformed(java.awt.event.ActionEvent evt) {
- if (isConnected == false) {
- username = tf_username.getText();
- tf_username.setEditable(false);
- try {
- sock = new Socket(address, port);
- InputStreamReader streamreader = new InputStreamReader(sock.getInputStream());
- reader = new BufferedReader(streamreader);
- writer = new PrintWriter(sock.getOutputStream());
- writer.println(username + ":has connected.:Connect");
- writer.flush();
- isConnected = true;
- } catch (Exception ex) {
- ta_chat.append("Cannot Connect! Try Again. \n");
- tf_username.setEditable(true);
- }
- ListenThread();
- } else if (isConnected == true) {
- ta_chat.append("You are already connected. \n");
- }
- }
- private void b_disconnectActionPerformed(java.awt.event.ActionEvent evt) {
- sendDisconnect();
- Disconnect();
- }
- private void b_anonymousActionPerformed(java.awt.event.ActionEvent evt) {
- tf_username.setText("");
- if (isConnected == false) {
- String anon = "anon";
- Random generator = new Random();
- int i = generator.nextInt(999) + 1;
- String is = String.valueOf(i);
- anon = anon.concat(is);
- username = anon;
- tf_username.setText(anon);
- tf_username.setEditable(false);
- try {
- sock = new Socket(address, port);
- InputStreamReader streamreader = new InputStreamReader(sock.getInputStream());
- reader = new BufferedReader(streamreader);
- writer = new PrintWriter(sock.getOutputStream());
- writer.println(anon + ":has connected.:Connect");
- writer.flush();
- isConnected = true;
- } catch (Exception ex) {
- ta_chat.append("Cannot Connect! Try Again. \n");
- tf_username.setEditable(true);
- }
- ListenThread();
- } else if (isConnected == true) {
- ta_chat.append("You are already connected. \n");
- }
- }
- private void b_sendActionPerformed(java.awt.event.ActionEvent evt) {
- String nothing = "";
- if ((tf_chat.getText()).equals(nothing)) {
- tf_chat.setText("");
- tf_chat.requestFocus();
- } else {
- try {
- writer.println(username + ":" + tf_chat.getText() + ":" + "Chat");
- writer.flush(); // flushes the buffer
- } catch (Exception ex) {
- ta_chat.append("Message was not sent. \n");
- }
- tf_chat.setText("");
- tf_chat.requestFocus();
- }
- if ((tf_chat.getText()).equals(dicetype)) {
- Auto_Crit_VTT d = new Auto_Crit_VTT();
- System.out.println( d.diceRoll( "1d20"));
- }
- tf_chat.setText("");
- tf_chat.requestFocus();
- }
- private void tf_chatActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- }
- public static void main(String args[]) {
- java.awt.EventQueue.invokeLater(new Runnable() {
- @Override
- public void run() {
- new Auto_Crit_VTT().setVisible(true);
- }
- });
- }
- // Variables declaration - do not modify
- private javax.swing.JButton b_anonymous;
- private javax.swing.JButton b_connect;
- private javax.swing.JButton b_disconnect;
- private javax.swing.JButton b_send;
- private javax.swing.JPanel jPanel1;
- private javax.swing.JScrollPane jScrollPane1;
- private javax.swing.JLabel lb_address;
- private javax.swing.JLabel lb_password;
- private javax.swing.JLabel lb_port;
- private javax.swing.JLabel lb_username;
- private javax.swing.JTextArea ta_chat;
- private javax.swing.JTextField tf_address;
- private javax.swing.JTextField tf_chat;
- private javax.swing.JTextField tf_password;
- private javax.swing.JTextField tf_port;
- private javax.swing.JTextField tf_username;
- // End of variables declaration
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement