Advertisement
Guest User

update

a guest
Feb 9th, 2017
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.92 KB | None | 0 0
  1. package tcpclient;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import java.net.*;
  6. import java.io.*;
  7. import java.sql.*;
  8. import java.util.logging.Level;
  9. import java.util.logging.Logger;
  10. import javax.swing.*;
  11.  
  12. public class TCPclient implements Runnable {
  13.  
  14.     private static final String UserName = "root";
  15.     private static final String Password = "";
  16.     private static final String ConnString = "jdbc:mysql://localhost:3306/football_db?zeroDateTimeBehavior=convertToNull";
  17.  
  18.     private Socket socket = null;
  19.     private Thread thread = null;
  20.     private DataInputStream console = null;
  21.     private DataOutputStream streamOut = null;
  22.     private ChatClientThread client = null;
  23.  
  24.     Connection conn = null;
  25.     boolean loginFlag = false;
  26.  
  27.     JFrame frameReg;
  28.  
  29.     JLabel labelLog1;//show msg from top
  30.     JLabel labelLog2;//shows "username"
  31.     JLabel labelLog3;//shows "password"
  32.     JLabel labelLog4;//shows msg to register
  33.     JTextField textLog1;//username textField
  34.     JTextField textLog2;//password textField
  35.     JButton buttonLog1;//login button
  36.     JButton buttonLog2;//register button
  37.     JPanel panelLog;//panel for user login
  38.  
  39.     JLabel labelReg1;//show msg from top
  40.     JLabel labelReg2;//First name
  41.     JLabel labelReg3;//Last name
  42.     JLabel labelReg4;//user name
  43.     JLabel labelReg5;//password
  44.     JTextField textReg1;//First name txt
  45.     JTextField textReg2;//Last name txt
  46.     JTextField textReg3;//user name txt
  47.     JTextField textReg4;//password txt
  48.     JButton buttonReg1;//register button
  49.     JPanel panelReg;//panel for register
  50.  
  51.     String userNameLog;
  52.     String passwordLog;
  53.     String registerLog = "register"; //login panel er register button
  54.     String firstNameReg;
  55.     String userNameReg;
  56.     String passwordReg;
  57.     String teamNameReg;
  58.     String registerReg = "registered"; //register panel er register button
  59.     String allRegInfo;
  60.  
  61.     public TCPclient(String serverName, int serverPort) throws SQLException {
  62.  
  63.         frameReg = new JFrame();
  64.         frameReg.setSize(500, 500);
  65.         frameReg.setLayout(null);
  66.  
  67.         //everything for login panel
  68.         panelLog = new JPanel();
  69.         panelLog.setBounds(0, 0, 0, 0);
  70.         panelLog.setLayout(null);
  71.         panelLog.setSize(500, 500);
  72.         panelLog.setBackground(Color.LIGHT_GRAY);
  73.  
  74.         labelLog1 = new JLabel();
  75.         labelLog1.setBounds(100, 20, 400, 50);
  76.         labelLog1.setFont(new Font("Reklame Script", Font.PLAIN, 20));
  77.         labelLog1.setText("Login if you have an existing account");
  78.  
  79.         labelLog2 = new JLabel();
  80.         labelLog2.setBounds(20, 95, 100, 50);
  81.         labelLog2.setFont(new Font("Reklame Script", Font.BOLD, 14));
  82.         labelLog2.setText("Username");
  83.         textLog1 = new JTextField();
  84.         textLog1.setBounds(100, 105, 300, 30);
  85.         textLog1.setFont(new Font("Reklame Script", Font.PLAIN, 14));
  86.  
  87.         labelLog3 = new JLabel();
  88.         labelLog3.setBounds(20, 165, 100, 50);
  89.         labelLog3.setFont(new Font("Reklame Script", Font.BOLD, 14));
  90.         labelLog3.setText("Password");
  91.         textLog2 = new JTextField();
  92.         textLog2.setBounds(100, 175, 300, 30);
  93.         textLog2.setFont(new Font("Reklame Script", Font.PLAIN, 14));
  94.  
  95.         buttonLog1 = new JButton();
  96.         buttonLog1.setText("Login");
  97.         buttonLog1.setBounds(150, 100, 130, 30);
  98.         buttonLog1.setLocation(190, 250);
  99.  
  100.         labelLog4 = new JLabel();
  101.         labelLog4.setBounds(15, 330, 400, 50);
  102.         labelLog4.setFont(new Font("Reklame Script", Font.BOLD, 14));
  103.         labelLog4.setText("If you do not have an account click-->>");
  104.  
  105.         buttonLog2 = new JButton();
  106.         buttonLog2.setText("Register");
  107.         buttonLog2.setBounds(150, 100, 130, 30);
  108.         buttonLog2.setLocation(330, 340);
  109.  
  110.         panelLog.add(labelLog1);
  111.         panelLog.add(labelLog2);
  112.         panelLog.add(labelLog3);
  113.         panelLog.add(labelLog4);
  114.         panelLog.add(textLog1);
  115.         panelLog.add(textLog2);
  116.         panelLog.add(buttonLog1);
  117.         panelLog.add(buttonLog2);
  118.         frameReg.add(panelLog);
  119.  
  120.         //everything for register panel.
  121.         panelReg = new JPanel();
  122.         panelReg.setBounds(0, 0, 0, 0);
  123.         panelReg.setLayout(null);
  124.         panelReg.setSize(500, 500);
  125.         panelReg.setBackground(Color.LIGHT_GRAY);
  126.  
  127.         labelReg1 = new JLabel();
  128.         labelReg1.setBounds(100, 20, 400, 50);
  129.         labelReg1.setFont(new Font("Reklame Script", Font.PLAIN, 20));
  130.         labelReg1.setText("Fill the form below to register");
  131.  
  132.         labelReg2 = new JLabel();
  133.         labelReg2.setBounds(20, 95, 100, 50);
  134.         labelReg2.setFont(new Font("Reklame Script", Font.BOLD, 14));
  135.         labelReg2.setText("First Name");
  136.         textReg1 = new JTextField();
  137.         textReg1.setBounds(120, 105, 300, 30);
  138.         textReg1.setFont(new Font("Reklame Script", Font.PLAIN, 14));
  139.  
  140.         labelReg3 = new JLabel();
  141.         labelReg3.setBounds(20, 165, 100, 50);
  142.         labelReg3.setFont(new Font("Reklame Script", Font.BOLD, 14));
  143.         labelReg3.setText("User Name");
  144.         textReg2 = new JTextField();
  145.         textReg2.setBounds(120, 175, 300, 30);
  146.         textReg2.setFont(new Font("Reklame Script", Font.PLAIN, 14));
  147.  
  148.         labelReg4 = new JLabel();
  149.         labelReg4.setBounds(20, 235, 100, 50);
  150.         labelReg4.setFont(new Font("Reklame Script", Font.BOLD, 14));
  151.         labelReg4.setText("Password");
  152.         textReg3 = new JTextField();
  153.         textReg3.setBounds(120, 245, 300, 30);
  154.         textReg3.setFont(new Font("Reklame Script", Font.PLAIN, 14));
  155.  
  156.         labelReg5 = new JLabel();
  157.         labelReg5.setBounds(20, 305, 100, 50);
  158.         labelReg5.setFont(new Font("Reklame Script", Font.BOLD, 14));
  159.         labelReg5.setText("Team Name");
  160.         textReg4 = new JTextField();
  161.         textReg4.setBounds(120, 315, 300, 30);
  162.         textReg4.setFont(new Font("Reklame Script", Font.PLAIN, 14));
  163.  
  164.         buttonReg1 = new JButton();
  165.         buttonReg1.setText("Register");
  166.         buttonReg1.setBounds(150, 100, 130, 30);
  167.         buttonReg1.setLocation(210, 370);
  168.  
  169.         panelReg.add(labelReg1);
  170.         panelReg.add(labelReg2);
  171.         panelReg.add(textReg1);
  172.         panelReg.add(labelReg3);
  173.         panelReg.add(textReg2);
  174.         panelReg.add(labelReg4);
  175.         panelReg.add(textReg3);
  176.         panelReg.add(labelReg5);
  177.         panelReg.add(textReg4);
  178.         panelReg.add(buttonReg1);
  179.  
  180.         frameReg.show();
  181.  
  182.         System.out.println("Establishing connection. Please wait ...");
  183.         try {
  184.             socket = new Socket(serverName, serverPort);
  185.             System.out.println("Connected: " + socket);
  186.             start();
  187.         } catch (UnknownHostException uhe) {
  188.             System.out.println("Host unknown: " + uhe.getMessage());
  189.         } catch (IOException ioe) {
  190.             System.out.println("Unexpected exception: " + ioe.getMessage());
  191.         }
  192.     }
  193.  
  194.     public void run() {
  195.         while (thread != null) {
  196.             try {
  197.                
  198.                 if(!loginFlag)
  199.                 {
  200.                     System.out.println("login flag true.");
  201.                     panelLog.setVisible(false);
  202.                 }
  203.  
  204.                 buttonLog1.addActionListener(new ActionListener() {
  205.  
  206.                     @Override
  207.                     public void actionPerformed(ActionEvent ae) {
  208.                         //buttonLog1.setBackground(Color.GRAY);
  209.  
  210.                         userNameLog = textLog1.getText();
  211.                         passwordLog = textLog2.getText();
  212.                         userNameLog += " " + passwordLog + " user";
  213.                         System.out.println("check krtesi userlog: " + userNameLog);
  214.  
  215.                         try {
  216.                             streamOut.writeUTF(userNameLog);
  217.                             streamOut.flush();
  218.  
  219.                         } catch (IOException ex) {
  220.                             Logger.getLogger(TCPclient.class.getName()).log(Level.SEVERE, null, ex);
  221.                             stop();//THIS STOP() IS VERy IMPORTATN X_X
  222.                         }
  223.                     }
  224.                 });
  225.  
  226.                 buttonLog2.addActionListener(new ActionListener() {
  227.  
  228.                     @Override
  229.                     public void actionPerformed(ActionEvent ae) {
  230.                         //buttonLog2.setBackground(Color.GRAY);
  231.                         //System.out.println("register button clicked.");
  232.                         panelLog.setVisible(false);
  233.                         frameReg.add(panelReg);
  234.  
  235.                         try {
  236.                             streamOut.writeUTF(registerLog);//register
  237.                             streamOut.flush();
  238.  
  239.                         } catch (IOException ex) {
  240.                             Logger.getLogger(TCPclient.class.getName()).log(Level.SEVERE, null, ex);
  241.                             stop();//THIS STOP() IS VERy IMPORTATN X_X
  242.                         }
  243.                     }
  244.                 });
  245.  
  246.                 buttonReg1.addActionListener(new ActionListener() {
  247.  
  248.                     @Override
  249.                     public void actionPerformed(ActionEvent ae) {
  250.                         //buttonReg1.setBackground(Color.GRAY);
  251.                         //System.out.println("register button clicked.");
  252.                         panelReg.setVisible(false);
  253.                         frameReg.add(panelLog);
  254.                         panelLog.setVisible(true);
  255.  
  256.                         firstNameReg = textReg1.getText();
  257.                         userNameReg = textReg2.getText();
  258.                         passwordReg = textReg3.getText();
  259.                         teamNameReg = textReg4.getText();
  260.                         allRegInfo += " " + firstNameReg + " " + userNameReg + " " + passwordReg + " " + teamNameReg + " registered";
  261.                         System.out.println("First name: " + firstNameReg + " User: " + userNameReg + " Password: " + passwordReg + " Team name: " + teamNameReg);
  262.  
  263.                         try {
  264.                             streamOut.writeUTF(allRegInfo);//registered
  265.                             streamOut.flush();
  266.  
  267.                         } catch (IOException ex) {
  268.                             Logger.getLogger(TCPclient.class.getName()).log(Level.SEVERE, null, ex);
  269.                             stop();//THIS STOP() IS VERy IMPORTATN X_X
  270.                         }
  271.                     }
  272.                 });
  273.  
  274.                 streamOut.writeUTF(console.readLine());
  275.                 streamOut.flush();
  276.             } catch (IOException ioe) {
  277.                 System.out.println("Sending error: " + ioe.getMessage());
  278.                 stop();
  279.             }
  280.         }
  281.     }
  282.  
  283.     public void handle(String msg) {
  284.         if (msg.equals("matched")) {
  285.             System.out.println("Server sent *" + msg + "*");
  286.         } else if (msg.endsWith("failed")) {
  287.             JOptionPane.showMessageDialog(panelLog, msg);
  288.             System.err.println("LogIn failed");
  289.         } else if (msg.endsWith("successful")) {
  290.             JOptionPane.showMessageDialog(panelLog, "Login successful. Welcome");
  291.             loginFlag = true;
  292.         } else if (msg.equals(".bye")) {
  293.             System.out.println("Good bye. Press RETURN to exit ...");
  294.             stop();
  295.         } else {
  296.             System.out.println(msg);
  297.         }
  298.     }
  299.  
  300.     public void start() throws IOException {
  301.         console = new DataInputStream(System.in);
  302.         streamOut = new DataOutputStream(socket.getOutputStream());
  303.         if (thread == null) {
  304.             client = new ChatClientThread(this, socket);
  305.             thread = new Thread(this);
  306.             thread.start();
  307.         }
  308.     }
  309.  
  310.     public void stop() {
  311.         if (thread != null) {
  312.             thread.stop();
  313.             thread = null;
  314.         }
  315.         try {
  316.             if (console != null) {
  317.                 console.close();
  318.             }
  319.             if (streamOut != null) {
  320.                 streamOut.close();
  321.             }
  322.             if (socket != null) {
  323.                 socket.close();
  324.             }
  325.         } catch (IOException ioe) {
  326.             System.out.println("Error closing ...");
  327.         }
  328.         client.close();
  329.         client.stop();
  330.     }
  331.  
  332.     public static void main(String args[]) throws SQLException, ClassNotFoundException {
  333.         TCPclient client = null;
  334.         client = new TCPclient("localhost", 2000);
  335.  
  336.         /*Connection conn = null;
  337.          try {
  338.          Class.forName("com.mysql.jdbc.Driver");
  339.          conn = (Connection) DriverManager.getConnection(ConnString, UserName, Password);
  340.          System.out.println("Connected");
  341.          Statement st = (Statement) conn.createStatement();
  342.          String insert = "INSERT INTO `team` (`team_id`, `team_name`, `gk`, `def`, `mid1`, `mid2`, `strike`) "
  343.                  + "VALUES ('1', 'alpha', 'a', 'f', 'a', 'cvb', 'b');";
  344.          st.executeUpdate(insert);
  345.          } catch (SQLException e) {
  346.          System.err.println(e);
  347.          }*/
  348.     }
  349.  
  350. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement