Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package tcpclient;
- import java.awt.*;
- import java.awt.event.*;
- import java.net.*;
- import java.io.*;
- import java.sql.*;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import javax.swing.*;
- import javax.swing.JOptionPane;
- public class TCPclient implements Runnable {
- private static final String UserName = "root";
- private static final String Password = "";
- private static final String ConnString = "jdbc:mysql://localhost:3306/football_db?zeroDateTimeBehavior=convertToNull";
- private Socket socket = null;
- private Thread thread = null;
- private DataInputStream console = null;
- private DataOutputStream streamOut = null;
- private ChatClientThread client = null;
- Connection conn = null;
- boolean loginFlag = false;
- boolean teamChose = false;
- JFrame frameReg;
- JFrame framePlay;
- JLabel labelLog1;//show msg from top
- JLabel labelLog2;//shows "username"
- JLabel labelLog3;//shows "password"
- JLabel labelLog4;//shows msg to register
- JTextField textLog1;//username textField
- JTextField textLog2;//password textField
- JButton buttonLog1;//login button
- JButton buttonLog2;//register button
- JPanel panelLog;//panel for user login
- JLabel labelReg1;//show msg from top
- JLabel labelReg2;//First name
- JLabel labelReg3;//Last name
- JLabel labelReg4;//user name
- JLabel labelReg5;//password
- JTextField textReg1;//First name txt
- JTextField textReg2;//Last name txt
- JTextField textReg3;//user name txt
- JTextField textReg4;//password txt
- JButton buttonReg1;//register button
- JPanel panelReg;//panel for register
- JLabel labelChTm1;//show GK
- JLabel labelChTm2;//show def
- JLabel labelChTm3;//show mid1
- JLabel labelChTm4;//show mid2
- JLabel labelChTm5;//show strike
- JLabel labelChTm6;//show team
- JButton buttonChTm1;//Button team 1
- JButton buttonChTm2;//Button team 2
- JButton buttonChTm3;//Button team 3
- JButton buttonChTm4;//Button team 3
- JButton buttonChTm5;//Button team 4
- JPanel panelChTm;//Panel for team choose
- JLabel labelPlay1;
- JPanel panelPlay;
- String userNameLog;
- String passwordLog;
- String registerLog = "register"; //login panel er register button
- String firstNameReg;
- String userNameReg;
- String passwordReg;
- String teamNameReg;
- String registerReg = "registered"; //register panel er register button
- String allRegInfo;
- String teamChoose = "team";
- public TCPclient(String serverName, int serverPort) throws SQLException {
- frameReg = new JFrame();
- frameReg.setSize(600, 600);
- frameReg.setLayout(null);
- framePlay = new JFrame();
- framePlay.setSize(600, 600);
- framePlay.setLayout(null);
- //everything for login panel
- panelLog = new JPanel();
- panelLog.setBounds(0, 0, 0, 0);
- panelLog.setLayout(null);
- panelLog.setSize(600, 600);
- panelLog.setBackground(Color.LIGHT_GRAY);
- labelLog1 = new JLabel();
- labelLog1.setBounds(80, 20, 400, 50);
- labelLog1.setFont(new Font("Reklame Script", Font.PLAIN, 20));
- labelLog1.setText("Login if you have an existing account");
- labelLog2 = new JLabel();
- labelLog2.setBounds(20, 95, 100, 50);
- labelLog2.setFont(new Font("Reklame Script", Font.BOLD, 14));
- labelLog2.setText("Username");
- textLog1 = new JTextField();
- textLog1.setBounds(100, 105, 300, 30);
- textLog1.setFont(new Font("Reklame Script", Font.PLAIN, 14));
- labelLog3 = new JLabel();
- labelLog3.setBounds(20, 165, 100, 50);
- labelLog3.setFont(new Font("Reklame Script", Font.BOLD, 14));
- labelLog3.setText("Password");
- textLog2 = new JTextField();
- textLog2.setBounds(100, 175, 300, 30);
- textLog2.setFont(new Font("Reklame Script", Font.PLAIN, 14));
- buttonLog1 = new JButton();
- buttonLog1.setText("Login");
- buttonLog1.setBounds(150, 100, 130, 30);
- buttonLog1.setLocation(190, 250);
- labelLog4 = new JLabel();
- labelLog4.setBounds(15, 330, 400, 50);
- labelLog4.setFont(new Font("Reklame Script", Font.BOLD, 14));
- labelLog4.setText("If you do not have an account click-->>");
- buttonLog2 = new JButton();
- buttonLog2.setText("Register");
- buttonLog2.setBounds(150, 100, 130, 30);
- buttonLog2.setLocation(330, 340);
- panelLog.add(labelLog1);
- panelLog.add(labelLog2);
- panelLog.add(labelLog3);
- panelLog.add(labelLog4);
- panelLog.add(textLog1);
- panelLog.add(textLog2);
- panelLog.add(buttonLog1);
- panelLog.add(buttonLog2);
- frameReg.add(panelLog); //1st ai show krbe login panel.
- //everything for register panel.
- panelReg = new JPanel();
- panelReg.setBounds(0, 0, 0, 0);
- panelReg.setLayout(null);
- panelReg.setSize(600, 600);
- panelReg.setBackground(Color.LIGHT_GRAY);
- labelReg1 = new JLabel();
- labelReg1.setBounds(100, 20, 400, 50);
- labelReg1.setFont(new Font("Reklame Script", Font.PLAIN, 20));
- labelReg1.setText("Fill the form below to register");
- labelReg2 = new JLabel();
- labelReg2.setBounds(20, 95, 100, 50);
- labelReg2.setFont(new Font("Reklame Script", Font.BOLD, 14));
- labelReg2.setText("First Name");
- textReg1 = new JTextField();
- textReg1.setBounds(120, 105, 300, 30);
- textReg1.setFont(new Font("Reklame Script", Font.PLAIN, 14));
- labelReg3 = new JLabel();
- labelReg3.setBounds(20, 165, 100, 50);
- labelReg3.setFont(new Font("Reklame Script", Font.BOLD, 14));
- labelReg3.setText("User Name");
- textReg2 = new JTextField();
- textReg2.setBounds(120, 175, 300, 30);
- textReg2.setFont(new Font("Reklame Script", Font.PLAIN, 14));
- labelReg4 = new JLabel();
- labelReg4.setBounds(20, 235, 100, 50);
- labelReg4.setFont(new Font("Reklame Script", Font.BOLD, 14));
- labelReg4.setText("Password");
- textReg3 = new JTextField();
- textReg3.setBounds(120, 245, 300, 30);
- textReg3.setFont(new Font("Reklame Script", Font.PLAIN, 14));
- buttonReg1 = new JButton();
- buttonReg1.setText("Register");
- buttonReg1.setBounds(150, 100, 130, 30);
- buttonReg1.setLocation(210, 370);
- panelReg.add(labelReg1);
- panelReg.add(labelReg2);
- panelReg.add(textReg1);
- panelReg.add(labelReg3);
- panelReg.add(textReg2);
- panelReg.add(labelReg4);
- panelReg.add(textReg3);
- panelReg.add(buttonReg1);
- //everything for chooseteam panel
- panelChTm = new JPanel();
- panelChTm.setBounds(0, 0, 0, 0);
- panelChTm.setLayout(null);
- panelChTm.setSize(600, 600);
- panelChTm.setBackground(Color.LIGHT_GRAY);
- labelChTm6 = new JLabel();
- labelChTm6.setBounds(20, 50, 600, 50);
- labelChTm6.setFont(new Font("Reklame Script", Font.BOLD, 12));
- labelChTm6.setText("Team: Alpha Team: Beta Team: Charlie Team: Delta Team: Echo");
- labelChTm1 = new JLabel();
- labelChTm1.setBounds(20, 90, 400, 50);
- labelChTm1.setFont(new Font("Reklame Script", Font.BOLD, 10));
- labelChTm1.setText("GK: MANUEK NEUER GK: PETR CECH GK: IKER CASILLAS");
- labelChTm2 = new JLabel();
- labelChTm2.setBounds(20, 120, 400, 50);
- labelChTm2.setFont(new Font("Reklame Script", Font.BOLD, 10));
- labelChTm2.setText("DEF: GK: GK:");
- labelChTm3 = new JLabel();
- labelChTm3.setBounds(20, 150, 400, 50);
- labelChTm3.setFont(new Font("Reklame Script", Font.BOLD, 14));
- labelChTm3.setText("MID1: GK: GK:");
- labelChTm4 = new JLabel();
- labelChTm4.setBounds(20, 180, 400, 50);
- labelChTm4.setFont(new Font("Reklame Script", Font.BOLD, 14));
- labelChTm4.setText("MID2: GK: GK:");
- labelChTm5 = new JLabel();
- labelChTm5.setBounds(20, 210, 400, 50);
- labelChTm5.setFont(new Font("Reklame Script", Font.BOLD, 14));
- labelChTm5.setText("STRIKE: GK: GK:");
- buttonChTm1 = new JButton();
- buttonChTm1.setText("ALPHA");
- buttonChTm1.setBounds(150, 100, 90, 30);
- buttonChTm1.setLocation(20, 250);
- buttonChTm2 = new JButton();
- buttonChTm2.setText("BETA");
- buttonChTm2.setBounds(150, 100, 90, 30);
- buttonChTm2.setLocation(130, 250);
- buttonChTm3 = new JButton();
- buttonChTm3.setText("CHARLIE");
- buttonChTm3.setBounds(150, 100, 90, 30);
- buttonChTm3.setLocation(220, 250);
- buttonChTm4 = new JButton();
- buttonChTm4.setText("DELTA");
- buttonChTm4.setBounds(150, 100, 90, 30);
- buttonChTm4.setLocation(300, 250);
- buttonChTm5 = new JButton();
- buttonChTm5.setText("ECHO");
- buttonChTm5.setBounds(150, 100, 90, 30);
- buttonChTm5.setLocation(400, 250);
- panelChTm.add(labelChTm1);
- panelChTm.add(labelChTm2);
- panelChTm.add(labelChTm3);
- panelChTm.add(labelChTm4);
- panelChTm.add(labelChTm5);
- panelChTm.add(labelChTm6);
- panelChTm.add(buttonChTm1);
- panelChTm.add(buttonChTm2);
- panelChTm.add(buttonChTm3);
- panelChTm.add(buttonChTm4);
- panelChTm.add(buttonChTm5);
- //Everything for play panel
- panelPlay = new JPanel();
- panelPlay.setBounds(0, 0, 0, 0);
- panelPlay.setLayout(null);
- panelPlay.setSize(600, 600);
- panelPlay.setBackground(Color.LIGHT_GRAY);
- labelPlay1 = new JLabel();
- labelPlay1.setBounds(80, 20, 400, 50);
- labelPlay1.setFont(new Font("Reklame Script", Font.PLAIN, 20));
- labelPlay1.setText("Login if you have an existing account");
- panelPlay.add(labelPlay1);
- panelPlay.setVisible(false);
- frameReg.add(panelPlay);
- //framePlay.add(panelPlay);
- frameReg.show();
- System.out.println("Establishing connection. Please wait ...");
- try {
- socket = new Socket(serverName, serverPort);
- System.out.println("Connected: " + socket);
- start();
- } catch (UnknownHostException uhe) {
- System.out.println("Host unknown: " + uhe.getMessage());
- } catch (IOException ioe) {
- System.out.println("Unexpected exception: " + ioe.getMessage());
- }
- }
- public void run() {
- while (thread != null) {
- try {
- if (!loginFlag && teamChose) {
- System.out.println("loginFlag false && teamChose true");
- //panelChTm.setVisible(false);
- //frameReg.setVisible(false);
- //panelPlay.add(labelPlay1);
- //framePlay.add(panelPlay);
- //frameReg.show();
- //teamChose = false;
- panelChTm.setVisible(false);
- frameReg.add(panelPlay);
- System.out.println("last e kaj hche");
- JOptionPane.showMessageDialog(panelLog, "LALALALLALALA");
- } else if (loginFlag) {
- System.out.println("login flag true.");
- panelLog.setVisible(false);
- frameReg.add(panelChTm);
- buttonChTm1.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent ae) {
- teamChoose += " alpha";
- //System.out.println("team choose ta ai khne print krtesi"+teamChoose);
- try {
- streamOut.writeUTF(teamChoose);
- streamOut.flush();
- } catch (IOException ex) {
- Logger.getLogger(TCPclient.class.getName()).log(Level.SEVERE, null, ex);
- stop();//THIS STOP() IS VERy IMPORTATN X_X
- }
- teamChose = true;
- loginFlag = false;
- //panelChTm.setVisible(false);
- //frameReg.add(panelPlay);
- //run();
- }
- });
- buttonChTm2.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent ae) {
- teamChoose += " beta";
- try {
- streamOut.writeUTF(teamChoose);
- streamOut.flush();
- } catch (IOException ex) {
- Logger.getLogger(TCPclient.class.getName()).log(Level.SEVERE, null, ex);
- stop();//THIS STOP() IS VERy IMPORTATN X_X
- }
- teamChose = true;
- loginFlag = false;
- run();
- }
- });
- buttonChTm3.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent ae) {
- teamChoose += " charlie";
- try {
- streamOut.writeUTF(teamChoose);
- streamOut.flush();
- } catch (IOException ex) {
- Logger.getLogger(TCPclient.class.getName()).log(Level.SEVERE, null, ex);
- stop();//THIS STOP() IS VERy IMPORTATN X_X
- }
- teamChose = true;
- loginFlag = false;
- run();
- }
- });
- buttonChTm4.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent ae) {
- teamChoose += " delta";
- try {
- streamOut.writeUTF(teamChoose);
- streamOut.flush();
- } catch (IOException ex) {
- Logger.getLogger(TCPclient.class.getName()).log(Level.SEVERE, null, ex);
- stop();//THIS STOP() IS VERy IMPORTATN X_X
- }
- teamChose = true;
- loginFlag = false;
- run();
- }
- });
- buttonChTm5.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent ae) {
- teamChoose += " echo";
- try {
- streamOut.writeUTF(teamChoose);
- streamOut.flush();
- } catch (IOException ex) {
- Logger.getLogger(TCPclient.class.getName()).log(Level.SEVERE, null, ex);
- stop();//THIS STOP() IS VERy IMPORTATN X_X
- }
- teamChose = true;
- loginFlag = false;
- run();
- }
- });
- } else {
- buttonLog1.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent ae) {
- //buttonLog1.setBackground(Color.GRAY);
- userNameLog = textLog1.getText();
- passwordLog = textLog2.getText();
- userNameLog += " " + passwordLog + " user";
- System.out.println("check krtesi userlog: " + userNameLog);
- try {
- streamOut.writeUTF(userNameLog);
- streamOut.flush();
- } catch (IOException ex) {
- Logger.getLogger(TCPclient.class.getName()).log(Level.SEVERE, null, ex);
- stop();//THIS STOP() IS VERy IMPORTATN X_X
- }
- }
- });
- buttonLog2.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent ae) {
- //buttonLog2.setBackground(Color.GRAY);
- //System.out.println("register button clicked.");
- panelLog.setVisible(false);
- frameReg.add(panelReg);
- try {
- streamOut.writeUTF(registerLog);//register
- streamOut.flush();
- } catch (IOException ex) {
- Logger.getLogger(TCPclient.class.getName()).log(Level.SEVERE, null, ex);
- stop();//THIS STOP() IS VERy IMPORTATN X_X
- }
- }
- });
- buttonReg1.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent ae) {
- //buttonReg1.setBackground(Color.GRAY);
- //System.out.println("register button clicked.");
- panelReg.setVisible(false);
- frameReg.add(panelLog);
- panelLog.setVisible(true);
- firstNameReg = textReg1.getText();
- userNameReg = textReg2.getText();
- passwordReg = textReg3.getText();
- //teamNameReg = textReg4.getText();
- allRegInfo += " " + firstNameReg + " " + userNameReg + " " + passwordReg + " registered";
- System.out.println("First name: " + firstNameReg + " User: " + userNameReg + " Password: " + passwordReg);
- try {
- streamOut.writeUTF(allRegInfo);//registered
- streamOut.flush();
- } catch (IOException ex) {
- Logger.getLogger(TCPclient.class.getName()).log(Level.SEVERE, null, ex);
- stop();//THIS STOP() IS VERy IMPORTATN X_X
- }
- }
- });
- }
- streamOut.writeUTF(console.readLine());
- streamOut.flush();
- } catch (IOException ioe) {
- System.out.println("Sending error: " + ioe.getMessage());
- stop();
- }
- }
- }
- public void handle(String msg) {
- System.out.println("Prottek bar check krtesi msg: " + msg);
- if (msg.equals("matched")) {
- System.out.println("Server sent *" + msg + "*");
- } else if (msg.endsWith("failed")) {
- JOptionPane.showMessageDialog(panelLog, msg);
- System.err.println("LogIn failed");
- } else if (msg.endsWith("successful")) {
- //JOptionPane.showMessageDialog(panelLog, "Login successful. Welcome");
- int input = JOptionPane.showOptionDialog(null, "Login Successful", "The title", JOptionPane.OK_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE, null, null, null);
- if (input == JOptionPane.OK_OPTION) {
- panelChTm.setVisible(false);
- panelLog.setVisible(false);
- panelPlay.setVisible(true);
- System.out.println(">>>>>>ok hche");
- }
- loginFlag = true;
- System.out.println(">>>>>>>>>> client er handle kaj krtese");
- //run();
- } else if (msg.equals("check")) {
- JOptionPane.showMessageDialog(panelLog, "Checking this ");
- } else if (msg.equals(".bye")) {
- System.out.println("Good bye. Press RETURN to exit ...");
- stop();
- } else {
- System.out.println("else activate hoise... msg: " + msg);
- }
- }
- public void start() throws IOException {
- console = new DataInputStream(System.in);
- streamOut = new DataOutputStream(socket.getOutputStream());
- if (thread == null) {
- client = new ChatClientThread(this, socket);
- thread = new Thread(this);
- thread.start();
- }
- }
- public void stop() {
- if (thread != null) {
- thread.stop();
- thread = null;
- }
- try {
- if (console != null) {
- console.close();
- }
- if (streamOut != null) {
- streamOut.close();
- }
- if (socket != null) {
- socket.close();
- }
- } catch (IOException ioe) {
- System.out.println("Error closing ...");
- }
- client.close();
- client.stop();
- }
- public static void main(String args[]) throws SQLException, ClassNotFoundException {
- TCPclient client = null;
- client = new TCPclient("localhost", 2000);
- /*Connection conn = null;
- try {
- Class.forName("com.mysql.jdbc.Driver");
- conn = (Connection) DriverManager.getConnection(ConnString, UserName, Password);
- System.out.println("Connected");
- Statement st = (Statement) conn.createStatement();
- String insert = "INSERT INTO `team` (`team_id`, `team_name`, `gk`, `def`, `mid1`, `mid2`, `strike`) "
- + "VALUES ('1', 'alpha', 'a', 'f', 'a', 'cvb', 'b');";
- st.executeUpdate(insert);
- } catch (SQLException e) {
- System.err.println(e);
- }*/
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement