Translate-Traducir

18/5/14

El embalse

Un embalse debe manejar la cantidad de mts3 de agua que pasa por cada compuerta. 
Por cada compuerta puede pasar un caudal de 100 mts3 x seg.
Cuando presionamos el botón "Actualizar caudal" mostramos el nivel de caudal actual y un mensaje que indica si el caudal es:

  • Bajo (0 a 100 mts3 x seg.)
  • Medio (> 100 -200 mts3. x seg.)
  • Alto (>200 mts3 x seg.)
Para la selección del caudal de cada compuerta utilizar componentes de tipo JSpinner.

Solución:

Hemos optado por simular tres compuertas para esta simulación.

Descargar código fuente.
Descargar ejecutable.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class Embalse extends JFrame implements ActionListener, ChangeListener{

    private static final long serialVersionUID = 5711819577192515672L;
    private JPanel contentPane;
    private JButton btnActualizar;
    private JLabel lblCaudal1;
    private JLabel lblCaudal2;
    private JLabel lblCaudal3;
    private JSpinner spCompuerta2;
    private JSpinner spCompuerta1;
    private JSpinner spCompuerta3;
    private JLabel lblCaudal;
    private int caudalTotal;

// Metodo Principal
    public static void main(String[] args) {
        Embalse frame = new Embalse();
        frame.setVisible(true);
    }

// Constructor, inicializamos los Atributos de la clase
    public Embalse() {
        setCaudalTotal(0);
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);
       
        lblCaudal = new JLabel("EMBALSE CERRADO");
        lblCaudal.setHorizontalAlignment(SwingConstants.CENTER);
        lblCaudal.setFont(new Font("Arial", Font.BOLD, 22));
        lblCaudal.setName("lblCaudal");
        lblCaudal.setBounds(10, 28, 424, 41);
        contentPane.add(lblCaudal);
       
        JPanel compuerta1 = new JPanel();
        compuerta1.setBackground(Color.LIGHT_GRAY);
        compuerta1.setName("compuerta1");
        compuerta1.setBounds(10, 94, 134, 82);
        contentPane.add(compuerta1);
        compuerta1.setLayout(new BorderLayout(0, 0));
       
        JLabel lblCaudal_1 = new JLabel("COMPUERTA 1");
        lblCaudal_1.setHorizontalAlignment(SwingConstants.CENTER);
        lblCaudal_1.setFont(new Font("Arial", Font.BOLD, 14));
        compuerta1.add(lblCaudal_1, BorderLayout.NORTH);
       
        lblCaudal1 = new JLabel("CERRADA");
        lblCaudal1.setBackground(Color.WHITE);
        lblCaudal1.setOpaque(true);
        lblCaudal1.setName("lblCaudal1");
        compuerta1.add(lblCaudal1, BorderLayout.SOUTH);
        lblCaudal1.setFont(new Font("Arial", Font.BOLD, 14));
        lblCaudal1.setHorizontalAlignment(SwingConstants.CENTER);
       
        spCompuerta1 = new JSpinner();
        spCompuerta1.addChangeListener(this);
        spCompuerta1.setModel(new SpinnerNumberModel(0, 0, 300, 1));
        spCompuerta1.setFont(new Font("Arial Black", Font.BOLD, 22));
        spCompuerta1.setName("spCompuerta1");
        compuerta1.add(spCompuerta1, BorderLayout.CENTER);
       
        JPanel compuerta2 = new JPanel();
        compuerta2.setBackground(Color.LIGHT_GRAY);
        compuerta2.setName("compuerta2");
        compuerta2.setBounds(154, 94, 134, 82);
        contentPane.add(compuerta2);
        compuerta2.setLayout(new BorderLayout(0, 0));
       
        JLabel lblNewLabel = new JLabel("COMPUERTA 2");
        lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
        lblNewLabel.setFont(new Font("Arial", Font.BOLD, 14));
        compuerta2.add(lblNewLabel, BorderLayout.NORTH);
       
        lblCaudal2 = new JLabel("CERRADA");
        lblCaudal2.setBackground(Color.WHITE);
        lblCaudal2.setOpaque(true);
        lblCaudal2.setName("lblCaudal2");
        lblCaudal2.setHorizontalAlignment(SwingConstants.CENTER);
        lblCaudal2.setFont(new Font("Arial", Font.BOLD, 14));
        compuerta2.add(lblCaudal2, BorderLayout.SOUTH);
       
        spCompuerta2 = new JSpinner();
        spCompuerta2.addChangeListener(this);
        spCompuerta2.setModel(new SpinnerNumberModel(0, 0, 300, 1));
        spCompuerta2.setFont(new Font("Arial Black", Font.BOLD, 22));
        spCompuerta2.setName("spCompuerta2");
        compuerta2.add(spCompuerta2, BorderLayout.CENTER);
       
        JPanel compuerta3 = new JPanel();
        compuerta3.setBackground(Color.LIGHT_GRAY);
        compuerta3.setName("compuerta3");
        compuerta3.setBounds(298, 94, 134, 82);
        contentPane.add(compuerta3);
        compuerta3.setLayout(new BorderLayout(0, 0));
       
        JLabel lblCompuerta = new JLabel("COMPUERTA 3");
        lblCompuerta.setHorizontalAlignment(SwingConstants.CENTER);
        lblCompuerta.setFont(new Font("Arial", Font.BOLD, 14));
        compuerta3.add(lblCompuerta, BorderLayout.NORTH);
       
        lblCaudal3 = new JLabel("CERRADA");
        lblCaudal3.setBackground(Color.WHITE);
        lblCaudal3.setOpaque(true);
        lblCaudal3.setName("lblCaudal3");
        lblCaudal3.setHorizontalAlignment(SwingConstants.CENTER);
        lblCaudal3.setFont(new Font("Arial", Font.BOLD, 14));
        compuerta3.add(lblCaudal3, BorderLayout.SOUTH);
       
        spCompuerta3 = new JSpinner();
        spCompuerta3.addChangeListener(this);
        spCompuerta3.setModel(new SpinnerNumberModel(0, 0, 300, 1));
        spCompuerta3.setName("spCompuerta3");
        spCompuerta3.setFont(new Font("Arial Black", Font.BOLD, 22));
        compuerta3.add(spCompuerta3, BorderLayout.CENTER);
       
        btnActualizar = new JButton("Actualizar Caudal");
        btnActualizar.setName("btnActualizar");
        btnActualizar.setFont(new Font("Arial", Font.PLAIN, 16));
        btnActualizar.setBounds(129, 209, 185, 41);
        btnActualizar.addActionListener(this);
        contentPane.add(btnActualizar);
    }

// Método actalizar el caudal del embalse
    private void actualizarCaudal(){
        int caudal1, caudal2, caudal3;
        // sumamos el caudal de las tres compuertas
        caudal1 = (Integer) spCompuerta1.getValue();
        caudal2 = (Integer) spCompuerta2.getValue();
        caudal3 = (Integer) spCompuerta3.getValue();
        this.setCaudalTotal(caudal1 + caudal2 + caudal3);
        this.modificarEtiquetas(caudal1, caudal2, caudal3);
    }
   
    private void modificarEtiquetas(int c1, int c2, int c3){
        // Mostramos el nivel del caudal de la compuerta 1
        if (c1==0){
            // El caudal es 0 -> la compuerta está cerrada
            this.lblCaudal1.setText("CERRADA");
            this.lblCaudal1.setBackground(Color.white);               
        }else if (c1<=100){
            this.lblCaudal1.setText("CAUDAL BAJO");
            this.lblCaudal1.setBackground(Color.cyan);
        } else if (c1>100 && c1<=200){
            this.lblCaudal1.setText("CAUDAL MEDIO");
            this.lblCaudal1.setBackground(Color.green);
        } else {
            this.lblCaudal1.setText("CAUDAL ALTO");           
            this.lblCaudal1.setBackground(Color.orange);
        }
        // Mostramos el nivel del caudal de la compuerta 2
        if (c2==0){
            // El caudal es 0 -> la compuerta está cerrada
            this.lblCaudal2.setText("CERRADA");
            this.lblCaudal2.setBackground(Color.white);               
        }else if (c2<=100){
            this.lblCaudal2.setText("CAUDAL BAJO");
            this.lblCaudal2.setBackground(Color.cyan);
        } else if (c2>100 && c2<=200){
            this.lblCaudal2.setText("CAUDAL MEDIO");
            this.lblCaudal2.setBackground(Color.green);
        } else {
            this.lblCaudal2.setText("CAUDAL ALTO");
            this.lblCaudal2.setBackground(Color.orange);
        }
        // Mostramos el nivel del caudal de la compuerta 3
        if (c3==0){
            // El caudal es 0 -> la compuerta está cerrada
            this.lblCaudal3.setText("CERRADA");
            this.lblCaudal3.setBackground(Color.white);               
        }else if (c3<=100){
            this.lblCaudal3.setText("CAUDAL BAJO");
            this.lblCaudal3.setBackground(Color.cyan);
        } else if (c3>100 && c3<=200){
            this.lblCaudal3.setText("CAUDAL MEDIO");
            this.lblCaudal3.setBackground(Color.green);
        } else {
            this.lblCaudal3.setText("CAUDAL ALTO");           
            this.lblCaudal3.setBackground(Color.orange);
        }
        // Mostramos el nivel del caudal general
        if (this.caudalTotal==0){
            // El caudal es 0 -> todas las compuertas están cerradas
            this.lblCaudal.setText("EMBALSE CERRADO");
        }else if (this.caudalTotal<=100){
            this.lblCaudal.setText("CAUDAL BAJO");
        } else if (this.caudalTotal>100 && this.caudalTotal<=200){
            this.lblCaudal.setText("CAUDAL MEDIO");
        } else {
            this.lblCaudal.setText("CAUDAL ALTO");           
        }

    }
   
// Escuchador: Método que se ejecuta cuando se registra una acción
    @Override
    public void actionPerformed(ActionEvent e) {
        // Comprobamos que el evento que ha producido la acción sea del tipo JButton
        if (e.getSource() instanceof JButton){
            // Comprobamos que botón ha sido el que ha producido la acción
            JButton boton = (JButton) e.getSource();
            switch (boton.getName()){
            case "btnActualizar":
                actualizarCaudal();
                break;
            }
        }       
    }

// Escuchador de cambio de los JSpinner: Método que se ejecuta cuando se registra un cambio en el estado
    @Override
    public void stateChanged(ChangeEvent e) {
        // Comprobamos que el componente que ha cambiado de estado sea del tipo JSpinner
        if (e.getSource() instanceof JSpinner){
            JSpinner spinner = (JSpinner) e.getSource();
            // Comprobamos que JSpinner ha sido el que ha cambiado de estado
            switch (spinner.getName()){
            case "spCompuerta1":
                int c1 = (Integer) spCompuerta1.getValue();
                if (c1==0){
                    this.lblCaudal1.setText("CERRADA");
                    this.lblCaudal1.setBackground(Color.white);               
                }else if (c1<=100){
                    this.lblCaudal1.setText("CAUDAL BAJO");
                    this.lblCaudal1.setBackground(Color.cyan);
                } else if (c1>100 && c1<=200){
                    this.lblCaudal1.setText("CAUDAL MEDIO");
                    this.lblCaudal1.setBackground(Color.green);
                } else {
                    this.lblCaudal1.setText("CAUDAL ALTO");
                    this.lblCaudal1.setBackground(Color.orange);
                }
                break;
            case "spCompuerta2":
                int c2 = (Integer) spCompuerta2.getValue();
                if (c2==0){
                    this.lblCaudal2.setText("CERRADA");
                    this.lblCaudal2.setBackground(Color.white);               
                }else if (c2<=100){
                    this.lblCaudal2.setText("CAUDAL BAJO");
                    this.lblCaudal2.setBackground(Color.cyan);
                } else if (c2>100 && c2<=200){
                    this.lblCaudal2.setText("CAUDAL MEDIO");
                    this.lblCaudal2.setBackground(Color.green);
                } else {
                    this.lblCaudal2.setText("CAUDAL ALTO");           
                    this.lblCaudal2.setBackground(Color.orange);
                }
                break;
            case "spCompuerta3":
                int c3 = (Integer) spCompuerta3.getValue();
                if (c3==0){
                    this.lblCaudal3.setText("CERRADA");
                    this.lblCaudal3.setBackground(Color.white);               
                }else if (c3<=100){
                    this.lblCaudal3.setText("CAUDAL BAJO");
                    this.lblCaudal3.setBackground(Color.cyan);
                } else if (c3>100 && c3<=200){
                    this.lblCaudal3.setText("CAUDAL MEDIO");
                    this.lblCaudal3.setBackground(Color.green);
                } else {
                    this.lblCaudal3.setText("CAUDAL ALTO");           
                    this.lblCaudal3.setBackground(Color.orange);
                }
                break;
            }
        }
    }

// Getter del atributo caudalTotal
    public int getCaudalTotal() {
        return caudalTotal;
    }

// Setter del atributo caudalTotal
    // es private porque no queremos que sea accesible desde fuera del objeto
    private void setCaudalTotal(int caudalTotal) {
        this.caudalTotal = caudalTotal;
    }
}

No hay comentarios: