Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package DiceGame2;
- import java.net.*;
- import java.io.*;
- import java.util.*;
- @SuppressWarnings("serial")
- public class ChatClient2 extends javax.swing.JFrame
- {
- String username, address;
- ArrayList<String> users = new ArrayList<String>();
- int port = 2222;
- int playerScore = 0;
- public int rollAmnt = 0;
- Boolean isConnected = false;
- private Dice2 dice = new Dice2();
- public static int status = 0;
- private ChatServer2 tellallserver = new ChatServer2();
- Socket sock;
- BufferedReader reader;
- PrintWriter writer;
- //Communication Functions
- public void ListenThread()
- {
- Thread IncomingReader = new Thread(new IncomingReader());
- IncomingReader.start();
- }
- public void userAdd(String data)
- {
- users.add(data);
- //tellallserver.tellEveryone("Hello");
- }
- 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");
- }
- //tellallserver.tellEveryone("Hello");
- }
- 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);
- }
- //Creates Chat Client
- public ChatClient2()
- {
- 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) { }
- }
- }
- //GUI Creation/Implementation
- @SuppressWarnings("unchecked")
- 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();
- b_roll = new javax.swing.JButton();
- b_again = new javax.swing.JButton();
- lb_name = new javax.swing.JLabel();
- setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
- setTitle("Chat - Client's frame");
- setName("client");
- setResizable(false);
- lb_address.setText("Address : ");
- tf_address.setText("");
- //address = "192.168.1.3";
- address = tf_address.getText();
- tf_address.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- tf_addressActionPerformed(evt);
- }
- });
- lb_port.setText("Port :");
- //Auto Sets Port to 2222
- 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);
- b_send.setText("SEND");
- b_send.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- b_sendActionPerformed(evt);
- }
- });
- b_roll.setText("Roll Dice");
- b_roll.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- b_rollActionPerformed(evt);
- }
- });
- b_again.setText("Play Again");
- b_again.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- b_againActionPerformed(evt);
- }
- });
- lb_name.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
- //Add GUI
- 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()
- .addComponent(tf_chat, javax.swing.GroupLayout.PREFERRED_SIZE, 352, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(b_send, javax.swing.GroupLayout.DEFAULT_SIZE, 111, Short.MAX_VALUE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(b_roll, javax.swing.GroupLayout.DEFAULT_SIZE, 111, Short.MAX_VALUE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(b_again, javax.swing.GroupLayout.DEFAULT_SIZE, 111, Short.MAX_VALUE))
- .addComponent(jScrollPane1)
- .addGroup(layout.createSequentialGroup()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
- .addComponent(lb_username, javax.swing.GroupLayout.DEFAULT_SIZE, 62, Short.MAX_VALUE)
- .addComponent(lb_address, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
- .addGap(18, 18, 18)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
- .addComponent(tf_address, javax.swing.GroupLayout.DEFAULT_SIZE, 89, Short.MAX_VALUE)
- .addComponent(tf_username))
- .addGap(18, 18, 18)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
- .addComponent(lb_password, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(lb_port, 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(tf_password)
- .addComponent(tf_port, javax.swing.GroupLayout.DEFAULT_SIZE, 50, Short.MAX_VALUE))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addComponent(b_connect)
- .addGap(2, 2, 2)
- .addComponent(b_disconnect)
- .addGap(0, 0, Short.MAX_VALUE))
- .addComponent(b_anonymous, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
- .addContainerGap())
- .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
- .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(lb_name)
- .addGap(201, 201, 201))
- );
- 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.LEADING, false)
- .addComponent(tf_username)
- .addComponent(tf_password)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(lb_username)
- .addComponent(lb_password)
- .addComponent(b_connect)
- .addComponent(b_disconnect)))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
- .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 310, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(tf_chat)
- .addComponent(b_send, javax.swing.GroupLayout.DEFAULT_SIZE, 31, Short.MAX_VALUE)
- .addComponent(b_roll, javax.swing.GroupLayout.DEFAULT_SIZE, 31, Short.MAX_VALUE)
- .addComponent(b_again, javax.swing.GroupLayout.DEFAULT_SIZE, 31, Short.MAX_VALUE))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(lb_name))
- );
- pack();
- }
- 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
- {
- address = tf_address.getText();
- 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");
- }
- ta_chat.append("Welcome to Dice Poker " + username + "!\nThe rules are simple. Each player will\ncontinue clicking the roll dice button\nuntil both players "
- + "are finished and\n a winner is declared.\n Click play again to go another round!\n\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) {//GEN-FIRST:event_b_sendActionPerformed
- 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();
- }
- tf_chat.setText("");
- tf_chat.requestFocus();
- }
- private void b_rollActionPerformed(java.awt.event.ActionEvent evt)
- {
- int[] diceCounts = new int [6];
- //int playerScore = 0;
- //int rollAmnt = 0;
- rollAmnt = rollAmnt + 1;
- ta_chat.append(username + ": ");
- for(int i = 0; i < 5; i++)
- {
- diceCounts[i] = dice.rollDice();
- ta_chat.append(diceCounts[i] + " ");
- }
- //ta_chat.append(Dice.getResult(diceCounts));
- if(Dice2.getResult(diceCounts).equals("Five of a kind"))
- {
- playerScore = playerScore + 20;
- if(playerScore >= 40)
- {
- ta_chat.append("\n" + username + " Finishes the Game in " + rollAmnt + "rolls!");
- }
- }
- else if(Dice2.getResult(diceCounts).equals("Four of a kind"))
- {
- playerScore = playerScore + 10;
- if(playerScore >= 40)
- {
- ta_chat.append("\n" + username + " Finishes the Game in " + rollAmnt + "rolls!");
- }
- }
- else if(Dice2.getResult(diceCounts).equals("Three of a kind"))
- {
- playerScore = playerScore + 7;
- if(playerScore >= 40)
- {
- ta_chat.append("\n" + username + " Finishes the Game in " + rollAmnt + "rolls!");
- }
- }
- else if(Dice2.getResult(diceCounts).equals("Full House"))
- {
- playerScore = playerScore + 15;
- if(playerScore >= 40)
- {
- ta_chat.append("\n" + username + " Finishes the Game in " + rollAmnt + "rolls!");
- }
- }
- else if(Dice2.getResult(diceCounts).equals("Two Pairs"))
- {
- playerScore = playerScore + 5;
- if(playerScore >= 40)
- {
- ta_chat.append("\n" + username + " Finishes the Game in " + rollAmnt + "rolls!");
- }
- }
- else if(Dice2.getResult(diceCounts).equals("One Pair"))
- {
- playerScore = playerScore + 2;
- if(playerScore >= 40)
- {
- ta_chat.append("\n" + username + " Finishes the Game in " + rollAmnt + "rolls!");
- }
- }
- ta_chat.append("\n" + username + " got " + Dice2.getResult(diceCounts) + "!");
- ta_chat.append("\n");
- if(playerScore < 40)
- {
- ta_chat.append("Keep Rolling!\n\n");
- }
- else if(playerScore >=40)
- {
- String message = username + " has finished with " + rollAmnt + " rolls!";
- b_roll.setEnabled(false);
- //tellallserver.tellEveryone(username + " has finished with " + rollAmnt + " rolls!");
- //tellallserver.tellEveryone(message);
- tf_chat.setText(message);
- b_send.doClick();
- ta_chat.append("Click 'Play again' button to keep playing\n\n");
- }
- }
- private void b_againActionPerformed(java.awt.event.ActionEvent evt)
- {
- b_roll.setEnabled(true);
- rollAmnt = 0;
- playerScore = 0;
- }
- //Main
- public static void main(String args[])
- {
- java.awt.EventQueue.invokeLater(new Runnable()
- {
- @Override
- public void run()
- {
- new ChatClient2().setVisible(true);
- }
- });
- }
- // Variables declaration
- 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.JButton b_roll;
- private javax.swing.JButton b_again;
- private javax.swing.JScrollPane jScrollPane1;
- private javax.swing.JLabel lb_address;
- private javax.swing.JLabel lb_name;
- 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;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement