Ejemplos sencillos de java

Este blog tiene por objetivo ayudar a los que se inician en el
lenguaje Java, con códigos que seguramente les serán de
mucha utilidad.
Mostrando entradas con la etiqueta aplicacion awt java. Mostrar todas las entradas
Mostrando entradas con la etiqueta aplicacion awt java. Mostrar todas las entradas

sábado, 26 de marzo de 2011

EJEMPLO DE AWT

En este ejemplo , vamos a crear una pequeña aplicación que nos calculara el área y el perímetro de un triangulo , introduciendo primero los tres vértices de un triangulo cualquiera, hemos heredado de Frame
ademas implementando WindowsListener, ActionListener, KeyListener para gestionar los eventos de los
botones.
==================================================================
import java.awt.*;
import java.awt.event.*;

public class DibujoPrueba extends Frame implements WindowListener,ActionListener,KeyListener {

    //ELEMENTOS DE NUESTRO FRAME
    private Label L1,L2,L3,L4,L5=null;
    private Label V1,V2,V3,V4,V5,V6=null;
    private TextField T1,T2,T3,T4,T5,T6,T7,T8=null;
    private Button calculo=null;
    private Button limpio=null;
  
    //CONSTRUCTORES
    public DibujoPrueba(){
        super();
        configurarFrame();
    }
    public DibujoPrueba(String titulo){
        super(titulo);
        configurarFrame();
    }
    public void keyPressed(KeyEvent e){

        Object ob=e.getSource();
        char tecla=e.getKeyChar();
        if(ob.equals(T1)){
            if(tecla=='\n'){
                T2.requestFocus();
            }
        }
        if(ob.equals(T2)){
            if(tecla=='\n'){
                T3.requestFocus();
            }
        }
        if(ob.equals(T3)){
            if(tecla=='\n'){
                T4.requestFocus();
            }
        }
        if(ob.equals(T4)){
            if(tecla=='\n'){
                T5.requestFocus();
            }
        }
        if(ob.equals(T5)){
            if(tecla=='\n'){
                T6.requestFocus();
            }
        }
        if(ob.equals(T6)){
            if(tecla=='\n'){
                calcular();
            }
        }
    }
    public void keyReleased(KeyEvent e){}
    public void keyTyped(KeyEvent e){}

    public void aclarar(){
        T1.setText("");
        T2.setText("");
        T3.setText("");
        T4.setText("");
        T5.setText("");
        T6.setText("");
        T7.setText("");
        T8.setText("");
    }

    public void calcular(){
        double x1=0;
        double y1=0;
        double x2=0;
        double y2=0;
        double x3=0;
        double y3=0;

        x1=Double.valueOf(T1.getText()).doubleValue();
        y1=Double.valueOf(T2.getText()).doubleValue();
        x2=Double.valueOf(T3.getText()).doubleValue();
        y2=Double.valueOf(T4.getText()).doubleValue();
        x3=Double.valueOf(T5.getText()).doubleValue();
        y3=Double.valueOf(T6.getText()).doubleValue();

        double area=Math.abs((0.5)*(y1*(x2-x3) + y2*(x3-x1) + y3*(x1-x2)));
        double lado1=Math.sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
        double lado2=Math.sqrt((x2-x3)*(x2-x3) + (y2-y3)*(y2-y3));
        double lado3=Math.sqrt((x1-x3)*(x1-x3) + (y1-y3)*(y1-y3));
        double perimetro=lado1+lado2+lado3;

        T7.setText(String.valueOf(area));
        T8.setText(String.valueOf(perimetro));
              
    }

    public void actionPerformed(ActionEvent e){
        Object ob=e.getSource();
        if(ob.equals(calculo)){
            calcular();
        }
        else if(ob.equals(limpio)){
            aclarar();
        }
    }

    public static void main(java.lang.String[]args){
        DibujoPrueba D1=new DibujoPrueba();
        D1.show();
    }
    private void configurarFrame(){
        this.addWindowListener(this);
        this.setLayout(null);
        this.setBackground(Color.BLUE);
        this.setResizable(true);

        L1=new Label("VERTICE 1");
        L1.setBounds(20,40,70,20);
        add(L1);

        L2=new Label("VERTICE 2");
        L2.setBounds(20,60,70,20);
        add(L2);

        L3=new Label("VERTICE 3");
        L3.setBounds(20,80,70,20);
        add(L3);

        L4=new Label("AREA");
        L4.setBounds(20,120,70,20);
        add(L4);

        L5=new Label("PERIMETRO");
        L5.setBounds(20,140,80,20);
        add(L5);

        V1=new Label("X1");
        V1.setBounds(160,40,100,20);
        add(V1);

        V2=new Label("Y1");
        V2.setBounds(450,40,100,20);
        add(V2);

        V3=new Label("X2");
        V3.setBounds(160,60,100,20);
        add(V3);

        V4=new Label("Y2");
        V4.setBounds(450,60,100,20);
        add(V4);

        V5=new Label("X3");
        V5.setBounds(160,80,100,20);
        add(V5);

        V6=new Label("Y3");
        V6.setBounds(450,80,100,20);
        add(V6);

        T1=new TextField();
        T1.setBounds(270,40,80,20);
        T1.addKeyListener(this);
        add(T1);

        T2=new TextField();
        T2.setBounds(560,40,80,20);
        T2.addKeyListener(this);
        add(T2);

        T3=new TextField();
        T3.setBounds(270,60,80,20);
        T3.addKeyListener(this);
        add(T3);

        T4=new TextField();
        T4.setBounds(560,60,80,20);
        T4.addKeyListener(this);
        add(T4);

        T5=new TextField();
        T5.setBounds(270,80,80,20);
        T5.addKeyListener(this);
        add(T5);

        T6=new TextField();
        T6.setBounds(560,80,80,20);
        T6.addKeyListener(this);
        add(T6);

        T7=new TextField();
        T7.setBounds(140,120,100,20);
        T7.addKeyListener(this);
        add(T7);

        T8=new TextField();
        T8.setBounds(140,140,100,20);
        T8.addKeyListener(this);
        add(T8);

        calculo=new Button("CALCULAR");
        calculo.setBounds(20,160,100,20);
        calculo.addActionListener(this);
        add(calculo);

        limpio=new Button("RESET");
        limpio.setBounds(20,180,100,20);
        limpio.addActionListener(this);
        add(limpio);

        setSize(700,210);
        setLocation(200,200);

    }
    public void windowActivated(WindowEvent e){}
    public void windowClosed(WindowEvent e){}
    public void windowClosing(WindowEvent e){
        Window w=e.getWindow();
        if(w.equals(this)){
            this.dispose();
            System.exit(0);
        }
    }
    public void windowDeactivated(WindowEvent e){}
    public void windowDeiconified(WindowEvent e){}
    public void windowIconified(WindowEvent e){}
    public void windowOpened(WindowEvent e){}
}

============================================================


Aqui , podemos ver el resultado de nuestro pequeño codigo.