simuladorruleta.Principal.java Source code

Java tutorial

Introduction

Here is the source code for simuladorruleta.Principal.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 simuladorruleta;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.data.category.DefaultCategoryDataset;

/**
 *
 * @author Agu
 */
public class Principal extends javax.swing.JFrame {

    private Ruleta r;
    private DefaultCategoryDataset data = new DefaultCategoryDataset();

    public Principal() {
        initComponents();
        r = new Ruleta();

    }

    private void resetData() {
        for (int i = 0; i < 37; i++)
            data.addValue(0, "numeros", "" + i);
    }

    private void graficar() {
        ChartPanel panel;
        JFreeChart chart = null;

        /* data.addValue(10, "numeros",""+0);
         data.addValue(30, "numeros",""+1);
         data.addValue(23, "numeros",""+2);*/

        chart = ChartFactory.createBarChart("Repeticiones de los numeros", "Cantidad", "Numero", data);
        CategoryPlot plot = (CategoryPlot) chart.getPlot();
        plot.setDomainGridlinesVisible(true);

        panel = new ChartPanel(chart);
        panel.setBounds(5, 5, 800, 600);

        grafRepe.removeAll();
        grafRepe.add(panel);
        grafRepe.repaint();
        grafRepe.setVisible(true);

    }

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

        numero = new javax.swing.JTextField();
        rotar = new javax.swing.JButton();
        grafRepe = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        numero.setName("numero"); // NOI18N

        rotar.setText("Rotar");
        rotar.setName("rotar"); // NOI18N
        rotar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rotarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout grafRepeLayout = new javax.swing.GroupLayout(grafRepe);
        grafRepe.setLayout(grafRepeLayout);
        grafRepeLayout.setHorizontalGroup(grafRepeLayout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 0, Short.MAX_VALUE));
        grafRepeLayout.setVerticalGroup(grafRepeLayout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 601, Short.MAX_VALUE));

        jLabel1.setText("Cantidad:");

        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()
                        .addComponent(grafRepe, javax.swing.GroupLayout.DEFAULT_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addContainerGap())
                .addGroup(layout.createSequentialGroup().addGap(92, 92, 92).addComponent(jLabel1).addGap(18, 18, 18)
                        .addComponent(numero, javax.swing.GroupLayout.PREFERRED_SIZE, 88,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(33, 33, 33).addComponent(rotar).addContainerGap(487, Short.MAX_VALUE)));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addContainerGap().addGroup(layout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(numero, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(rotar).addComponent(jLabel1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 21, Short.MAX_VALUE)
                        .addComponent(grafRepe, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap()));

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

    private void rotarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rotarActionPerformed
        // TODO add your handling code here:
        if ("".equals(numero.getText())) {
            return;
        }

        int cant = Integer.parseInt(numero.getText());
        int rot;

        this.resetData();

        for (int i = 0; i < cant; i++) {
            rot = r.rotar();
            data.incrementValue(1, "numeros", "" + rot);
        }

        this.graficar();
    }//GEN-LAST:event_rotarActionPerformed

    /**
     * @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(Principal.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Principal.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Principal.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Principal.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 Principal().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel grafRepe;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JTextField numero;
    private javax.swing.JButton rotar;
    // End of variables declaration//GEN-END:variables
}