Tela.java Source code

Java tutorial

Introduction

Here is the source code for Tela.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

import java.awt.BorderLayout;
import java.util.ArrayList;
import java.util.List;
import javax.swing.GroupLayout;
import javax.swing.JOptionPane;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyledDocument;
import org.jfree.chart.ChartPanel;

/**
 *
 * @author Willian
 */
public class Tela extends javax.swing.JFrame {

    private Algoritmo algoritmo = new Algoritmo();
    private Grafico grafico;
    private String msgValidacao;

    /**
     * Creates new form Tela
     */
    public Tela() {
        initComponents();
        jPanel2.setLayout(new BorderLayout());
        exibeGrafico(new ArrayList<Double>());

        this.setVisible(true);

        StyledDocument doc = txtArtigos.getStyledDocument();
        SimpleAttributeSet center = new SimpleAttributeSet();
        doc.setParagraphAttributes(0, doc.getLength(), center, 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">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtArtigos = new javax.swing.JTextPane();
        jLabel1 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        lblPeso = new javax.swing.JLabel();
        lblVolume = new javax.swing.JLabel();
        lblValor = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        btExecutar = new javax.swing.JButton();
        txtPctMutacao = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        txtQtdSelecaoPais = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        txtNumeroIteracoes = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtNumeroIndividuosInicial = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Problema da mochila utilizando algoritmos genticos");
        setResizable(false);

        jPanel2.setBorder(
                javax.swing.BorderFactory.createTitledBorder("Grfico da evoluo da mdia da fitness"));

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(jPanel2Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 0, Short.MAX_VALUE));
        jPanel2Layout.setVerticalGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 323, Short.MAX_VALUE));

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Melhor resultado"));

        txtArtigos.setEditable(false);
        txtArtigos.setFont(new java.awt.Font("Lucida Console", 0, 12)); // NOI18N
        jScrollPane1.setViewportView(txtArtigos);

        jLabel1.setText("PESO:");
        jLabel1.setToolTipText("");

        jLabel6.setText("VOLUME:");

        jLabel7.setText("VALOR:");

        jLabel9.setText("ARTIGOS:");
        jLabel9.setToolTipText("");

        lblPeso.setText("0.00");

        lblVolume.setText("0.00");
        lblVolume.setToolTipText("");

        lblValor.setText("0.00");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(jPanel1Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap().addGroup(jPanel1Layout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(
                                jPanel1Layout.createSequentialGroup().addComponent(jScrollPane1).addContainerGap())
                        .addGroup(jPanel1Layout.createSequentialGroup().addComponent(jLabel9).addGap(0, 0,
                                Short.MAX_VALUE))
                        .addGroup(jPanel1Layout.createSequentialGroup().addGap(10, 10, 10).addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(lblPeso, javax.swing.GroupLayout.PREFERRED_SIZE, 70,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18).addComponent(jLabel6)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(lblVolume, javax.swing.GroupLayout.PREFERRED_SIZE, 70,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 52,
                                        Short.MAX_VALUE)
                                .addComponent(jLabel7)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(lblValor, javax.swing.GroupLayout.PREFERRED_SIZE, 70,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(195, 195, 195)))));
        jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap().addComponent(jLabel9)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 43,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(lblPeso, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(lblVolume, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(lblValor, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGroup(jPanel1Layout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jLabel6).addComponent(jLabel7).addComponent(jLabel1)))
                        .addContainerGap()));

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Opes",
                javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
                javax.swing.border.TitledBorder.DEFAULT_POSITION, null, java.awt.Color.black));

        btExecutar.setText("Executar");
        btExecutar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btExecutarActionPerformed(evt);
            }
        });

        txtPctMutacao.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtPctMutacao.setText("5");

        jLabel5.setText("Mutao (%)");

        txtQtdSelecaoPais.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtQtdSelecaoPais.setText("10");

        jLabel4.setText("Seleo Pais (qtd)");

        txtNumeroIteracoes.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtNumeroIteracoes.setText("100");

        jLabel3.setText("Nmero de iteraes");

        txtNumeroIndividuosInicial.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtNumeroIndividuosInicial.setText("20");

        jLabel2.setText("Populao inicial");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(jPanel3Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel3Layout.createSequentialGroup().addContainerGap().addGroup(jPanel3Layout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel3Layout.createSequentialGroup().addGroup(jPanel3Layout
                                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(jPanel3Layout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                        .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING,
                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(txtNumeroIndividuosInicial,
                                                javax.swing.GroupLayout.Alignment.LEADING,
                                                javax.swing.GroupLayout.PREFERRED_SIZE, 97,
                                                javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addComponent(txtNumeroIteracoes, javax.swing.GroupLayout.PREFERRED_SIZE, 95,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(txtQtdSelecaoPais, javax.swing.GroupLayout.PREFERRED_SIZE, 93,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(txtPctMutacao, javax.swing.GroupLayout.PREFERRED_SIZE, 93,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(btExecutar, javax.swing.GroupLayout.PREFERRED_SIZE, 93,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(15, 15, 15))
                        .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addContainerGap()));
        jPanel3Layout.setVerticalGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel3Layout.createSequentialGroup().addGap(55, 55, 55).addComponent(jLabel2)
                        .addGap(3, 3, 3)
                        .addComponent(txtNumeroIndividuosInicial, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18).addComponent(jLabel3).addGap(4, 4, 4)
                        .addComponent(txtNumeroIteracoes, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18).addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtQtdSelecaoPais, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED).addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtPctMutacao, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18).addComponent(btExecutar).addContainerGap(157, Short.MAX_VALUE)));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(19, 19, 19)));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addGroup(layout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(layout.createSequentialGroup().addContainerGap().addComponent(jPanel3,
                                javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
                                javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING,
                                layout.createSequentialGroup().addGap(9, 9, 9)
                                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addContainerGap(22, Short.MAX_VALUE)));

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void btExecutarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btExecutarActionPerformed
        if (validaCampos()) {
            algoritmo.setNumeroIndividuos(Integer.parseInt(txtNumeroIndividuosInicial.getText()));
            algoritmo.setNumeroEpocas(Integer.parseInt(txtNumeroIteracoes.getText()));
            algoritmo.setQuantidadeSelecaoPais(Integer.parseInt(txtQtdSelecaoPais.getText()));
            algoritmo.setPorcentagemMutacao(Integer.parseInt(txtPctMutacao.getText()));
            algoritmo.executa();

            exibeDetalhesMelhorResultado();

            exibeGrafico(algoritmo.getListaValoresGrafico());

        } else {
            JOptionPane.showMessageDialog(null, msgValidacao);
        }
    }//GEN-LAST:event_btExecutarActionPerformed

    private void exibeDetalhesMelhorResultado() {

        Individuo individuo = algoritmo.selecionaMelhorResultado();
        List<Artigo> artigos = algoritmo.getArtigos();

        List<String> artigosMochila = new ArrayList<>();
        for (int i = 0; i < individuo.getCromossomo().size(); i++) {
            if (individuo.getCromossomo().get(i) == 1) {
                artigosMochila.add(artigos.get(i).getNome());
            }
        }
        txtArtigos.setText(artigosMochila.toString());

        lblPeso.setText(individuo.getPeso().toString());
        lblVolume.setText(individuo.getVolume().toString());
        lblValor.setText(individuo.getValor().toString());
    }

    private void exibeGrafico(List<Double> listaValoresGrafico) {
        grafico = new Grafico(listaValoresGrafico);
        jPanel2.removeAll();
        jPanel2.add(grafico.getChartPanel(), BorderLayout.CENTER);
        jPanel2.validate();
    }

    private boolean validaCampos() {
        boolean validou = true;
        msgValidacao = "";

        if (Integer.parseInt(txtQtdSelecaoPais.getText()) % 2 != 0) {
            msgValidacao += "A quantidade de pais deve ser par!\n";
            validou = false;
        }

        if (Integer.parseInt(txtQtdSelecaoPais.getText()) > Integer
                .parseInt(txtNumeroIndividuosInicial.getText())) {
            msgValidacao += "A quantidade de pais no pode ser maior que a quantidade de indivduos!\n";
            validou = false;
        }

        if (Integer.parseInt(txtPctMutacao.getText()) > 100) {
            msgValidacao += "O percentual de mutao no pode ser maior que 100%!\n";
            validou = false;
        }

        return validou;
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btExecutar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblPeso;
    private javax.swing.JLabel lblValor;
    private javax.swing.JLabel lblVolume;
    private javax.swing.JTextPane txtArtigos;
    private javax.swing.JTextField txtNumeroIndividuosInicial;
    private javax.swing.JTextField txtNumeroIteracoes;
    private javax.swing.JTextField txtPctMutacao;
    private javax.swing.JTextField txtQtdSelecaoPais;
    // End of variables declaration//GEN-END:variables
}