Advertisement
II_Shawn_II

Reddit r/JavaHelp - Binary Converter

Oct 25th, 2015
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.46 KB | None | 0 0
  1. import javax.swing.JOptionPane;
  2.  
  3. @SuppressWarnings( "serial")
  4. public class BinaryFrame extends javax.swing.JFrame{
  5.  
  6.     /**
  7.      * Creates new form BinaryFrame
  8.      */
  9.     public BinaryFrame(){
  10.         init();
  11.         setVisible(true);
  12.         setLocationRelativeTo(null);
  13.         setTitle("Binary Converter");
  14.     }
  15.  
  16.     private void init(){
  17.  
  18.         inputDecField = new javax.swing.JTextField();
  19.         resultBinField = new javax.swing.JTextField();
  20.         inputBinField = new javax.swing.JTextField();
  21.         resultDecField = new javax.swing.JTextField();
  22.         convertDectoBin = new javax.swing.JButton();
  23.         convertBintoDec = new javax.swing.JButton();
  24.         jLabel1 = new javax.swing.JLabel();
  25.         jLabel2 = new javax.swing.JLabel();
  26.  
  27.         setDefaultCloseOperation( javax.swing.WindowConstants.EXIT_ON_CLOSE);
  28.  
  29.         convertDectoBin.setText( "Convert");
  30.         convertDectoBin.addActionListener( new java.awt.event.ActionListener(){
  31.             public void actionPerformed( java.awt.event.ActionEvent evt){
  32.                 convertDectoBinActionPerformed( evt);
  33.             }
  34.         });
  35.  
  36.         convertBintoDec.setText( "Convert");
  37.         convertBintoDec.addActionListener( new java.awt.event.ActionListener(){
  38.             public void actionPerformed( java.awt.event.ActionEvent evt){
  39.                 convertBintoDecActionPerformed( evt);
  40.             }
  41.         });
  42.  
  43.         jLabel1.setText( "Decimal to Binary");
  44.  
  45.         jLabel2.setText( "Binary to Decimal");
  46.  
  47.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout( getContentPane());
  48.         getContentPane().setLayout( layout);
  49.         layout.setHorizontalGroup(
  50.                 layout.createParallelGroup( javax.swing.GroupLayout.Alignment.LEADING)
  51.                         .addGroup( layout.createSequentialGroup()
  52.                                 .addGap( 19, 19, 19)
  53.                                 .addGroup( layout.createParallelGroup( javax.swing.GroupLayout.Alignment.LEADING)
  54.                                         .addGroup( layout.createSequentialGroup()
  55.                                                 .addComponent( jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  56.                                                 .addContainerGap())
  57.                                         .addGroup( layout.createSequentialGroup()
  58.                                                 .addGroup( layout.createParallelGroup( javax.swing.GroupLayout.Alignment.TRAILING)
  59.                                                         .addGroup( layout.createSequentialGroup()
  60.                                                                 .addGroup( layout.createParallelGroup( javax.swing.GroupLayout.Alignment.LEADING, false)
  61.                                                                         .addComponent( inputDecField)
  62.                                                                         .addComponent( inputBinField, javax.swing.GroupLayout.DEFAULT_SIZE, 92, Short.MAX_VALUE))
  63.                                                                 .addGap( 18, 18, 18)
  64.                                                                 .addGroup( layout.createParallelGroup( javax.swing.GroupLayout.Alignment.TRAILING)
  65.                                                                         .addGroup( layout.createSequentialGroup()
  66.                                                                                 .addComponent( convertBintoDec)
  67.                                                                                 .addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.RELATED,
  68.                                                                                         javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  69.                                                                                 .addComponent( resultDecField, javax.swing.GroupLayout.PREFERRED_SIZE, 94,
  70.                                                                                         javax.swing.GroupLayout.PREFERRED_SIZE))
  71.                                                                         .addGroup( layout.createSequentialGroup()
  72.                                                                                 .addGap( 0, 0, Short.MAX_VALUE)
  73.                                                                                 .addComponent( convertDectoBin)
  74.                                                                                 .addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  75.                                                                                 .addComponent( resultBinField, javax.swing.GroupLayout.PREFERRED_SIZE, 98,
  76.                                                                                         javax.swing.GroupLayout.PREFERRED_SIZE))))
  77.                                                         .addComponent( jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  78.                                                 .addGap( 22, 22, 22)))));
  79.         layout.setVerticalGroup(
  80.                 layout.createParallelGroup( javax.swing.GroupLayout.Alignment.LEADING)
  81.                         .addGroup( layout.createSequentialGroup()
  82.                                 .addGap( 53, 53, 53)
  83.                                 .addComponent( jLabel1)
  84.                                 .addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  85.                                 .addGroup( layout.createParallelGroup( javax.swing.GroupLayout.Alignment.BASELINE)
  86.                                         .addComponent( inputDecField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
  87.                                                 javax.swing.GroupLayout.PREFERRED_SIZE)
  88.                                         .addComponent( resultBinField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
  89.                                                 javax.swing.GroupLayout.PREFERRED_SIZE)
  90.                                         .addComponent( convertDectoBin))
  91.                                 .addGap( 38, 38, 38)
  92.                                 .addComponent( jLabel2)
  93.                                 .addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  94.                                 .addGroup( layout.createParallelGroup( javax.swing.GroupLayout.Alignment.BASELINE)
  95.                                         .addComponent( inputBinField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
  96.                                                 javax.swing.GroupLayout.PREFERRED_SIZE)
  97.                                         .addComponent( resultDecField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
  98.                                                 javax.swing.GroupLayout.PREFERRED_SIZE)
  99.                                         .addComponent( convertBintoDec))
  100.                                 .addContainerGap( 151, Short.MAX_VALUE)));
  101.  
  102.         pack();
  103.     }
  104.  
  105.     private void convertDectoBinActionPerformed( java.awt.event.ActionEvent evt){
  106.         int inputDec = Integer.parseInt( inputDecField.getText());
  107.         if( inputDec < 0){
  108.             JOptionPane.showMessageDialog( this, "Try a positive number.", "Message", JOptionPane.INFORMATION_MESSAGE);
  109.             inputDecField.setText("");
  110.             resultBinField.setText("");
  111.         }else{
  112.             resultBinField.setText( convertDectoBinRecursive( inputDec));
  113.         }
  114.     }
  115.  
  116.     private void convertBintoDecActionPerformed( java.awt.event.ActionEvent evt){
  117.         int inputBin = Integer.parseInt( inputBinField.getText());
  118.         if( inputBin < 0){
  119.             JOptionPane.showMessageDialog( this, "Try a positive number.", "Message", JOptionPane.INFORMATION_MESSAGE);
  120.             inputBinField.setText("");
  121.             resultDecField.setText("");
  122.         }else{
  123.             resultDecField.setText( convertBintoDec( inputBinField.getText()));
  124.         }
  125.     }
  126.    
  127.     //http://www.wikihow.com/Convert-from-Binary-to-Decimal
  128.     //http://stackoverflow.com/questions/7437987/how-to-convert-binary-string-value-to-decimal
  129.     private String convertBintoDec( String binInput){
  130.         int resultDec = 0;
  131.         int binLength = binInput.length();
  132.         for( int i = 0; i < binLength; i++){
  133.             if( binInput.charAt( i) == '1'){
  134.                 resultDec += Math.pow( 2, binLength - 1 - i);
  135.             }
  136.         }
  137.         return Integer.toString( resultDec);
  138.     }
  139.  
  140.     private String convertDectoBinRecursive( int inputDec){
  141.         int modulus;
  142.         if( inputDec <= 1){
  143.             return Integer.toString( inputDec);
  144.         }
  145.         modulus = inputDec % 2;
  146.         //Right Shift (bitwise operator)in this case equivalent to division by 2
  147.         return convertDectoBinRecursive( inputDec >> 1) + modulus;
  148.     }
  149.  
  150.     /**
  151.      * @param args the command line arguments
  152.      */
  153.     public static void main( String args[]){
  154.         /* Set the Nimbus look and feel */
  155.         // <editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  156.         /*
  157.          * If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  158.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  159.          */
  160.         try{
  161.             for( javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()){
  162.                 if( "Nimbus".equals( info.getName())){
  163.                     javax.swing.UIManager.setLookAndFeel( info.getClassName());
  164.                     break;
  165.                 }
  166.             }
  167.         }catch( ClassNotFoundException ex){
  168.             java.util.logging.Logger.getLogger( BinaryFrame.class.getName()).log( java.util.logging.Level.SEVERE, null, ex);
  169.         }catch( InstantiationException ex){
  170.             java.util.logging.Logger.getLogger( BinaryFrame.class.getName()).log( java.util.logging.Level.SEVERE, null, ex);
  171.         }catch( IllegalAccessException ex){
  172.             java.util.logging.Logger.getLogger( BinaryFrame.class.getName()).log( java.util.logging.Level.SEVERE, null, ex);
  173.         }catch( javax.swing.UnsupportedLookAndFeelException ex){
  174.             java.util.logging.Logger.getLogger( BinaryFrame.class.getName()).log( java.util.logging.Level.SEVERE, null, ex);
  175.         } // </editor-fold>
  176.  
  177.         /* Create and display the form */
  178.         java.awt.EventQueue.invokeLater( new Runnable(){
  179.             public void run(){
  180.                 new BinaryFrame().setVisible( true);
  181.             }
  182.         });
  183.     }
  184.  
  185.     // Variables declaration - do not modify
  186.     private javax.swing.JButton convertDectoBin;
  187.     private javax.swing.JTextField inputBinField;
  188.     private javax.swing.JTextField inputDecField;
  189.     private javax.swing.JButton convertBintoDec;
  190.     private javax.swing.JLabel jLabel1;
  191.     private javax.swing.JLabel jLabel2;
  192.     private javax.swing.JTextField resultBinField;
  193.     private javax.swing.JTextField resultDecField;
  194.     // End of variables declaration
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement