Advertisement
Guest User

Untitled

a guest
Apr 27th, 2017
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.42 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package Client;
  7.  
  8. import java.io.IOException;
  9. import java.net.InetSocketAddress;
  10. import java.nio.ByteBuffer;
  11. import java.nio.channels.SocketChannel;
  12. import java.util.logging.Level;
  13. import java.util.logging.Logger;
  14. import javax.swing.JOptionPane;
  15.  
  16. /**
  17.  *
  18.  * @author osm70
  19.  */
  20. public class RegisterWindow extends javax.swing.JFrame {
  21.  
  22.     /**
  23.      * Creates new form RegisterWindow
  24.      */
  25.     public RegisterWindow() {
  26.         initComponents();
  27.     }
  28.  
  29.     /**
  30.      * This method is called from within the constructor to initialize the form.
  31.      * WARNING: Do NOT modify this code. The content of this method is always
  32.      * regenerated by the Form Editor.
  33.      */
  34.     @SuppressWarnings("unchecked")
  35.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  36.     private void initComponents() {
  37.  
  38.         jLabel1 = new javax.swing.JLabel();
  39.         jLabel2 = new javax.swing.JLabel();
  40.         jLabel3 = new javax.swing.JLabel();
  41.         name = new javax.swing.JTextField();
  42.         password = new javax.swing.JPasswordField();
  43.         repeat = new javax.swing.JPasswordField();
  44.         jButton1 = new javax.swing.JButton();
  45.         jButton2 = new javax.swing.JButton();
  46.  
  47.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  48.  
  49.         jLabel1.setText("Name:");
  50.  
  51.         jLabel2.setText("Password:");
  52.  
  53.         jLabel3.setText("Password:");
  54.  
  55.         jButton1.setText("Register");
  56.         jButton1.setToolTipText("");
  57.         jButton1.addActionListener(new java.awt.event.ActionListener() {
  58.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  59.                 jButton1ActionPerformed(evt);
  60.             }
  61.         });
  62.  
  63.         jButton2.setText("back");
  64.         jButton2.addActionListener(new java.awt.event.ActionListener() {
  65.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  66.                 jButton2ActionPerformed(evt);
  67.             }
  68.         });
  69.  
  70.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  71.         getContentPane().setLayout(layout);
  72.         layout.setHorizontalGroup(
  73.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  74.             .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
  75.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
  76.                     .addGroup(layout.createSequentialGroup()
  77.                         .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  78.                         .addComponent(jButton1))
  79.                     .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
  80.                         .addGap(29, 29, 29)
  81.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  82.                             .addGroup(layout.createSequentialGroup()
  83.                                 .addComponent(jLabel3)
  84.                                 .addGap(18, 18, 18)
  85.                                 .addComponent(repeat))
  86.                             .addGroup(layout.createSequentialGroup()
  87.                                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  88.                                     .addComponent(jButton2)
  89.                                     .addGroup(layout.createSequentialGroup()
  90.                                         .addComponent(jLabel1)
  91.                                         .addGap(31, 31, 31)
  92.                                         .addComponent(name, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE))
  93.                                     .addGroup(layout.createSequentialGroup()
  94.                                         .addComponent(jLabel2)
  95.                                         .addGap(18, 18, 18)
  96.                                         .addComponent(password, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)))
  97.                                 .addGap(0, 0, Short.MAX_VALUE)))))
  98.                 .addGap(223, 223, 223))
  99.         );
  100.         layout.setVerticalGroup(
  101.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  102.             .addGroup(layout.createSequentialGroup()
  103.                 .addGap(28, 28, 28)
  104.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  105.                     .addComponent(jLabel1)
  106.                     .addComponent(name, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  107.                 .addGap(18, 18, 18)
  108.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  109.                     .addComponent(jLabel2)
  110.                     .addComponent(password, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  111.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  112.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  113.                     .addComponent(jLabel3)
  114.                     .addComponent(repeat, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  115.                 .addGap(36, 36, 36)
  116.                 .addComponent(jButton1)
  117.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 73, Short.MAX_VALUE)
  118.                 .addComponent(jButton2)
  119.                 .addGap(33, 33, 33))
  120.         );
  121.  
  122.         pack();
  123.     }// </editor-fold>                        
  124.  
  125.     private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  126. this.setVisible(false);
  127. Client.lw.setVisible(true);
  128. // TODO add your handling code here:
  129.     }                                        
  130.  
  131.     private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  132.  
  133. if (name.getText().contains("§")||password.getText().contains("§")||repeat.getText().contains("§"))
  134. {
  135.     JOptionPane.showMessageDialog(rootPane, "Invalid entry. Do not use §");
  136. }
  137. else {
  138.     if(password.getText().equals(repeat.getText())) {
  139.  try {
  140.      char tmp='O';
  141.      
  142.            SocketChannel socketChannel = SocketChannel.open();
  143.     InetSocketAddress address = new InetSocketAddress("127.0.0.1", 1253);
  144.     socketChannel.connect(address);
  145.    
  146.     ByteBuffer buf = ByteBuffer.allocate(9999);
  147.    
  148.     buf.clear();
  149.       String message = name.getText() + "§" + password.getText();
  150.      
  151.          
  152.     buf.put(message.getBytes());
  153.     buf.flip();
  154.     while (buf.hasRemaining()) {
  155.        
  156.       socketChannel.write(buf);
  157.     }              
  158.                
  159.  
  160.  
  161.             buf.clear();
  162.               buf.clear();
  163.    
  164.    
  165.  
  166.  
  167.      
  168.        
  169.         buf.clear();
  170.      
  171.     socketChannel.close();
  172.      
  173.                  try {
  174.          
  175.          
  176.          
  177.           buf.clear();
  178.          
  179.           int cnt;
  180.           cnt = socketChannel.read(buf);
  181.           socketChannel.configureBlocking(false);
  182.           while (cnt > 0){
  183.               try {
  184.                   buf.flip();
  185.                   while (buf.hasRemaining()){
  186.                      tmp=((char) buf.get());
  187.                       System.out.println(tmp);
  188.                   }
  189.                   buf.clear();
  190.                   cnt = socketChannel.read(buf);
  191.               } catch (IOException ex) {
  192.                   Logger.getLogger(ChatWindow.class.getName()).log(Level.SEVERE, null, ex);
  193.               }
  194.           }
  195.           socketChannel.close();
  196.       } catch (IOException ex) {
  197.           Logger.getLogger(ChatWindow.class.getName()).log(Level.SEVERE, null, ex);
  198.       }
  199. try {
  200.          
  201.          
  202.          
  203.           buf.clear();
  204.          
  205.           int cnt;
  206.           cnt = socketChannel.read(buf);
  207.           socketChannel.configureBlocking(false);
  208.           while (cnt > 0){
  209.               try {
  210.                   buf.flip();
  211.                   while (buf.hasRemaining()){
  212.                      tmp=((char) buf.get());
  213.                       System.out.println(tmp);
  214.                   }
  215.                   buf.clear();
  216.                   cnt = socketChannel.read(buf);
  217.               } catch (IOException ex) {
  218.                   Logger.getLogger(ChatWindow.class.getName()).log(Level.SEVERE, null, ex);
  219.               }
  220.           }
  221.           socketChannel.close();
  222.       } catch (IOException ex) {
  223.           Logger.getLogger(ChatWindow.class.getName()).log(Level.SEVERE, null, ex);
  224.       }
  225.          
  226.  if (tmp=='O')
  227.  {
  228.      JOptionPane.showMessageDialog(rootPane, "Account created");
  229.  
  230.  }
  231.  else {
  232.      JOptionPane.showMessageDialog(rootPane, "Name taken");
  233.  
  234.  }
  235.         } catch (IOException e) {
  236.             e.printStackTrace();
  237.         }
  238. }
  239.     else {    JOptionPane.showMessageDialog(rootPane, "Password mismatch");
  240. }
  241.  
  242. }
  243.  
  244.  
  245.     }                                        
  246.  
  247.     /**
  248.      * @param args the command line arguments
  249.      */
  250.     public static void main(String args[]) {
  251.         /* Set the Nimbus look and feel */
  252.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  253.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  254.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  255.          */
  256.         try {
  257.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  258.                 if ("Nimbus".equals(info.getName())) {
  259.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  260.                     break;
  261.                 }
  262.             }
  263.         } catch (ClassNotFoundException ex) {
  264.             java.util.logging.Logger.getLogger(RegisterWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  265.         } catch (InstantiationException ex) {
  266.             java.util.logging.Logger.getLogger(RegisterWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  267.         } catch (IllegalAccessException ex) {
  268.             java.util.logging.Logger.getLogger(RegisterWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  269.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  270.             java.util.logging.Logger.getLogger(RegisterWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  271.         }
  272.         //</editor-fold>
  273.  
  274.         /* Create and display the form */
  275.         java.awt.EventQueue.invokeLater(new Runnable() {
  276.             public void run() {
  277.                 new RegisterWindow().setVisible(true);
  278.             }
  279.         });
  280.     }
  281.  
  282.     // Variables declaration - do not modify                    
  283.     private javax.swing.JButton jButton1;
  284.     private javax.swing.JButton jButton2;
  285.     private javax.swing.JLabel jLabel1;
  286.     private javax.swing.JLabel jLabel2;
  287.     private javax.swing.JLabel jLabel3;
  288.     private javax.swing.JTextField name;
  289.     private javax.swing.JPasswordField password;
  290.     private javax.swing.JPasswordField repeat;
  291.     // End of variables declaration                  
  292. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement