Java tutorial
/* * 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 main; import java.awt.BorderLayout; import java.util.Observable; import java.util.Observer; import monte_carlo.SCMC; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartPanel; import org.jfree.chart.JFreeChart; import org.jfree.chart.axis.NumberAxis; import org.jfree.chart.plot.PlotOrientation; import org.jfree.data.xy.IntervalXYDataset; import org.jfree.data.xy.XYSeries; import org.jfree.data.xy.XYSeriesCollection; /** * * @author Joko */ public class GUI extends javax.swing.JFrame implements Observer { /** * Creates new form GUI */ XYSeries avgSeries, avgSeries2; SCMC carlo; public GUI() { 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() { txtPoceRep = new javax.swing.JTextField(); jLabel1 = new javax.swing.JLabel(); btnStart = new javax.swing.JButton(); tabPanel = new javax.swing.JTabbedPane(); panelLive = new javax.swing.JPanel(); panelHistogram = new javax.swing.JPanel(); panelKrivka = new javax.swing.JPanel(); jLabel2 = new javax.swing.JLabel(); txtZahod = new javax.swing.JTextField(); jLabel3 = new javax.swing.JLabel(); jLabel4 = new javax.swing.JLabel(); jLabel5 = new javax.swing.JLabel(); jLabel6 = new javax.swing.JLabel(); txtPercento = new javax.swing.JTextField(); txtEndProject = new javax.swing.JTextField(); btnVypPrav = new javax.swing.JButton(); btnVypEnd = new javax.swing.JButton(); jScrollPane1 = new javax.swing.JScrollPane(); panePrav = new javax.swing.JTextPane(); jScrollPane2 = new javax.swing.JScrollPane(); paneDni = new javax.swing.JTextPane(); jScrollPane3 = new javax.swing.JScrollPane(); areaVypis = new javax.swing.JTextArea(); jLabel7 = new javax.swing.JLabel(); txtavgName = new javax.swing.JTextField(); jLabel8 = new javax.swing.JLabel(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); txtPoceRep.setText("10000000"); jLabel1.setText("Po?et replikacii"); btnStart.setText("Start"); btnStart.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnStartActionPerformed(evt); } }); tabPanel.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(java.awt.event.MouseEvent evt) { tabPanelMouseClicked(evt); } }); panelLive.setBackground(new java.awt.Color(102, 102, 102)); panelLive.setLayout(new java.awt.BorderLayout()); tabPanel.addTab("Live", panelLive); panelHistogram.setBackground(new java.awt.Color(102, 102, 102)); panelHistogram.setLayout(new java.awt.BorderLayout()); tabPanel.addTab("Histogram", panelHistogram); panelKrivka.setBackground(new java.awt.Color(102, 102, 102)); panelKrivka.setLayout(new java.awt.BorderLayout()); tabPanel.addTab("Krivka", panelKrivka); jLabel2.setText("Zahodi prvch"); txtZahod.setText("1000000"); jLabel3.setText("Ak je ?as trvania projektu ak poadujem"); jLabel4.setText("% pravdepodobnos splnenia tohto termnu"); jLabel5.setText("Ak je pravdepodobnos, e sa projekt podar ukon?i za"); jLabel6.setText("dn?"); txtPercento.setText("80"); txtEndProject.setText("140"); btnVypPrav.setText("Vypo?taj"); btnVypPrav.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnVypPravActionPerformed(evt); } }); btnVypEnd.setText("Vypo?taj"); btnVypEnd.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnVypEndActionPerformed(evt); } }); jScrollPane1.setViewportView(panePrav); jScrollPane2.setViewportView(paneDni); areaVypis.setColumns(20); areaVypis.setRows(5); jScrollPane3.setViewportView(areaVypis); jLabel7.setText("Priemern trvanie projektu"); txtavgName.setText("0"); jLabel8.setText("dn"); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(45, 45, 45).addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(1, 1, 1).addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 226, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGroup(layout.createSequentialGroup().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addGroup(layout.createSequentialGroup().addComponent(jLabel5) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(txtEndProject, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jLabel6)) .addGroup(layout.createSequentialGroup() .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(txtPercento, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGap(124, 124, 124))) .addGap(0, 0, Short.MAX_VALUE))) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addComponent(btnVypPrav) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 297, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup().addComponent(btnVypEnd) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 297, javax.swing.GroupLayout.PREFERRED_SIZE))) .addContainerGap(17, Short.MAX_VALUE)) .addGroup(layout.createSequentialGroup().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addComponent(jLabel1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(txtPoceRep, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup().addComponent(jLabel2) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(txtZahod, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18).addComponent(btnStart, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGap(65, 65, 65).addComponent(jLabel7) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(txtavgName, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))) .addGroup(layout.createSequentialGroup().addGap(295, 295, 295).addComponent(tabPanel) .addContainerGap())); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(69, 69, 69).addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addGroup(layout.createSequentialGroup().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(txtPoceRep, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel1)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel2).addComponent(txtZahod, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))) .addComponent(btnStart, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel7) .addComponent(txtavgName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel8))) .addGap(24, 24, 24) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup( javax.swing.GroupLayout.Alignment.TRAILING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel3).addComponent(jLabel4) .addComponent(txtPercento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(btnVypEnd)) .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.TRAILING, 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.LEADING) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel5).addComponent(jLabel6) .addComponent(txtEndProject, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(btnVypPrav)) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(24, 24, 24).addComponent(tabPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 350, Short.MAX_VALUE)) .addGroup(layout.createSequentialGroup().addGap(33, 33, 33) .addComponent(jScrollPane3))) .addGap(23, 23, 23))); pack(); }// </editor-fold>//GEN-END:initComponents private void btnStartActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnStartActionPerformed areaVypis.setText(""); int pocetRep = Integer.parseInt(txtPoceRep.getText().toString()); int zahod = Integer.parseInt(txtZahod.getText().toString()); carlo = new SCMC(pocetRep, zahod); carlo.addObserver(this); avgSeries = new XYSeries("AVG"); XYSeriesCollection dataset = new XYSeriesCollection(avgSeries); JFreeChart lineChart = ChartFactory.createXYLineChart(null, null, null, dataset); final NumberAxis rangeAxis = (NumberAxis) lineChart.getXYPlot().getRangeAxis(); rangeAxis.setAutoRangeIncludesZero(false); ChartPanel chartPanel = new ChartPanel(lineChart); panelLive.removeAll(); panelLive.add(chartPanel, BorderLayout.CENTER); panelLive.validate(); carlo.simuluj(avgSeries); }//GEN-LAST:event_btnStartActionPerformed private void tabPanelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tabPanelMouseClicked switch (tabPanel.getSelectedIndex()) { case 1: createHistogram(); break; case 2: createKrivka(); break; } }//GEN-LAST:event_tabPanelMouseClicked private void btnVypEndActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnVypEndActionPerformed int dni = carlo.vypDni(Integer.parseInt(txtPercento.getText().toString())); paneDni.setText(String.valueOf(dni)); }//GEN-LAST:event_btnVypEndActionPerformed private void btnVypPravActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnVypPravActionPerformed double prav = carlo.getPrav(Integer.parseInt(txtEndProject.getText().toString())); panePrav.setText(String.valueOf(prav)); }//GEN-LAST:event_btnVypPravActionPerformed private void createHistogram() { IntervalXYDataset dataset = carlo.getDataset(); JFreeChart chart = ChartFactory.createHistogram("Histogram", "Pocet dni", "Vskyt", dataset, PlotOrientation.VERTICAL, true, true, false); ChartPanel myChart = new ChartPanel(chart); myChart.setMouseWheelEnabled(true); panelHistogram.setLayout(new java.awt.BorderLayout()); panelHistogram.add(myChart, BorderLayout.CENTER); panelHistogram.validate(); } public void createKrivka() { XYSeriesCollection dataset = carlo.getDataSetKrivka(); JFreeChart lineChart = ChartFactory.createXYLineChart("Pravdepodobnos splnenia termnu", "Trvanie projektu v doch", "Pravdepodobnos v %", dataset); final NumberAxis rangeAxis = (NumberAxis) lineChart.getXYPlot().getRangeAxis(); rangeAxis.setAutoRangeIncludesZero(false); ChartPanel chartPanel = new ChartPanel(lineChart); panelKrivka.setLayout(new java.awt.BorderLayout()); panelKrivka.add(chartPanel, BorderLayout.CENTER); panelKrivka.validate(); } /** * @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(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(GUI.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 GUI().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JTextArea areaVypis; private javax.swing.JButton btnStart; private javax.swing.JButton btnVypEnd; private javax.swing.JButton btnVypPrav; 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 jLabel8; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JScrollPane jScrollPane2; private javax.swing.JScrollPane jScrollPane3; private javax.swing.JTextPane paneDni; private javax.swing.JTextPane panePrav; private javax.swing.JPanel panelHistogram; private javax.swing.JPanel panelKrivka; private javax.swing.JPanel panelLive; private javax.swing.JTabbedPane tabPanel; private javax.swing.JTextField txtEndProject; private javax.swing.JTextField txtPercento; private javax.swing.JTextField txtPoceRep; private javax.swing.JTextField txtZahod; private javax.swing.JTextField txtavgName; // End of variables declaration//GEN-END:variables @Override public void update(Observable o, Object arg) { areaVypis.setText(carlo.vypis()); txtavgName.setText(String.valueOf(carlo.getAvg())); } }