com.jimaginary.machine.graph.params.BernoulliParamPropertyPanel.java Source code

Java tutorial

Introduction

Here is the source code for com.jimaginary.machine.graph.params.BernoulliParamPropertyPanel.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 com.jimaginary.machine.graph.params;

import com.jimaginary.machine.graph.viewer.ParameterPropertyEditor;
import com.jimaginary.machine.math.Bernoulli;
import com.jimaginary.machine.math.MathFunction;
import com.jimaginary.machine.math.MathFunctionFactory;
import com.jimaginary.machine.math.Poisson;
import java.awt.BorderLayout;
import java.text.ParseException;
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.DefaultCategoryDataset;

/**
 *
 * @author simonkenny
 */
public class BernoulliParamPropertyPanel extends javax.swing.JPanel {
    private final int PARAM_PROB = 0;

    String[] idxNames;
    private final MathFunction mathFunc;

    ParameterPropertyEditor editor;
    ChartPanel chartPanel;

    /**
     * Creates new form BernoulliParamPropertyPanel
     */
    public BernoulliParamPropertyPanel(String[] idxNames, MathFunction mathFunc, ParameterPropertyEditor editor) {
        initComponents();
        this.idxNames = idxNames;
        this.editor = editor;
        this.mathFunc = mathFunc;
        jTextFieldAProb.setText("" + mathFunc.getParameter(PARAM_PROB));
        jTextFieldBProb.setText("" + (1.f - mathFunc.getParameter(PARAM_PROB)));
        jScrollBarProb.setValue((int) ((mathFunc.getParameter(PARAM_PROB)) * 100.f));
        initChart();
    }

    private void initChart() {
        // BAR CHART
        DefaultCategoryDataset chartData = new DefaultCategoryDataset();
        if (idxNames != null) {
            chartData.setValue(mathFunc.getParameter(PARAM_PROB), mathFunc.getParamName(0), idxNames[0]);
            chartData.setValue(1.f - mathFunc.getParameter(PARAM_PROB), mathFunc.getParamName(0), idxNames[1]);
        } else {
            chartData.setValue(mathFunc.getParameter(PARAM_PROB), mathFunc.getParamName(0), "0");
            chartData.setValue(1.f - mathFunc.getParameter(PARAM_PROB), mathFunc.getParamName(0), "1");
        }
        JFreeChart chart = ChartFactory.createBarChart("Bernoulli Distribution (PMF)",
                mathFunc.getParamName(PARAM_PROB), "probability", chartData, //Chart Data 
                PlotOrientation.VERTICAL, // orientation
                true, // include legend?
                true, // include tooltips?
                false // include URLs?
        );

        if (chartPanel != null) {
            jPanelChart.remove(chartPanel);
        }
        chartPanel = new ChartPanel(chart);
        jPanelChart.setLayout(new java.awt.BorderLayout());
        jPanelChart.add(chartPanel, BorderLayout.CENTER);
        jPanelChart.validate();
    }

    /**
     * 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();
        jPanelChart = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jTextFieldAProb = new javax.swing.JTextField();
        jScrollBarProb = new javax.swing.JScrollBar();
        jLabel3 = new javax.swing.JLabel();
        jTextFieldBProb = new javax.swing.JTextField();

        setBackground(new java.awt.Color(255, 255, 255));

        org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle
                .getMessage(BernoulliParamPropertyPanel.class, "BernoulliParamPropertyPanel.jLabel1.text")); // NOI18N

        jPanelChart.setBackground(new java.awt.Color(204, 204, 204));
        jPanelChart.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        javax.swing.GroupLayout jPanelChartLayout = new javax.swing.GroupLayout(jPanelChart);
        jPanelChart.setLayout(jPanelChartLayout);
        jPanelChartLayout.setHorizontalGroup(jPanelChartLayout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 0, Short.MAX_VALUE));
        jPanelChartLayout.setVerticalGroup(jPanelChartLayout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 196, Short.MAX_VALUE));

        org.openide.awt.Mnemonics.setLocalizedText(jLabel2, org.openide.util.NbBundle
                .getMessage(BernoulliParamPropertyPanel.class, "BernoulliParamPropertyPanel.jLabel2.text")); // NOI18N

        jTextFieldAProb.setText(org.openide.util.NbBundle.getMessage(BernoulliParamPropertyPanel.class,
                "BernoulliParamPropertyPanel.jTextFieldAProb.text")); // NOI18N
        jTextFieldAProb.setEnabled(false);

        jScrollBarProb.setMaximum(110);
        jScrollBarProb.setOrientation(javax.swing.JScrollBar.HORIZONTAL);
        jScrollBarProb.addAdjustmentListener(new java.awt.event.AdjustmentListener() {
            public void adjustmentValueChanged(java.awt.event.AdjustmentEvent evt) {
                jScrollBarProbAdjustmentValueChanged(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(jLabel3, org.openide.util.NbBundle
                .getMessage(BernoulliParamPropertyPanel.class, "BernoulliParamPropertyPanel.jLabel3.text")); // NOI18N

        jTextFieldBProb.setText(org.openide.util.NbBundle.getMessage(BernoulliParamPropertyPanel.class,
                "BernoulliParamPropertyPanel.jTextFieldBProb.text")); // NOI18N
        jTextFieldBProb.setEnabled(false);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addContainerGap().addGroup(layout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jPanelChart, javax.swing.GroupLayout.Alignment.TRAILING,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
                                Short.MAX_VALUE)
                        .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
                                Short.MAX_VALUE)
                        .addGroup(layout.createSequentialGroup()
                                .addGroup(
                                        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                                .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 61,
                                                        Short.MAX_VALUE)
                                                .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(
                                        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                                .addComponent(jTextFieldAProb, javax.swing.GroupLayout.DEFAULT_SIZE,
                                                        73, Short.MAX_VALUE)
                                                .addComponent(jTextFieldBProb))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jScrollBarProb, javax.swing.GroupLayout.DEFAULT_SIZE, 272,
                                        Short.MAX_VALUE)))
                        .addContainerGap()));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addContainerGap().addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanelChart, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jLabel2).addComponent(jTextFieldAProb,
                                                javax.swing.GroupLayout.PREFERRED_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addComponent(jScrollBarProb, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jTextFieldBProb, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel3))
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));
    }// </editor-fold>//GEN-END:initComponents

    private void jScrollBarProbAdjustmentValueChanged(java.awt.event.AdjustmentEvent evt) {//GEN-FIRST:event_jScrollBarProbAdjustmentValueChanged
        System.out.println("scroll val:" + jScrollBarProb.getValue());
        mathFunc.setParameter(PARAM_PROB, ((float) jScrollBarProb.getValue()) / 100.f);
        jTextFieldAProb.setText("" + mathFunc.getParameter(PARAM_PROB));
        jTextFieldBProb.setText("" + (1.f - mathFunc.getParameter(PARAM_PROB)));
        editor.setAsText(mathFunc.toString());
        initChart();
    }//GEN-LAST:event_jScrollBarProbAdjustmentValueChanged

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanelChart;
    private javax.swing.JScrollBar jScrollBarProb;
    private javax.swing.JTextField jTextFieldAProb;
    private javax.swing.JTextField jTextFieldBProb;
    // End of variables declaration//GEN-END:variables
}