Ver Mensaje Individual
  #1 (permalink)  
Antiguo 25/06/2011, 17:04
ElMeoS
 
Fecha de Ingreso: junio-2011
Mensajes: 4
Antigüedad: 12 años, 10 meses
Puntos: 3
Transformacion de numero binario a decimal

Muy Buenas noches!

El dia de Hoy he querido compartir con ustedes el codigo de mi sencillo pero util applet en Java!

el programa convierte un numero binario a decimal como bien dice el titulo, solo que solo es factible de 4 bits, no obstante si se logra entender la idea del programa se puede ampliar hasta 8 bits que seria mucho mas util.

He aqui el Codigo!

Código HTML:
Ver original
  1. import java.awt.*;
  2. import java.applet.*;
  3. import java.awt.event.*;
  4. import javax.swing.*;
  5.  
  6. public class TransBinDec extends Applet implements ActionListener{
  7.    
  8.     private Button A,B,C,D;
  9.     private Color C1 = new Color(198,226,255);
  10.     private Color C2 = new Color(255,255,255);
  11.     private TextField t1;
  12.     private Font TL = new Font("Default",Font.PLAIN,20);
  13.     private String frase1 ="Presione en los botones binarios",frase2="El valor en decimal es";
  14.     private String a1,b1,c1,d1;
  15.     private int bin1,bin2,bin3,bin4,bin5;
  16.    
  17.     public void init(){
  18.        
  19.         t1 = new TextField("0",10);
  20.         t1.setBounds(170,200,150,40);
  21.         t1.setEditable(false);
  22.         t1.setBackground(Color.white);
  23.         t1.setFont( TL );
  24.         add(t1);
  25.         this.setLayout(null);
  26.         this.setSize(500,500);
  27.         setBackground(C1);                          
  28.         A=new Button ("0");          
  29.         A.setBounds(150,100,40,60);                
  30.         A.addActionListener(this);              
  31.         A.setBackground(C2);                    
  32.         add(A);
  33.         a1="0";
  34.         B=new Button("0");                
  35.         B.setBounds(200,100,40,60);          
  36.         B.addActionListener(this);          
  37.         B.setBackground(C2);                
  38.         add(B);
  39.         b1="0";
  40.         C=new Button("0");                
  41.         C.setBounds(250,100,40,60);          
  42.         C.addActionListener(this);          
  43.         C.setBackground(C2);                
  44.         add(C);
  45.         c1="0";
  46.         D=new Button("0");                
  47.         D.setBounds(300,100,40,60);          
  48.         D.addActionListener(this);          
  49.         D.setBackground(C2);
  50.         add(D);
  51.         d1="0";
  52.    
  53.     }
  54.    
  55.     public void paint (Graphics g){
  56.         g.setFont(new Font("TimesRoman", Font.PLAIN, 20));
  57.         g.setColor(Color.black);
  58.         g.drawString(frase1,120,90);
  59.         g.drawString(frase2,150,190);
  60.     }
  61.    
  62.     public void actionPerformed( ActionEvent e){
  63.         if(e.getSource() == A){
  64.             String E=e.getActionCommand();
  65.             if(E.equals("0") ){
  66.                 A.setLabel("1");
  67.                 a1="1";
  68.             }
  69.            
  70.             if(E.equals("1")){
  71.                 A.setLabel("0");
  72.                 a1="0";
  73.             }
  74.         }
  75.        
  76.         if(e.getSource() == B){
  77.             String E=e.getActionCommand();
  78.             if(E.equals("0") ){
  79.                 B.setLabel("1");
  80.                 b1="1";
  81.             }
  82.            
  83.             if(E.equals("1")){
  84.                 B.setLabel("0");
  85.                 b1="0";
  86.             }
  87.         }
  88.        
  89.         if(e.getSource() == C){
  90.             String E=e.getActionCommand();
  91.             if(E.equals("0") ){
  92.                 C.setLabel("1");
  93.                 c1="1";
  94.             }
  95.            
  96.             if(E.equals("1")){
  97.                 C.setLabel("0");
  98.                 c1="0";
  99.             }
  100.         }
  101.        
  102.         if(e.getSource() == D){
  103.             String E=e.getActionCommand();
  104.             if(E.equals("0") ){
  105.                 D.setLabel("1");
  106.                 d1="1";
  107.             }
  108.            
  109.             if(E.equals("1")){
  110.                 D.setLabel("0");
  111.                 d1="0";
  112.             }
  113.         }
  114.         convierte();
  115.     }
  116.    
  117.     public void convierte(){
  118.        
  119.         bin1 = Integer.parseInt(a1)*8;
  120.         bin2 = Integer.parseInt(b1)*4;
  121.         bin3 = Integer.parseInt(c1)*2;
  122.         bin4 = Integer.parseInt(d1)*1;
  123.         bin5=0;
  124.         bin5 = bin1+bin2+bin3+bin4;
  125.         t1.setText(Integer.toString(bin5));
  126.         repaint();
  127.     }
  128. }

Espero poder seguir subiendo nuevos aportes proximamente asiesque esten atentos amigos del web!

Saludos!