Advertisement
Guest User

Untitled

a guest
Feb 16th, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.74 KB | None | 0 0
  1. package panels;
  2.  
  3. import java.awt.Font;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import java.net.UnknownHostException;
  7. import java.util.ArrayList;
  8. import java.util.LinkedHashMap;
  9. import java.util.Observable;
  10. import java.util.Observer;
  11. import java.util.Timer;
  12. import java.util.TimerTask;
  13.  
  14. import javax.swing.DefaultComboBoxModel;
  15. import javax.swing.JButton;
  16. import javax.swing.JComboBox;
  17. import javax.swing.JLabel;
  18. import javax.swing.JOptionPane;
  19. import javax.swing.JPanel;
  20. import javax.swing.JTextField;
  21. import javax.swing.SwingConstants;
  22.  
  23. import controller.Controller;
  24. import mainLoop.Connection;
  25.  
  26. public class firstPanel extends JPanel implements Observer {
  27.  
  28. private JTextField nameTxt, number, username, password, rePassword;
  29. private JButton startGame;
  30. private JComboBox day, month, year;
  31. private LinkedHashMap<String, String> user_info;
  32. private Controller controller;
  33. private Thread clientConnection;
  34. private Timer timerInfo;
  35. /**
  36. * Launch the application.
  37. */
  38.  
  39. /**
  40. * Create the application.
  41. *
  42. * @param controller
  43. */
  44. public firstPanel(Controller controller) {
  45. this.controller = controller;
  46. initialize();
  47. }
  48.  
  49. public firstPanel() {
  50. initialize();
  51. }
  52.  
  53. /**
  54. * Initialize the contents of the frame.
  55. *
  56. * @throws UnknownHostException
  57. */
  58. private void initialize() {
  59.  
  60. this.setBounds(29, 25, 802, 596);
  61. user_info = new LinkedHashMap<String, String>();
  62.  
  63. this.setLayout(null);
  64. JLabel title = new JLabel("Sign Up");
  65. title.setHorizontalAlignment(SwingConstants.CENTER);
  66. title.setFont(new Font("Tahoma", Font.BOLD, 20));
  67. title.setBounds(289, 13, 190, 40);
  68. this.add(title);
  69.  
  70. JLabel name = new JLabel("Name:");
  71. name.setFont(new Font("Tahoma", Font.PLAIN, 16));
  72. name.setBounds(61, 107, 121, 23);
  73. this.add(name);
  74.  
  75. JLabel date = new JLabel("Birth of Date:");
  76. date.setFont(new Font("Tahoma", Font.PLAIN, 16));
  77. date.setBounds(61, 147, 121, 23);
  78. this.add(date);
  79.  
  80. JLabel num = new JLabel("Mobile Number:");
  81. num.setFont(new Font("Tahoma", Font.PLAIN, 16));
  82. num.setBounds(61, 187, 121, 23);
  83. this.add(num);
  84.  
  85. nameTxt = new JTextField();
  86. nameTxt.setBounds(211, 108, 171, 22);
  87. this.add(nameTxt);
  88. nameTxt.setColumns(10);
  89.  
  90. number = new JTextField();
  91. number.setBounds(211, 188, 171, 22);
  92. this.add(number);
  93. number.setColumns(10);
  94.  
  95. JLabel lblPersonalInformation = new JLabel("Personal Information");
  96. lblPersonalInformation.setFont(new Font("Tahoma", Font.BOLD, 16));
  97. lblPersonalInformation.setBounds(63, 64, 190, 30);
  98. this.add(lblPersonalInformation);
  99.  
  100. JLabel lblUsername = new JLabel("Username:");
  101. lblUsername.setFont(new Font("Tahoma", Font.PLAIN, 16));
  102. lblUsername.setBounds(61, 282, 96, 22);
  103. this.add(lblUsername);
  104.  
  105. JLabel name_4 = new JLabel("Password:");
  106. name_4.setFont(new Font("Tahoma", Font.PLAIN, 16));
  107. name_4.setBounds(61, 317, 96, 29);
  108. this.add(name_4);
  109.  
  110. JLabel lblReenterPassword = new JLabel("Re-enter Password:");
  111. lblReenterPassword.setFont(new Font("Tahoma", Font.PLAIN, 16));
  112. lblReenterPassword.setBounds(61, 356, 145, 29);
  113. this.add(lblReenterPassword);
  114.  
  115. username = new JTextField();
  116. username.setColumns(10);
  117. username.setBounds(211, 283, 171, 22);
  118. this.add(username);
  119.  
  120. password = new JTextField();
  121. password.setColumns(10);
  122. password.setBounds(211, 321, 171, 22);
  123. this.add(password);
  124.  
  125. rePassword = new JTextField();
  126. rePassword.setColumns(10);
  127. rePassword.setBounds(211, 360, 171, 22);
  128. this.add(rePassword);
  129.  
  130. JLabel lblCreateYourAccount = new JLabel("Create Your Account");
  131. lblCreateYourAccount.setFont(new Font("Tahoma", Font.BOLD, 16));
  132. lblCreateYourAccount.setBounds(61, 243, 217, 30);
  133. this.add(lblCreateYourAccount);
  134.  
  135. startGame = new JButton("Start Game");
  136. startGame.setBounds(572, 451, 136, 40);
  137. this.add(startGame);
  138.  
  139. final DefaultComboBoxModel<Integer> days = new DefaultComboBoxModel<Integer>();
  140. for (int i = 1; i < 32; i++) {
  141. days.addElement(i);
  142. }
  143.  
  144. day = new JComboBox(days);
  145. day.setBounds(263, 148, 50, 22);
  146. day.setSelectedIndex(-1);
  147. this.add(day);
  148.  
  149. JLabel lblDay = new JLabel("Day");
  150. lblDay.setFont(new Font("Tahoma", Font.PLAIN, 14));
  151. lblDay.setBounds(211, 151, 50, 22);
  152. this.add(lblDay);
  153.  
  154. JLabel lblMonth = new JLabel("Month");
  155. lblMonth.setFont(new Font("Tahoma", Font.PLAIN, 14));
  156. lblMonth.setBounds(338, 150, 70, 16);
  157.  
  158. this.add(lblMonth);
  159.  
  160. final DefaultComboBoxModel<Integer> months = new DefaultComboBoxModel<Integer>();
  161. for (int i = 1; i < 13; i++) {
  162. months.addElement(i);
  163. }
  164.  
  165. month = new JComboBox(months);
  166. month.setBounds(406, 148, 40, 22);
  167. month.setSelectedIndex(-1);
  168. this.add(month);
  169.  
  170. JLabel lblYear = new JLabel("Year");
  171. lblYear.setFont(new Font("Tahoma", Font.PLAIN, 14));
  172. lblYear.setBounds(464, 151, 56, 16);
  173. this.add(lblYear);
  174.  
  175. final DefaultComboBoxModel<Integer> years = new DefaultComboBoxModel<Integer>();
  176. for (int i = 2017; i > 1989; i--) {
  177. years.addElement(i);
  178. }
  179. year = new JComboBox(years);
  180. year.setBounds(524, 148, 100, 22);
  181. year.setSelectedIndex(-1);
  182. this.add(year);
  183.  
  184. startGame.addActionListener(new ActionHandler());
  185. }
  186.  
  187. @Override
  188. public void update(Observable arg0, Object valueChanged) {
  189.  
  190. LinkedHashMap<String, String> player2_data;
  191. if (valueChanged instanceof LinkedHashMap) {
  192. player2_data = (LinkedHashMap<String, String>) valueChanged;
  193. controller.changeScene(new PlayerInfo(player2_data));
  194. timerInfo = new Timer();
  195. timerInfo.schedule(new TimerTask() {
  196. @Override
  197. public void run() {
  198. controller.changeScene(new Game(player2_data.get("pw"), controller));
  199. }
  200.  
  201. }, 3000);
  202. }
  203.  
  204. }
  205.  
  206. private class ActionHandler implements ActionListener {
  207.  
  208. @Override
  209. public void actionPerformed(ActionEvent e) {
  210. if (e.getSource() == startGame) {
  211. boolean valid = true;
  212. int byear = 0, bmonth = 0, bday = 0;
  213. String udate, unumber, user, pw;
  214. user_info.put("name", nameTxt.getText());
  215. if (year.getSelectedIndex() != -1)
  216. byear = (int) year.getItemAt(year.getSelectedIndex());
  217. else
  218. valid = false;
  219. if (month.getSelectedIndex() != -1)
  220. bmonth = (int) month.getItemAt(month.getSelectedIndex());
  221. else
  222. valid = false;
  223. if (day.getSelectedIndex() != -1)
  224. bday = (int) day.getItemAt(month.getSelectedIndex());
  225. else
  226. valid = false;
  227.  
  228. if (valid) {
  229.  
  230. udate = new String(
  231. Integer.toString(bday) + "-" + Integer.toString(bmonth) + "-" + Integer.toString(byear));
  232. unumber = number.getText();
  233. user_info.put("date", udate);
  234. user_info.put("num", unumber);
  235. if (validatePw(password.getText(), rePassword.getText())) {
  236. user_info.put("pw", password.getText());
  237. } else {
  238. valid = false;
  239. }
  240. }
  241. if (!valid) {
  242. JOptionPane.showMessageDialog(new JLabel("Warning"), "Please Refill the form");
  243. } else {
  244. startConnection();
  245. }
  246. }
  247.  
  248. }
  249.  
  250. private boolean validatePw(String pw, String pw2) {
  251. boolean valid = true;
  252. if (!pw.equals(pw2))
  253. return false;
  254. if (pw.length() != 4)
  255. return false;
  256. for (int i = 0; i < pw.length(); i++) {
  257. valid = checkExist(pw.charAt(i));
  258. if (!valid)
  259. return valid;
  260. }
  261. return valid;
  262. }
  263.  
  264. private boolean checkExist(char c) {
  265. if (user_info.get("name").contains(String.valueOf(c)))
  266. return true;
  267. if (user_info.get("date").contains(String.valueOf(c)))
  268. return true;
  269. if (user_info.get("num").contains(String.valueOf(c)))
  270. return true;
  271. return false;
  272. }
  273.  
  274. private void startConnection() {
  275. controller.changeScene(new WaitPanel());
  276. clientConnection = new Thread(new Connection(firstPanel.this, user_info));
  277. clientConnection.start();
  278.  
  279. }
  280. }
  281. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement