Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.ounis.bincountdownvis;
- import com.ounis.utils.BinDigit;
- import java.awt.Color;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.JOptionPane;
- import javax.swing.JPanel;
- import java.util.Random;
- /**
- *
- * @author ounis
- */
- public class MyFrame extends javax.swing.JFrame {
- /**
- * Creates new form MyFrame
- */
- private static final int MAX_VAL = 255;
- JPanel[] binPanels;
- private int aCount = 32;
- public MyFrame() {
- initComponents();
- binPanels = new JPanel[] {jp7, jp6, jp5, jp4, jp3, jp2, jp1, jp0};
- btnTest.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent ae) {
- // JOptionPane.showMessageDialog(null, "Lalalala");
- btnTestOnClick(ae);
- }
- });
- // for (JPanel jp: binPanels) {
- // setPanelColor(jp, Color.white);
- // }
- btnTestOnClick(null);
- }
- private String dec2bin(int decimal) {
- String result = "";
- String[] binary = new String[] {"0", "0", "0", "0", "0", "0", "0","0"};
- int x = 0;
- while (decimal > 0) {
- binary[x++] = String.valueOf(decimal % 2);
- decimal = decimal / 2;
- }
- for (int i=binary.length-1;i >= 0;i--)
- result += binary[i];
- return result;
- }
- private void btnTestOnClick(ActionEvent aEvent) {
- // Random rnd = new Random();
- // String bval = dec2bin(aCount);
- BinDigit temp_bin = new BinDigit(aCount);
- temp_bin.Convert();
- String bval = temp_bin.toString();
- // uzupełnianie wiodących zer czyli przygotowanie do wyświetlenia
- while (bval.length() < binPanels.length)
- bval = "0".concat(bval);
- // System.out.println(bval);
- this.setTitle(String.valueOf(this.aCount));
- int ind = 0;
- int val = 0;
- for(;true;) {
- String c = String.valueOf(bval.charAt(ind));
- val = Integer.valueOf(c);
- System.out.print("%d".formatted(val));
- setPanelColor(binPanels[ind], val2Color(val));
- ind++;
- if (ind > binPanels.length-1)
- break;
- }
- System.out.println();
- // for (JPanel jp: binPanels) {
- // Random rnd = new Random();
- //
- // val = rnd.nextInt(2);
- // setPanelColor(jp, val2Color(val));
- // }
- // System.out.println(val);
- aCount++;
- if (aCount > MAX_VAL)
- aCount = 0;
- }
- private void setPanelColor(JPanel panel, Color color) {
- panel.setBackground(color);
- }
- private Color val2Color(int aVal) {
- return aVal == 1 ? Color.red: Color.white;
- }
- /**
- * This method is called from within the constructor to initialize the form.
- * WARNING: Do NOT modify this code. The content of this method is always
- * regenerated by the Form Editor.
- */
- @SuppressWarnings("unchecked")
- // <editor-fold defaultstate="collapsed" desc="Generated Code">
- private void initComponents() {
- jp7 = new javax.swing.JPanel();
- jp6 = new javax.swing.JPanel();
- jp5 = new javax.swing.JPanel();
- jp4 = new javax.swing.JPanel();
- jp3 = new javax.swing.JPanel();
- jp2 = new javax.swing.JPanel();
- jp1 = new javax.swing.JPanel();
- jp0 = new javax.swing.JPanel();
- btnTest = new javax.swing.JButton();
- setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
- setBackground(new java.awt.Color(255, 255, 204));
- jp7.setBackground(new java.awt.Color(255, 0, 0));
- jp7.setToolTipText("7");
- javax.swing.GroupLayout jp7Layout = new javax.swing.GroupLayout(jp7);
- jp7.setLayout(jp7Layout);
- jp7Layout.setHorizontalGroup(
- jp7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGap(0, 26, Short.MAX_VALUE)
- );
- jp7Layout.setVerticalGroup(
- jp7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGap(0, 68, Short.MAX_VALUE)
- );
- jp6.setBackground(new java.awt.Color(255, 0, 0));
- jp6.setToolTipText("6");
- jp6.setPreferredSize(new java.awt.Dimension(26, 68));
- jp6.setRequestFocusEnabled(false);
- javax.swing.GroupLayout jp6Layout = new javax.swing.GroupLayout(jp6);
- jp6.setLayout(jp6Layout);
- jp6Layout.setHorizontalGroup(
- jp6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGap(0, 26, Short.MAX_VALUE)
- );
- jp6Layout.setVerticalGroup(
- jp6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGap(0, 68, Short.MAX_VALUE)
- );
- jp5.setBackground(new java.awt.Color(255, 0, 0));
- jp5.setToolTipText("5");
- jp5.setPreferredSize(new java.awt.Dimension(26, 68));
- javax.swing.GroupLayout jp5Layout = new javax.swing.GroupLayout(jp5);
- jp5.setLayout(jp5Layout);
- jp5Layout.setHorizontalGroup(
- jp5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGap(0, 26, Short.MAX_VALUE)
- );
- jp5Layout.setVerticalGroup(
- jp5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGap(0, 68, Short.MAX_VALUE)
- );
- jp4.setBackground(new java.awt.Color(255, 0, 0));
- jp4.setToolTipText("4");
- jp4.setPreferredSize(new java.awt.Dimension(26, 68));
- javax.swing.GroupLayout jp4Layout = new javax.swing.GroupLayout(jp4);
- jp4.setLayout(jp4Layout);
- jp4Layout.setHorizontalGroup(
- jp4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGap(0, 26, Short.MAX_VALUE)
- );
- jp4Layout.setVerticalGroup(
- jp4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGap(0, 68, Short.MAX_VALUE)
- );
- jp3.setBackground(new java.awt.Color(255, 0, 0));
- jp3.setToolTipText("3");
- jp3.setPreferredSize(new java.awt.Dimension(26, 68));
- javax.swing.GroupLayout jp3Layout = new javax.swing.GroupLayout(jp3);
- jp3.setLayout(jp3Layout);
- jp3Layout.setHorizontalGroup(
- jp3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGap(0, 26, Short.MAX_VALUE)
- );
- jp3Layout.setVerticalGroup(
- jp3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGap(0, 68, Short.MAX_VALUE)
- );
- jp2.setBackground(new java.awt.Color(255, 0, 0));
- jp2.setToolTipText("2");
- jp2.setPreferredSize(new java.awt.Dimension(26, 68));
- javax.swing.GroupLayout jp2Layout = new javax.swing.GroupLayout(jp2);
- jp2.setLayout(jp2Layout);
- jp2Layout.setHorizontalGroup(
- jp2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGap(0, 26, Short.MAX_VALUE)
- );
- jp2Layout.setVerticalGroup(
- jp2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGap(0, 68, Short.MAX_VALUE)
- );
- jp1.setBackground(new java.awt.Color(255, 0, 0));
- jp1.setToolTipText("1");
- jp1.setPreferredSize(new java.awt.Dimension(26, 68));
- javax.swing.GroupLayout jp1Layout = new javax.swing.GroupLayout(jp1);
- jp1.setLayout(jp1Layout);
- jp1Layout.setHorizontalGroup(
- jp1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGap(0, 26, Short.MAX_VALUE)
- );
- jp1Layout.setVerticalGroup(
- jp1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGap(0, 68, Short.MAX_VALUE)
- );
- jp0.setBackground(new java.awt.Color(255, 0, 0));
- jp0.setToolTipText("0");
- jp0.setPreferredSize(new java.awt.Dimension(26, 68));
- javax.swing.GroupLayout jp0Layout = new javax.swing.GroupLayout(jp0);
- jp0.setLayout(jp0Layout);
- jp0Layout.setHorizontalGroup(
- jp0Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGap(0, 26, Short.MAX_VALUE)
- );
- jp0Layout.setVerticalGroup(
- jp0Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGap(0, 68, Short.MAX_VALUE)
- );
- btnTest.setText("TEST");
- javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
- getContentPane().setLayout(layout);
- layout.setHorizontalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addGap(18, 18, 18)
- .addComponent(jp7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
- .addComponent(jp6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
- .addComponent(jp5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(btnTest)
- .addGroup(layout.createSequentialGroup()
- .addComponent(jp4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
- .addComponent(jp3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
- .addComponent(jp2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
- .addComponent(jp1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
- .addComponent(jp0, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
- .addContainerGap(20, Short.MAX_VALUE))
- );
- layout.setVerticalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addGap(24, 24, 24)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(jp0, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(jp1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(jp2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(jp3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(jp4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(jp5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(jp6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(jp7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGap(18, 18, 18)
- .addComponent(btnTest)
- .addContainerGap(14, Short.MAX_VALUE))
- );
- pack();
- }// </editor-fold>
- /**
- * @param args the command line arguments
- */
- // public static void main(String args[]) {
- // /* Set the Nimbus look and feel */
- // //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
- // /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
- // * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
- // */
- // try {
- // for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
- // if ("Nimbus".equals(info.getName())) {
- // javax.swing.UIManager.setLookAndFeel(info.getClassName());
- // break;
- // }
- // }
- // } catch (ClassNotFoundException ex) {
- // java.util.logging.Logger.getLogger(MyFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- // } catch (InstantiationException ex) {
- // java.util.logging.Logger.getLogger(MyFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- // } catch (IllegalAccessException ex) {
- // java.util.logging.Logger.getLogger(MyFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- // } catch (javax.swing.UnsupportedLookAndFeelException ex) {
- // java.util.logging.Logger.getLogger(MyFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- // }
- // //</editor-fold>
- //
- // /* Create and display the form */
- // java.awt.EventQueue.invokeLater(new Runnable() {
- // public void run() {
- // new MyFrame().setVisible(true);
- // }
- // });
- // }
- // Variables declaration - do not modify
- private javax.swing.JButton btnTest;
- private javax.swing.JPanel jp0;
- private javax.swing.JPanel jp1;
- private javax.swing.JPanel jp2;
- private javax.swing.JPanel jp3;
- private javax.swing.JPanel jp4;
- private javax.swing.JPanel jp5;
- private javax.swing.JPanel jp6;
- private javax.swing.JPanel jp7;
- // End of variables declaration
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement