All pastes #896167 Raw Edit

Miscellany

public text v1 · immutable
·published 2008-02-08 12:41 UTC
rendered paste body
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;         //for events
import javax.swing.event.*;


public class FiveDigit extends JFrame implements ActionListener
{
    int value=0;
    
    
    JPanel jpCenter = new JPanel();
    JPanel jpTop = new JPanel();
    JPanel jpBottom = new JPanel(new FlowLayout());
    JButton jbAdd = new JButton("+");
    JButton jbSubtract = new JButton("-");
    JButton jbAutoInc = new JButton("Auto");
    ImageIcon[] img = new ImageIcon[10];//declare image array
    JLabel[] jlDigitDisplay = new JLabel[10];   //declare jlabel array
    JPanel[] jpDigitDisplay = new JPanel[10];
    JamesPanel jpSDDOne = new JamesPanel();
    JamesPanel jpSDDTwo = new JamesPanel();
    JamesPanel jpSDDThree = new JamesPanel();
    JamesPanel jpSDDFour = new JamesPanel();
    JamesPanel jpSDDFive = new JamesPanel();
    JLabel jlValue = new JLabel ("Input value 0 - 99999");
    JTextField valueInput = new JTextField(5);
    javax.swing.Timer t;
    boolean ticking;
    
    public FiveDigit()
    {
        
        for (int i=0; i<10; i++)
        {
            img[i] = new ImageIcon("sddigit"+i+".png");
        }      
        
        for (int i=0; i<10; i++)
        {
            jlDigitDisplay[i] = new JLabel (img[0]);
        }      
               
        for (int i=0; i<10; i++)
        {
            jlDigitDisplay[i].setIcon(img[value]);
        }
        
        jpCenter.add(jpSDDOne);
        jpCenter.add(jpSDDTwo);
        jpCenter.add(jpSDDThree);
        jpCenter.add(jpSDDFour);
        jpCenter.add(jpSDDFive);
        jpSDDOne.add(jlDigitDisplay[0]);
        jpSDDTwo.add(jlDigitDisplay[1]);
        jpSDDThree.add(jlDigitDisplay[2]);
        jpSDDFour.add(jlDigitDisplay[3]);
        jpSDDFive.add(jlDigitDisplay[4]);       
        getContentPane().add(jpCenter, BorderLayout.CENTER);
        
        jpTop.add(jlValue);
        jpTop.add(valueInput);
        valueInput.addActionListener(this);
        getContentPane().add(jpTop, BorderLayout.NORTH);
        
        jpBottom.add(jbAdd);
        jbAdd.addActionListener(this);
        jpBottom.add(jbSubtract);
        jbSubtract.addActionListener(this);
        jpBottom.add(jbAutoInc);
        jbAutoInc.addActionListener(this);
        
        getContentPane().add(jpBottom, BorderLayout.SOUTH);
        pack();
        
         jbAutoInc.addActionListener(new ActionListener()
              {
                  public void actionPerformed(ActionEvent e)
                        {
                            if(ticking)
                            {
                                t.stop();
                                ticking = false;
                            }
                            else{
                                t.restart();
                                ticking = true;
                            }
                            
                        }
                    }
            );
              
         t = new javax.swing.Timer(300, new ActionListener()
                    {
                        public void actionPerformed(ActionEvent e)
                        {
                            value++;
                            //ae();
                        }
                    }
                    );
        
        
    }  
    
    
    
    class JamesPanel extends JPanel
    {
        public JamesPanel()
        {
            this.setPreferredSize(new Dimension(100,150));
        }
        public void paintComponent(Graphics g)
        {
            super.paintComponent(g);
            
            
        }
        
    }
    
    
    public void actionPerformed(ActionEvent ae) 
    {
       if (ae.getSource() == jbAdd)
        {
           value=value+1;                                
           String strNum = "" + value;
           valueInput.setText(strNum);
           int value2 = strNum.length();
           if(value2==1)
           {
               String strDigit4 = strNum.substring(0,1);
               int intDigit4 = Integer.parseInt(strDigit4);
               jlDigitDisplay[4].setIcon(img[intDigit4]);
               
               int intDigit3 =0;
               jlDigitDisplay[3].setIcon(img[intDigit3]);
               
               int intDigit2 =0;
               jlDigitDisplay[2].setIcon(img[intDigit2]);
              
               int intDigit1 =0;
               jlDigitDisplay[1].setIcon(img[intDigit1]);
               
               int intDigit0 =0;
               jlDigitDisplay[0].setIcon(img[intDigit0]);
           }
           
           if(value2==2)
           
           {
               String strDigit4 = strNum.substring(1,2);
               int intDigit4 = Integer.parseInt(strDigit4);
               jlDigitDisplay[4].setIcon(img[intDigit4]);
               
               String strDigit3 = strNum.substring(0,1);
               int intDigit3 = Integer.parseInt(strDigit3);
               jlDigitDisplay[3].setIcon(img[intDigit3]);
               
               int intDigit2 =0;
               jlDigitDisplay[2].setIcon(img[intDigit2]);
              
               int intDigit1 =0;
               jlDigitDisplay[1].setIcon(img[intDigit1]);
               
               int intDigit0 =0;
               jlDigitDisplay[0].setIcon(img[intDigit0]);
           }
           
           if(value2==3)
           {
               
               String strDigit4 = strNum.substring(2,3);
               int intDigit4 = Integer.parseInt(strDigit4);
               jlDigitDisplay[4].setIcon(img[intDigit4]);
               
               String strDigit3 = strNum.substring(1,2);
               int intDigit3 = Integer.parseInt(strDigit3);
               jlDigitDisplay[3].setIcon(img[intDigit3]);
               
               String strDigit2 = strNum.substring(0,1);
               int intDigit2 = Integer.parseInt(strDigit2);
               jlDigitDisplay[2].setIcon(img[intDigit2]);
               
               int intDigit1 =0;
               jlDigitDisplay[1].setIcon(img[intDigit1]);
               
               int intDigit0 =0;
               jlDigitDisplay[0].setIcon(img[intDigit0]);
                              
            }   
            
            if(value2==4)
            {
                
               String strDigit4 = strNum.substring(3,4);
               int intDigit4 = Integer.parseInt(strDigit4);
               jlDigitDisplay[4].setIcon(img[intDigit4]);
               
               String strDigit3 = strNum.substring(2,3);
               int intDigit3 = Integer.parseInt(strDigit3);
               jlDigitDisplay[3].setIcon(img[intDigit3]);
               
               String strDigit2 = strNum.substring(1,2);
               int intDigit2 = Integer.parseInt(strDigit2);
               jlDigitDisplay[2].setIcon(img[intDigit2]);
               
               String strDigit1 = strNum.substring(0,1);
               int intDigit1 = Integer.parseInt(strDigit1);
               jlDigitDisplay[1].setIcon(img[intDigit1]);
                           
               int intDigit5 =0;
               jlDigitDisplay[0].setIcon(img[intDigit5]);

            }
            if(value2==5)
            {
               
               String strDigit4 = strNum.substring(4,5);
               int intDigit4 = Integer.parseInt(strDigit4);
               jlDigitDisplay[4].setIcon(img[intDigit4]);
               
               String strDigit3 = strNum.substring(3,4);
               int intDigit3 = Integer.parseInt(strDigit3);
               jlDigitDisplay[3].setIcon(img[intDigit3]);
               
               String strDigit2 = strNum.substring(2,3);
               int intDigit2 = Integer.parseInt(strDigit2);
               jlDigitDisplay[2].setIcon(img[intDigit2]);
               
               String strDigit1 = strNum.substring(1,2);
               int intDigit1 = Integer.parseInt(strDigit1);
               jlDigitDisplay[1].setIcon(img[intDigit1]);
               
               String strDigit0 = strNum.substring(0,1);
               int intDigit0 = Integer.parseInt(strDigit0);
               jlDigitDisplay[0].setIcon(img[intDigit0]);

                
            }
            
            if(value>99999 || value<0)
            {
                value=0;
            }

        }
        
       else if (ae.getSource() == jbSubtract)
        {
           if(value==0)
            {
                value=0;
            }
           else{
               
                   value=value-1;
                   String strNum = "" + value;
                   valueInput.setText(strNum);
                   int value2 = strNum.length();
                   
                   if(value2==1)
                   {
                       String strDigit4 = strNum.substring(0,1);
                       int intDigit4 = Integer.parseInt(strDigit4);
                       jlDigitDisplay[4].setIcon(img[intDigit4]);
                       
                       int intDigit3 =0;
                       jlDigitDisplay[3].setIcon(img[intDigit3]);
                       
                       int intDigit2 =0;
                       jlDigitDisplay[2].setIcon(img[intDigit2]);
                      
                       int intDigit1 =0;
                       jlDigitDisplay[1].setIcon(img[intDigit1]);
                       
                       int intDigit0 =0;
                       jlDigitDisplay[0].setIcon(img[intDigit0]);
                   }
                   
                   if(value2==2)
                   
                   {
                       String strDigit4 = strNum.substring(1,2);
                       int intDigit4 = Integer.parseInt(strDigit4);
                       jlDigitDisplay[4].setIcon(img[intDigit4]);
                       
                       String strDigit3 = strNum.substring(0,1);
                       int intDigit3 = Integer.parseInt(strDigit3);
                       jlDigitDisplay[3].setIcon(img[intDigit3]);
                       
                       int intDigit2 =0;
                       jlDigitDisplay[2].setIcon(img[intDigit2]);
                      
                       int intDigit1 =0;
                       jlDigitDisplay[1].setIcon(img[intDigit1]);
                       
                       int intDigit0 =0;
                       jlDigitDisplay[0].setIcon(img[intDigit0]);
                   }
                   
                   if(value2==3)
                   {
                       
                       String strDigit4 = strNum.substring(2,3);
                       int intDigit4 = Integer.parseInt(strDigit4);
                       jlDigitDisplay[4].setIcon(img[intDigit4]);
                       
                       String strDigit3 = strNum.substring(1,2);
                       int intDigit3 = Integer.parseInt(strDigit3);
                       jlDigitDisplay[3].setIcon(img[intDigit3]);
                       
                       String strDigit2 = strNum.substring(0,1);
                       int intDigit2 = Integer.parseInt(strDigit2);
                       jlDigitDisplay[2].setIcon(img[intDigit2]);
                       
                       int intDigit1 =0;
                       jlDigitDisplay[1].setIcon(img[intDigit1]);
                       
                       int intDigit0 =0;
                       jlDigitDisplay[0].setIcon(img[intDigit0]);
                                      
                    }   
                    
                    if(value2==4)
                    {
                        
                       String strDigit4 = strNum.substring(3,4);
                       int intDigit4 = Integer.parseInt(strDigit4);
                       jlDigitDisplay[4].setIcon(img[intDigit4]);
                       
                       String strDigit3 = strNum.substring(2,3);
                       int intDigit3 = Integer.parseInt(strDigit3);
                       jlDigitDisplay[3].setIcon(img[intDigit3]);
                       
                       String strDigit2 = strNum.substring(1,2);
                       int intDigit2 = Integer.parseInt(strDigit2);
                       jlDigitDisplay[2].setIcon(img[intDigit2]);
                       
                       String strDigit1 = strNum.substring(0,1);
                       int intDigit1 = Integer.parseInt(strDigit1);
                       jlDigitDisplay[1].setIcon(img[intDigit1]);
                                   
                       int intDigit5 =0;
                       jlDigitDisplay[0].setIcon(img[intDigit5]);
        
                    }
                    if(value2==5)
                    {
                       
                       String strDigit4 = strNum.substring(4,5);
                       int intDigit4 = Integer.parseInt(strDigit4);
                       jlDigitDisplay[4].setIcon(img[intDigit4]);
                       
                       String strDigit3 = strNum.substring(3,4);
                       int intDigit3 = Integer.parseInt(strDigit3);
                       jlDigitDisplay[3].setIcon(img[intDigit3]);
                       
                       String strDigit2 = strNum.substring(2,3);
                       int intDigit2 = Integer.parseInt(strDigit2);
                       jlDigitDisplay[2].setIcon(img[intDigit2]);
                       
                       String strDigit1 = strNum.substring(1,2);
                       int intDigit1 = Integer.parseInt(strDigit1);
                       jlDigitDisplay[1].setIcon(img[intDigit1]);
                       
                       String strDigit0 = strNum.substring(0,1);
                       int intDigit0 = Integer.parseInt(strDigit0);
                       jlDigitDisplay[0].setIcon(img[intDigit0]);
        
                        
                    }
                }
            if(value>99999 || value<0)
            {
                value=0;
            }
           
           
        }
       
       else if (ae.getSource() == valueInput)
       {
           
           String strNum = valueInput.getText();
           value = Integer.parseInt(valueInput.getText());
           int value2 = strNum.length();
           
           if(value2==1)
           {
               String strDigit4 = strNum.substring(0,1);
               int intDigit4 = Integer.parseInt(strDigit4);
               jlDigitDisplay[4].setIcon(img[intDigit4]);
               
               int intDigit3 =0;
               jlDigitDisplay[3].setIcon(img[intDigit3]);
               
               int intDigit2 =0;
               jlDigitDisplay[2].setIcon(img[intDigit2]);
              
               int intDigit1 =0;
               jlDigitDisplay[1].setIcon(img[intDigit1]);
               
               int intDigit0 =0;
               jlDigitDisplay[0].setIcon(img[intDigit0]);
           }
           
           if(value2==2)
           
           {
               String strDigit4 = strNum.substring(1,2);
               int intDigit4 = Integer.parseInt(strDigit4);
               jlDigitDisplay[4].setIcon(img[intDigit4]);
               
               String strDigit3 = strNum.substring(0,1);
               int intDigit3 = Integer.parseInt(strDigit3);
               jlDigitDisplay[3].setIcon(img[intDigit3]);
               
               int intDigit2 =0;
               jlDigitDisplay[2].setIcon(img[intDigit2]);
              
               int intDigit1 =0;
               jlDigitDisplay[1].setIcon(img[intDigit1]);
               
               int intDigit0 =0;
               jlDigitDisplay[0].setIcon(img[intDigit0]);
           }
           
           if(value2==3)
           {
               
               String strDigit4 = strNum.substring(2,3);
               int intDigit4 = Integer.parseInt(strDigit4);
               jlDigitDisplay[4].setIcon(img[intDigit4]);
               
               String strDigit3 = strNum.substring(1,2);
               int intDigit3 = Integer.parseInt(strDigit3);
               jlDigitDisplay[3].setIcon(img[intDigit3]);
               
               String strDigit2 = strNum.substring(0,1);
               int intDigit2 = Integer.parseInt(strDigit2);
               jlDigitDisplay[2].setIcon(img[intDigit2]);
               
               int intDigit1 =0;
               jlDigitDisplay[1].setIcon(img[intDigit1]);
               
               int intDigit0 =0;
               jlDigitDisplay[0].setIcon(img[intDigit0]);
                              
            }   
            
            if(value2==4)
            {
                
               String strDigit4 = strNum.substring(3,4);
               int intDigit4 = Integer.parseInt(strDigit4);
               jlDigitDisplay[4].setIcon(img[intDigit4]);
               
               String strDigit3 = strNum.substring(2,3);
               int intDigit3 = Integer.parseInt(strDigit3);
               jlDigitDisplay[3].setIcon(img[intDigit3]);
               
               String strDigit2 = strNum.substring(1,2);
               int intDigit2 = Integer.parseInt(strDigit2);
               jlDigitDisplay[2].setIcon(img[intDigit2]);
               
               String strDigit1 = strNum.substring(0,1);
               int intDigit1 = Integer.parseInt(strDigit1);
               jlDigitDisplay[1].setIcon(img[intDigit1]);
                           
               int intDigit5 =0;
               jlDigitDisplay[0].setIcon(img[intDigit5]);

            }
            if(value2==5)
            {
               
               String strDigit4 = strNum.substring(4,5);
               int intDigit4 = Integer.parseInt(strDigit4);
               jlDigitDisplay[4].setIcon(img[intDigit4]);
               
               String strDigit3 = strNum.substring(3,4);
               int intDigit3 = Integer.parseInt(strDigit3);
               jlDigitDisplay[3].setIcon(img[intDigit3]);
               
               String strDigit2 = strNum.substring(2,3);
               int intDigit2 = Integer.parseInt(strDigit2);
               jlDigitDisplay[2].setIcon(img[intDigit2]);
               
               String strDigit1 = strNum.substring(1,2);
               int intDigit1 = Integer.parseInt(strDigit1);
               jlDigitDisplay[1].setIcon(img[intDigit1]);
               
               String strDigit0 = strNum.substring(0,1);
               int intDigit0 = Integer.parseInt(strDigit0);
               jlDigitDisplay[0].setIcon(img[intDigit0]);

                
            }
            
            if(value>99999 || value<0)
            {
                value=0;
            }
        
        }
    }
    
    
    
     public static void main(String[] args)
     {
       
        FiveDigit fdd = new FiveDigit();
        fdd.setTitle("Five Digit Display");
        fdd.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        fdd.setLocation(20,20);
        fdd.setSize(550, 250);
        fdd.setVisible(true);
        
        
     }//end main   
    
    
}