Advertisement
FranzVuttke

bincountdownvis

Sep 27th, 2023
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 15.31 KB | Source Code | 0 0
  1. package com.ounis.bincountdownvis;
  2.  
  3. import com.ounis.utils.BinDigit;
  4. import java.awt.Color;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import javax.swing.JOptionPane;
  8. import javax.swing.JPanel;
  9. import java.util.Random;
  10.  
  11. /**
  12.  *
  13.  * @author ounis
  14.  */
  15. public class MyFrame extends javax.swing.JFrame {
  16.  
  17.     /**
  18.      * Creates new form MyFrame
  19.      */
  20.    
  21.     private static final int MAX_VAL = 255;
  22.     JPanel[] binPanels;
  23.    
  24.     private int aCount = 32;
  25.    
  26.     public MyFrame() {
  27.         initComponents();
  28.        
  29.        
  30.        binPanels = new JPanel[] {jp7, jp6, jp5, jp4, jp3, jp2, jp1, jp0};
  31.        
  32.        
  33.        
  34.        btnTest.addActionListener(new ActionListener() {
  35.             @Override
  36.             public void actionPerformed(ActionEvent ae) {
  37. //                JOptionPane.showMessageDialog(null, "Lalalala");
  38.                 btnTestOnClick(ae);
  39.             }
  40.        });
  41.      
  42.        
  43. //       for (JPanel jp: binPanels) {
  44. //           setPanelColor(jp, Color.white);
  45. //       }
  46.         btnTestOnClick(null);
  47.     }
  48.    
  49.     private String dec2bin(int decimal) {
  50.         String result = "";
  51.         String[] binary = new String[] {"0", "0", "0", "0", "0", "0", "0","0"};
  52.         int x = 0;
  53.         while (decimal > 0) {
  54.             binary[x++] = String.valueOf(decimal % 2);
  55.             decimal = decimal / 2;
  56.         }
  57.        
  58.         for (int i=binary.length-1;i >= 0;i--)
  59.            
  60.             result += binary[i];
  61.        
  62.         return result;
  63.     }
  64.    
  65.     private void btnTestOnClick(ActionEvent aEvent) {
  66. //        Random rnd = new Random();
  67.        
  68. //        String bval = dec2bin(aCount);
  69.         BinDigit temp_bin = new BinDigit(aCount);
  70.         temp_bin.Convert();
  71.         String bval = temp_bin.toString();
  72. //        uzupełnianie wiodących zer czyli przygotowanie do wyświetlenia
  73.         while (bval.length() < binPanels.length)
  74.             bval = "0".concat(bval);
  75. //        System.out.println(bval);
  76.         this.setTitle(String.valueOf(this.aCount));
  77.         int ind = 0;
  78.         int val = 0;
  79.         for(;true;) {
  80.             String c = String.valueOf(bval.charAt(ind));
  81.             val = Integer.valueOf(c);
  82.             System.out.print("%d".formatted(val));
  83.             setPanelColor(binPanels[ind], val2Color(val));          
  84.             ind++;
  85.             if (ind > binPanels.length-1)
  86.                 break;
  87.         }
  88.         System.out.println();
  89.        
  90. //        for (JPanel jp: binPanels) {
  91. //            Random rnd = new Random();
  92. //            
  93. //            val = rnd.nextInt(2);
  94. //            setPanelColor(jp, val2Color(val));
  95. //        }
  96. //        System.out.println(val);
  97.         aCount++;
  98.         if (aCount > MAX_VAL)
  99.             aCount = 0;
  100.     }
  101.    
  102.     private void setPanelColor(JPanel panel, Color color) {
  103.         panel.setBackground(color);
  104.     }
  105.    
  106.     private Color val2Color(int aVal) {
  107.         return aVal == 1 ? Color.red: Color.white;
  108.     }
  109.    
  110.    
  111.     /**
  112.      * This method is called from within the constructor to initialize the form.
  113.      * WARNING: Do NOT modify this code. The content of this method is always
  114.      * regenerated by the Form Editor.
  115.      */
  116.     @SuppressWarnings("unchecked")
  117.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  118.     private void initComponents() {
  119.  
  120.         jp7 = new javax.swing.JPanel();
  121.         jp6 = new javax.swing.JPanel();
  122.         jp5 = new javax.swing.JPanel();
  123.         jp4 = new javax.swing.JPanel();
  124.         jp3 = new javax.swing.JPanel();
  125.         jp2 = new javax.swing.JPanel();
  126.         jp1 = new javax.swing.JPanel();
  127.         jp0 = new javax.swing.JPanel();
  128.         btnTest = new javax.swing.JButton();
  129.  
  130.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  131.         setBackground(new java.awt.Color(255, 255, 204));
  132.  
  133.         jp7.setBackground(new java.awt.Color(255, 0, 0));
  134.         jp7.setToolTipText("7");
  135.  
  136.         javax.swing.GroupLayout jp7Layout = new javax.swing.GroupLayout(jp7);
  137.         jp7.setLayout(jp7Layout);
  138.         jp7Layout.setHorizontalGroup(
  139.             jp7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  140.             .addGap(0, 26, Short.MAX_VALUE)
  141.         );
  142.         jp7Layout.setVerticalGroup(
  143.             jp7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  144.             .addGap(0, 68, Short.MAX_VALUE)
  145.         );
  146.  
  147.         jp6.setBackground(new java.awt.Color(255, 0, 0));
  148.         jp6.setToolTipText("6");
  149.         jp6.setPreferredSize(new java.awt.Dimension(26, 68));
  150.         jp6.setRequestFocusEnabled(false);
  151.  
  152.         javax.swing.GroupLayout jp6Layout = new javax.swing.GroupLayout(jp6);
  153.         jp6.setLayout(jp6Layout);
  154.         jp6Layout.setHorizontalGroup(
  155.             jp6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  156.             .addGap(0, 26, Short.MAX_VALUE)
  157.         );
  158.         jp6Layout.setVerticalGroup(
  159.             jp6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  160.             .addGap(0, 68, Short.MAX_VALUE)
  161.         );
  162.  
  163.         jp5.setBackground(new java.awt.Color(255, 0, 0));
  164.         jp5.setToolTipText("5");
  165.         jp5.setPreferredSize(new java.awt.Dimension(26, 68));
  166.  
  167.         javax.swing.GroupLayout jp5Layout = new javax.swing.GroupLayout(jp5);
  168.         jp5.setLayout(jp5Layout);
  169.         jp5Layout.setHorizontalGroup(
  170.             jp5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  171.             .addGap(0, 26, Short.MAX_VALUE)
  172.         );
  173.         jp5Layout.setVerticalGroup(
  174.             jp5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  175.             .addGap(0, 68, Short.MAX_VALUE)
  176.         );
  177.  
  178.         jp4.setBackground(new java.awt.Color(255, 0, 0));
  179.         jp4.setToolTipText("4");
  180.         jp4.setPreferredSize(new java.awt.Dimension(26, 68));
  181.  
  182.         javax.swing.GroupLayout jp4Layout = new javax.swing.GroupLayout(jp4);
  183.         jp4.setLayout(jp4Layout);
  184.         jp4Layout.setHorizontalGroup(
  185.             jp4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  186.             .addGap(0, 26, Short.MAX_VALUE)
  187.         );
  188.         jp4Layout.setVerticalGroup(
  189.             jp4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  190.             .addGap(0, 68, Short.MAX_VALUE)
  191.         );
  192.  
  193.         jp3.setBackground(new java.awt.Color(255, 0, 0));
  194.         jp3.setToolTipText("3");
  195.         jp3.setPreferredSize(new java.awt.Dimension(26, 68));
  196.  
  197.         javax.swing.GroupLayout jp3Layout = new javax.swing.GroupLayout(jp3);
  198.         jp3.setLayout(jp3Layout);
  199.         jp3Layout.setHorizontalGroup(
  200.             jp3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  201.             .addGap(0, 26, Short.MAX_VALUE)
  202.         );
  203.         jp3Layout.setVerticalGroup(
  204.             jp3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  205.             .addGap(0, 68, Short.MAX_VALUE)
  206.         );
  207.  
  208.         jp2.setBackground(new java.awt.Color(255, 0, 0));
  209.         jp2.setToolTipText("2");
  210.         jp2.setPreferredSize(new java.awt.Dimension(26, 68));
  211.  
  212.         javax.swing.GroupLayout jp2Layout = new javax.swing.GroupLayout(jp2);
  213.         jp2.setLayout(jp2Layout);
  214.         jp2Layout.setHorizontalGroup(
  215.             jp2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  216.             .addGap(0, 26, Short.MAX_VALUE)
  217.         );
  218.         jp2Layout.setVerticalGroup(
  219.             jp2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  220.             .addGap(0, 68, Short.MAX_VALUE)
  221.         );
  222.  
  223.         jp1.setBackground(new java.awt.Color(255, 0, 0));
  224.         jp1.setToolTipText("1");
  225.         jp1.setPreferredSize(new java.awt.Dimension(26, 68));
  226.  
  227.         javax.swing.GroupLayout jp1Layout = new javax.swing.GroupLayout(jp1);
  228.         jp1.setLayout(jp1Layout);
  229.         jp1Layout.setHorizontalGroup(
  230.             jp1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  231.             .addGap(0, 26, Short.MAX_VALUE)
  232.         );
  233.         jp1Layout.setVerticalGroup(
  234.             jp1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  235.             .addGap(0, 68, Short.MAX_VALUE)
  236.         );
  237.  
  238.         jp0.setBackground(new java.awt.Color(255, 0, 0));
  239.         jp0.setToolTipText("0");
  240.         jp0.setPreferredSize(new java.awt.Dimension(26, 68));
  241.  
  242.         javax.swing.GroupLayout jp0Layout = new javax.swing.GroupLayout(jp0);
  243.         jp0.setLayout(jp0Layout);
  244.         jp0Layout.setHorizontalGroup(
  245.             jp0Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  246.             .addGap(0, 26, Short.MAX_VALUE)
  247.         );
  248.         jp0Layout.setVerticalGroup(
  249.             jp0Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  250.             .addGap(0, 68, Short.MAX_VALUE)
  251.         );
  252.  
  253.         btnTest.setText("TEST");
  254.  
  255.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  256.         getContentPane().setLayout(layout);
  257.         layout.setHorizontalGroup(
  258.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  259.             .addGroup(layout.createSequentialGroup()
  260.                 .addGap(18, 18, 18)
  261.                 .addComponent(jp7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  262.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  263.                 .addComponent(jp6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  264.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  265.                 .addComponent(jp5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  266.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  267.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  268.                     .addComponent(btnTest)
  269.                     .addGroup(layout.createSequentialGroup()
  270.                         .addComponent(jp4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  271.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  272.                         .addComponent(jp3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  273.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  274.                         .addComponent(jp2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  275.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  276.                         .addComponent(jp1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  277.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  278.                         .addComponent(jp0, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
  279.                 .addContainerGap(20, Short.MAX_VALUE))
  280.         );
  281.         layout.setVerticalGroup(
  282.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  283.             .addGroup(layout.createSequentialGroup()
  284.                 .addGap(24, 24, 24)
  285.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  286.                     .addComponent(jp0, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  287.                     .addComponent(jp1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  288.                     .addComponent(jp2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  289.                     .addComponent(jp3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  290.                     .addComponent(jp4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  291.                     .addComponent(jp5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  292.                     .addComponent(jp6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  293.                     .addComponent(jp7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  294.                 .addGap(18, 18, 18)
  295.                 .addComponent(btnTest)
  296.                 .addContainerGap(14, Short.MAX_VALUE))
  297.         );
  298.  
  299.         pack();
  300.     }// </editor-fold>                        
  301.  
  302.     /**
  303.      * @param args the command line arguments
  304.      */
  305.    
  306. //    public static void main(String args[]) {
  307. //        /* Set the Nimbus look and feel */
  308. //        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  309. //        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  310. //         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  311. //         */
  312. //        try {
  313. //            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  314. //                if ("Nimbus".equals(info.getName())) {
  315. //                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
  316. //                    break;
  317. //                }
  318. //            }
  319. //        } catch (ClassNotFoundException ex) {
  320. //            java.util.logging.Logger.getLogger(MyFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  321. //        } catch (InstantiationException ex) {
  322. //            java.util.logging.Logger.getLogger(MyFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  323. //        } catch (IllegalAccessException ex) {
  324. //            java.util.logging.Logger.getLogger(MyFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  325. //        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  326. //            java.util.logging.Logger.getLogger(MyFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  327. //        }
  328. //        //</editor-fold>
  329. //
  330. //        /* Create and display the form */
  331. //        java.awt.EventQueue.invokeLater(new Runnable() {
  332. //            public void run() {
  333. //                new MyFrame().setVisible(true);
  334. //            }
  335. //        });
  336. //    }
  337.  
  338.     // Variables declaration - do not modify                    
  339.     private javax.swing.JButton btnTest;
  340.     private javax.swing.JPanel jp0;
  341.     private javax.swing.JPanel jp1;
  342.     private javax.swing.JPanel jp2;
  343.     private javax.swing.JPanel jp3;
  344.     private javax.swing.JPanel jp4;
  345.     private javax.swing.JPanel jp5;
  346.     private javax.swing.JPanel jp6;
  347.     private javax.swing.JPanel jp7;
  348.     // End of variables declaration                  
  349. }
  350.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement