miércoles, 25 de mayo de 2011

CODIGO JAVA

CÓDIGO INTERFAZ (java).



import jPicUsb.*;
public class UsbLab3 extends javax.swing.JFrame {

int val1;
boolean cor=true,cor1=false;
    /** Creates new form UsbLab3 */
    public UsbLab3() {
        initComponents();
        try{iface.load();}
        catch(Exception e){System.out.println(e);}
        iface.set_instance(0);
        iface.set_vidpid("vid_04d8&pid_000b");
        LETRAS.setSelected(false);
        NUMERO.setSelected(true);
        T1.setEditable(false);
        T2.setEditable(false);
        T2.setVisible(false);
        velo.setVisible(false);
        L1.setVisible(false);
        L2.setVisible(false);
    }
    /** 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() {

        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jButton6 = new javax.swing.JButton();
        jButton7 = new javax.swing.JButton();
        jButton8 = new javax.swing.JButton();
        jButton9 = new javax.swing.JButton();
        jButton10 = new javax.swing.JButton();
        T1 = new javax.swing.JTextField();
        velo = new javax.swing.JScrollBar();
        T2 = new javax.swing.JTextField();
        L1 = new javax.swing.JLabel();
        L2 = new javax.swing.JLabel();
        NUMERO = new javax.swing.JRadioButton();
        LETRAS = new javax.swing.JRadioButton();
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        jButton1.setText("1");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jButton2.setText("2");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jButton3.setText("3");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        jButton4.setText("4");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });
        jButton5.setText("5");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });
        jButton6.setText("6");
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });
        jButton7.setText("7");
        jButton7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton7ActionPerformed(evt);
            }
        });
        jButton8.setText("8");
        jButton8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton8ActionPerformed(evt);
            }
        });
        jButton9.setText("9");
        jButton9.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton9ActionPerformed(evt);
            }
        });
     
        T1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                T1ActionPerformed(evt);
            }
        });
        velo.addAdjustmentListener(new java.awt.event.AdjustmentListener() {
            public void adjustmentValueChanged(java.awt.event.AdjustmentEvent evt) {
                veloAdjustmentValueChanged(evt);
            }
        });
        T2.setText("0");
        L1.setText("VELOCIDAD");
        L2.setText("MILISEGUNDOS");
        NUMERO.setText("MOSTRAR EN NUMEROS");
        NUMERO.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                NUMEROActionPerformed(evt);
            }
        });
        LETRAS.setText("MOSTRAR EN LETRAS");
        LETRAS.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                LETRASActionPerformed(evt);
            }
        });
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
              .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                   .addGroup(layout.createSequentialGroup()
                        .addGap(22, 22, 22)
                        .addComponent(T1, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButton2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jButton3))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButton5)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jButton6)))
                        .addGap(1, 1, 1))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(54, 54, 54)
                                .addComponent(jButton10)
                                .addGap(1, 1, 1))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(jButton7, javax.swing.GroupLayout.DEFAULT_SIZE, 48, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jButton8)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jButton9)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(68, 68, 68)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(NUMERO)
                            .addComponent(LETRAS))
                        .addGap(104, 104, 104))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 93, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(velo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(22, 22, 22)
                                .addComponent(T2, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(28, 28, 28)
                                .addComponent(L2))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(51, 51, 51)
                                .addComponent(L1)))
                        .addGap(33, 33, 33))))
        );
        layout.setVerticalGroup(
          layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap(22, Short.MAX_VALUE)
                        .addComponent(T1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(NUMERO)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(35, 35, 35)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButton1)
                            .addComponent(jButton2)
                            .addComponent(jButton3))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButton4)
                            .addComponent(jButton5)
                            .addComponent(jButton6))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButton7)
                            .addComponent(jButton8)
                            .addComponent(jButton9))
                        .addGap(18, 18, 18)
                        .addComponent(jButton10))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(LETRAS)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(L1)
                                .addGap(33, 33, 33)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(T2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(L2)))
                            .addComponent(velo, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(58, 58, 58))
        );
        pack();
    }// </editor-fold>                       
      public void comando (byte envio)
    {  byte[] out={envio};
       iface.QWrite(out, 1, 1000);}

    private void T1ActionPerformed(java.awt.event.ActionEvent evt) {                                  
        T1.setEditable(false);
    }                                 
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        if(cor==true){
            comando((byte)1);
            this.T1.setText(String.valueOf("1"));
        }
        if(cor1==true){
            try{
               Thread.sleep(val1);
            } catch (InterruptedException ex) {
                System.out.println("error");}
            comando((byte)85);
            try{
               Thread.sleep(val1);
            } catch (InterruptedException ex) {
                System.out.println("error");}
            comando((byte)78);
        
            try{
               Thread.sleep(val1);
            } catch (InterruptedException ex) {
                System.out.println("error");}
            comando((byte)79);
            this.T1.setText(String.valueOf("UNO"));
        }
    }                                       
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        if(cor==true){
            comando((byte)2);
            this.T1.setText(String.valueOf("2"));
        }
        if(cor1==true){
            try{
                Thread.sleep(val1);}
            catch(InterruptedException ex){
                System.out.println("error");}
            comando((byte)68);
            try{
                Thread.sleep(val1);}
            catch(InterruptedException ex){
                System.out.println("error");}
            comando((byte)79);
            try{
                Thread.sleep(val1);}
            catch(InterruptedException ex){
                System.out.println("error");}
            comando((byte)83);
            this.T1.setText(String.valueOf("DOS"));
        }
    }                                       
    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        if(cor==true){
            comando((byte)3);
            this.T1.setText(String.valueOf("3"));
        }
        if(cor1==true){
            try{
                Thread.sleep(val1);}
            catch(InterruptedException ex){
                System.out.println("error");}
            comando((byte)84);
            try{
                Thread.sleep(val1);}
            catch(InterruptedException ex){
                System.out.println("error");}
            comando((byte)82);
            try{
                Thread.sleep(val1);}
            catch(InterruptedException ex){
                System.out.println("error");}
            comando((byte)69);
            try{
                Thread.sleep(val1);}
            catch(InterruptedException ex){
                System.out.println("error");}
            comando((byte)83);
            this.T1.setText(String.valueOf("TRES"));
        }
    }                                       
    }                                       
    private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {                                         
         if(cor==true){
            comando((byte)0);
            this.T1.setText(String.valueOf("0"));
         }
         if(cor1==true){
             try{
                Thread.sleep(val1);}
            catch(InterruptedException ex){
                System.out.println("error");}
            comando((byte)67);
            try{
                Thread.sleep(val1);}
            catch(InterruptedException ex){
                System.out.println("error");}
            comando((byte)69);
            try{
                Thread.sleep(val1);}
            catch(InterruptedException ex){
                System.out.println("error");}
            comando((byte)82);
            try{
                Thread.sleep(val1);}
            catch(InterruptedException ex){
                System.out.println("error");}
            comando((byte)79);
            this.T1.setText(String.valueOf("CERO"));
         }
    }                                        
    private void veloAdjustmentValueChanged(java.awt.event.AdjustmentEvent evt) {                                           
        val1 = velo.getValue();
        val1 = val1*250;

        this.T2.setText(String.valueOf(val1));
}                                          
    private void NUMEROActionPerformed(java.awt.event.ActionEvent evt) {                                      
        velo.setVisible(false);
        LETRAS.setSelected(false);
        NUMERO.setSelected(true);
        cor=true;
        cor1=false;
        T2.setVisible(false);
        velo.setVisible(false);
        L1.setVisible(false);
        L2.setVisible(false);
}                                     
    private void LETRASActionPerformed(java.awt.event.ActionEvent evt) {                                      
        velo.setVisible(true);
        LETRAS.setSelected(true);
        NUMERO.setSelected(false);
        cor=false;
        cor1=true;
        T2.setVisible(true);
        velo.setVisible(true);
        L1.setVisible(true);
        L2.setVisible(true);
    }                                     
    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new UsbLab3().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify                    
    private javax.swing.JLabel L1;
    private javax.swing.JLabel L2;
    private javax.swing.JRadioButton LETRAS;
    private javax.swing.JRadioButton NUMERO;
    private javax.swing.JTextField T1;
    private javax.swing.JTextField T2;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton10;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButton7;
    private javax.swing.JButton jButton8;
    private javax.swing.JButton jButton9;
    private javax.swing.JScrollBar velo;
    // End of variables declaration                  
}

1 comentario:

  1. Seria mucho mejor utilizar un IDE que no sea NetBeans el cual genera una gran cantidad de codigo poco legible y que en la mayoria de los casos mas del 45% del codigo generado es innecesario. Seria mejor hacer la interfaz en un IDE como JDeveloper 11 g de Oracle, que genera codigo de forma dinamica, o Eclipse.

    ResponderEliminar