Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package panels;
- import java.awt.Font;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.net.UnknownHostException;
- import java.util.ArrayList;
- import java.util.LinkedHashMap;
- import java.util.Observable;
- import java.util.Observer;
- import java.util.Timer;
- import java.util.TimerTask;
- import javax.swing.DefaultComboBoxModel;
- import javax.swing.JButton;
- import javax.swing.JComboBox;
- import javax.swing.JLabel;
- import javax.swing.JOptionPane;
- import javax.swing.JPanel;
- import javax.swing.JTextField;
- import javax.swing.SwingConstants;
- import controller.Controller;
- import mainLoop.Connection;
- public class firstPanel extends JPanel implements Observer {
- private JTextField nameTxt, number, username, password, rePassword;
- private JButton startGame;
- private JComboBox day, month, year;
- private LinkedHashMap<String, String> user_info;
- private Controller controller;
- private Thread clientConnection;
- private Timer timerInfo;
- /**
- * Launch the application.
- */
- /**
- * Create the application.
- *
- * @param controller
- */
- public firstPanel(Controller controller) {
- this.controller = controller;
- initialize();
- }
- public firstPanel() {
- initialize();
- }
- /**
- * Initialize the contents of the frame.
- *
- * @throws UnknownHostException
- */
- private void initialize() {
- this.setBounds(29, 25, 802, 596);
- user_info = new LinkedHashMap<String, String>();
- this.setLayout(null);
- JLabel title = new JLabel("Sign Up");
- title.setHorizontalAlignment(SwingConstants.CENTER);
- title.setFont(new Font("Tahoma", Font.BOLD, 20));
- title.setBounds(289, 13, 190, 40);
- this.add(title);
- JLabel name = new JLabel("Name:");
- name.setFont(new Font("Tahoma", Font.PLAIN, 16));
- name.setBounds(61, 107, 121, 23);
- this.add(name);
- JLabel date = new JLabel("Birth of Date:");
- date.setFont(new Font("Tahoma", Font.PLAIN, 16));
- date.setBounds(61, 147, 121, 23);
- this.add(date);
- JLabel num = new JLabel("Mobile Number:");
- num.setFont(new Font("Tahoma", Font.PLAIN, 16));
- num.setBounds(61, 187, 121, 23);
- this.add(num);
- nameTxt = new JTextField();
- nameTxt.setBounds(211, 108, 171, 22);
- this.add(nameTxt);
- nameTxt.setColumns(10);
- number = new JTextField();
- number.setBounds(211, 188, 171, 22);
- this.add(number);
- number.setColumns(10);
- JLabel lblPersonalInformation = new JLabel("Personal Information");
- lblPersonalInformation.setFont(new Font("Tahoma", Font.BOLD, 16));
- lblPersonalInformation.setBounds(63, 64, 190, 30);
- this.add(lblPersonalInformation);
- JLabel lblUsername = new JLabel("Username:");
- lblUsername.setFont(new Font("Tahoma", Font.PLAIN, 16));
- lblUsername.setBounds(61, 282, 96, 22);
- this.add(lblUsername);
- JLabel name_4 = new JLabel("Password:");
- name_4.setFont(new Font("Tahoma", Font.PLAIN, 16));
- name_4.setBounds(61, 317, 96, 29);
- this.add(name_4);
- JLabel lblReenterPassword = new JLabel("Re-enter Password:");
- lblReenterPassword.setFont(new Font("Tahoma", Font.PLAIN, 16));
- lblReenterPassword.setBounds(61, 356, 145, 29);
- this.add(lblReenterPassword);
- username = new JTextField();
- username.setColumns(10);
- username.setBounds(211, 283, 171, 22);
- this.add(username);
- password = new JTextField();
- password.setColumns(10);
- password.setBounds(211, 321, 171, 22);
- this.add(password);
- rePassword = new JTextField();
- rePassword.setColumns(10);
- rePassword.setBounds(211, 360, 171, 22);
- this.add(rePassword);
- JLabel lblCreateYourAccount = new JLabel("Create Your Account");
- lblCreateYourAccount.setFont(new Font("Tahoma", Font.BOLD, 16));
- lblCreateYourAccount.setBounds(61, 243, 217, 30);
- this.add(lblCreateYourAccount);
- startGame = new JButton("Start Game");
- startGame.setBounds(572, 451, 136, 40);
- this.add(startGame);
- final DefaultComboBoxModel<Integer> days = new DefaultComboBoxModel<Integer>();
- for (int i = 1; i < 32; i++) {
- days.addElement(i);
- }
- day = new JComboBox(days);
- day.setBounds(263, 148, 50, 22);
- day.setSelectedIndex(-1);
- this.add(day);
- JLabel lblDay = new JLabel("Day");
- lblDay.setFont(new Font("Tahoma", Font.PLAIN, 14));
- lblDay.setBounds(211, 151, 50, 22);
- this.add(lblDay);
- JLabel lblMonth = new JLabel("Month");
- lblMonth.setFont(new Font("Tahoma", Font.PLAIN, 14));
- lblMonth.setBounds(338, 150, 70, 16);
- this.add(lblMonth);
- final DefaultComboBoxModel<Integer> months = new DefaultComboBoxModel<Integer>();
- for (int i = 1; i < 13; i++) {
- months.addElement(i);
- }
- month = new JComboBox(months);
- month.setBounds(406, 148, 40, 22);
- month.setSelectedIndex(-1);
- this.add(month);
- JLabel lblYear = new JLabel("Year");
- lblYear.setFont(new Font("Tahoma", Font.PLAIN, 14));
- lblYear.setBounds(464, 151, 56, 16);
- this.add(lblYear);
- final DefaultComboBoxModel<Integer> years = new DefaultComboBoxModel<Integer>();
- for (int i = 2017; i > 1989; i--) {
- years.addElement(i);
- }
- year = new JComboBox(years);
- year.setBounds(524, 148, 100, 22);
- year.setSelectedIndex(-1);
- this.add(year);
- startGame.addActionListener(new ActionHandler());
- }
- @Override
- public void update(Observable arg0, Object valueChanged) {
- LinkedHashMap<String, String> player2_data;
- if (valueChanged instanceof LinkedHashMap) {
- player2_data = (LinkedHashMap<String, String>) valueChanged;
- controller.changeScene(new PlayerInfo(player2_data));
- timerInfo = new Timer();
- timerInfo.schedule(new TimerTask() {
- @Override
- public void run() {
- controller.changeScene(new Game(player2_data.get("pw"), controller));
- }
- }, 3000);
- }
- }
- private class ActionHandler implements ActionListener {
- @Override
- public void actionPerformed(ActionEvent e) {
- if (e.getSource() == startGame) {
- boolean valid = true;
- int byear = 0, bmonth = 0, bday = 0;
- String udate, unumber, user, pw;
- user_info.put("name", nameTxt.getText());
- if (year.getSelectedIndex() != -1)
- byear = (int) year.getItemAt(year.getSelectedIndex());
- else
- valid = false;
- if (month.getSelectedIndex() != -1)
- bmonth = (int) month.getItemAt(month.getSelectedIndex());
- else
- valid = false;
- if (day.getSelectedIndex() != -1)
- bday = (int) day.getItemAt(month.getSelectedIndex());
- else
- valid = false;
- if (valid) {
- udate = new String(
- Integer.toString(bday) + "-" + Integer.toString(bmonth) + "-" + Integer.toString(byear));
- unumber = number.getText();
- user_info.put("date", udate);
- user_info.put("num", unumber);
- if (validatePw(password.getText(), rePassword.getText())) {
- user_info.put("pw", password.getText());
- } else {
- valid = false;
- }
- }
- if (!valid) {
- JOptionPane.showMessageDialog(new JLabel("Warning"), "Please Refill the form");
- } else {
- startConnection();
- }
- }
- }
- private boolean validatePw(String pw, String pw2) {
- boolean valid = true;
- if (!pw.equals(pw2))
- return false;
- if (pw.length() != 4)
- return false;
- for (int i = 0; i < pw.length(); i++) {
- valid = checkExist(pw.charAt(i));
- if (!valid)
- return valid;
- }
- return valid;
- }
- private boolean checkExist(char c) {
- if (user_info.get("name").contains(String.valueOf(c)))
- return true;
- if (user_info.get("date").contains(String.valueOf(c)))
- return true;
- if (user_info.get("num").contains(String.valueOf(c)))
- return true;
- return false;
- }
- private void startConnection() {
- controller.changeScene(new WaitPanel());
- clientConnection = new Thread(new Connection(firstPanel.this, user_info));
- clientConnection.start();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement