clienteProfessor.ClickerOne_Resultados.java Source code

Java tutorial

Introduction

Here is the source code for clienteProfessor.ClickerOne_Resultados.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.
 */
package clienteProfessor;

import java.awt.Color;
import java.awt.Font;
import java.util.ArrayList;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import server.*;

/**
 *
 * @author gabri_000
 */
public class ClickerOne_Resultados extends javax.swing.JFrame {

    /**
     * Creates new form ClickerOne_Resultados
     */
    ClickerOne_Cliente cliente = new ClickerOne_Cliente();
    ClickerOne_Resposta Respostas = new ClickerOne_Resposta();
    ClickerOne_Aula aula = new ClickerOne_Aula();

    ArrayList<ClickerOne_Resposta> listaDeResposta = new ArrayList<>();
    ArrayList<ClickerOne_Aula> listaDeAula = new ArrayList<>();
    String aux = "";
    int contador = 1;

    public ClickerOne_Resultados(ClickerOne_Cliente cliente) {
        this.cliente = cliente;
        initComponents();
        listaDeAula = aula.AulasDeUmProfessor(this.cliente.get_RA());
        for (ClickerOne_Aula Aux : listaDeAula) {
            ClickerOne_Aula cadaAula = (ClickerOne_Aula) Aux;
            if (!aux.equals(cadaAula.get_nomeAula())) {
                jComboBoxAulas.addItem(cadaAula.get_nomeAula());
                aux = cadaAula.get_nomeAula();
            }
        }
        this.setTitle("Resultados da aula: " + jComboBoxAulas.getSelectedItem());
        jPanel1.setBackground(Color.WHITE);
    }

    public ClickerOne_Resultados() {
        initComponents();
    }

    /**
     * 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() {

        jLabel1 = new javax.swing.JLabel();
        jComboBoxAulas = new javax.swing.JComboBox();
        jPanel1 = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setMaximumSize(new java.awt.Dimension(800, 550));
        setMinimumSize(new java.awt.Dimension(800, 550));
        setPreferredSize(new java.awt.Dimension(800, 550));
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel1.setFont(new java.awt.Font("Arial", 0, 18)); // NOI18N
        jLabel1.setText("Selecione a aula que deseja ver o resultado:");
        getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 30, 370, 20));

        jComboBoxAulas.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        jComboBoxAulas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBoxAulasActionPerformed(evt);
            }
        });
        getContentPane().add(jComboBoxAulas, new org.netbeans.lib.awtextra.AbsoluteConstraints(400, 30, -1, -1));

        jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jPanel1.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        jPanel1.setMaximumSize(new java.awt.Dimension(765, 440));
        jPanel1.setMinimumSize(new java.awt.Dimension(765, 440));
        jPanel1.setPreferredSize(new java.awt.Dimension(765, 440));
        getContentPane().add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 60, 765, 440));

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

    private void jComboBoxAulasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBoxAulasActionPerformed
        if (!jComboBoxAulas.getSelectedItem().equals(null)) {
            this.setTitle("Resultados da aula: " + jComboBoxAulas.getSelectedItem());
            listaDeResposta = Respostas.getRespostas(jComboBoxAulas.getSelectedItem().toString());
            criaGrafico();
        } else {
            this.setTitle("Resultados");
            criaGrafico();
        }
    }//GEN-LAST:event_jComboBoxAulasActionPerformed

    private CategoryDataset createDataset() {
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        for (ClickerOne_Resposta Aux : listaDeResposta) {
            ClickerOne_Resposta cadaReposta = (ClickerOne_Resposta) Aux;
            dataset.addValue(cadaReposta.get_altA(), "Alternativa A", "Questo " + contador);
            dataset.addValue(cadaReposta.get_altB(), "Alternativa B", "Questo " + contador);
            dataset.addValue(cadaReposta.get_altC(), "Alternativa C", "Questo " + contador);
            dataset.addValue(cadaReposta.get_altD(), "Alternativa D", "Questo " + contador);
            dataset.addValue(cadaReposta.get_altE(), "Alternativa E", "Questo " + contador);
            contador++;
        }
        contador = 1;
        return dataset;
    }

    public void criaGrafico() {
        CategoryDataset cds = createDataset();
        String titulo = "Resultado da aula: " + jComboBoxAulas.getSelectedItem();
        String eixoy = "Quantidade de escolhas";
        String txt_legenda = "Legenda:";
        boolean legenda = true;
        boolean tooltips = true;
        boolean urls = true;
        JFreeChart graf = ChartFactory.createBarChart3D(titulo, txt_legenda, eixoy, cds, PlotOrientation.VERTICAL,
                legenda, tooltips, urls);
        //JFreeChart graf = ChartFactory.createBarChart3D(titulo, txt_legenda, eixoy, cds, PlotOrientation.VERTICAL, legenda, tooltips, urls);
        ChartPanel myChartPanel = new ChartPanel(graf, true);
        myChartPanel.setSize(jPanel1.getWidth(), jPanel1.getHeight());
        myChartPanel.setVisible(true);
        myChartPanel.setFont(new Font("Arial", Font.PLAIN, 25));
        myChartPanel.setBackground(this.getBackground());
        jPanel1.removeAll();
        jPanel1.add(myChartPanel);
        jPanel1.revalidate();
        jPanel1.repaint();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ClickerOne_Resultados.class.getName())
                    .log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ClickerOne_Resultados.class.getName())
                    .log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ClickerOne_Resultados.class.getName())
                    .log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ClickerOne_Resultados.class.getName())
                    .log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ClickerOne_Resultados().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox jComboBoxAulas;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration//GEN-END:variables
}