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 ar.com.mantinanmatias.calculadoraderivadas; import javax.swing.JFrame; import javax.swing.JOptionPane; 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.xy.XYSeries; import org.jfree.data.xy.XYSeriesCollection; import org.lsmp.djep.djep.DJep; import static org.math.array.DoubleArray.increment; import org.math.plot.Plot2DPanel; import org.math.plot.canvas.Plot2DCanvas; import org.nfunk.jep.JEP; import org.nfunk.jep.Node; import org.nfunk.jep.ParseException; /** * * @author katana */ public class Interface extends javax.swing.JFrame { DJep djepObject; Node node_funcion; Node diff_simplificada; /** * Creates new form Interface */ public Interface() { initComponents(); djepObject = new DJep(); djepObject.addStandardFunctions(); djepObject.addStandardConstants(); djepObject.addComplex(); djepObject.setAllowUndeclared(true); djepObject.setAllowAssignment(true); djepObject.setImplicitMul(true); djepObject.addStandardDiffRules(); } /** * 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(); jLabel2 = new javax.swing.JLabel(); btnDerivar = new javax.swing.JButton(); txtFuncion = new javax.swing.JTextField(); txtDerivada = new javax.swing.JTextField(); jButton1 = new javax.swing.JButton(); jSeparator1 = new javax.swing.JSeparator(); txtDesde = new javax.swing.JTextField(); txtHasta = new javax.swing.JTextField(); jLabel3 = new javax.swing.JLabel(); jLabel4 = new javax.swing.JLabel(); jSeparator2 = new javax.swing.JSeparator(); jLabel5 = new javax.swing.JLabel(); txtIntervalo = new javax.swing.JTextField(); jMenuBar1 = new javax.swing.JMenuBar(); jMenu1 = new javax.swing.JMenu(); jMenuLimpias = new javax.swing.JMenuItem(); jMenuSalir = new javax.swing.JMenuItem(); jMenu2 = new javax.swing.JMenu(); jMenuDesarrollador = new javax.swing.JMenuItem(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT); jLabel1.setText("Funcin: f(x)="); jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT); jLabel2.setText("Derivada: f'(x)="); btnDerivar.setText("Derivar"); btnDerivar.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnDerivarActionPerformed(evt); } }); txtFuncion.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { txtFuncionActionPerformed(evt); } }); txtFuncion.addKeyListener(new java.awt.event.KeyAdapter() { public void keyTyped(java.awt.event.KeyEvent evt) { txtFuncionKeyTyped(evt); } }); txtDerivada.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { txtDerivadaActionPerformed(evt); } }); jButton1.setText("Graficar"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); jLabel3.setText("Desde:"); jLabel4.setText("Hasta:"); jLabel5.setText("Intervalo:"); jMenu1.setText("Archivo"); jMenuLimpias.setText("Limpiar"); jMenuLimpias.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jMenuLimpiasActionPerformed(evt); } }); jMenu1.add(jMenuLimpias); jMenuSalir.setText("Salir"); jMenu1.add(jMenuSalir); jMenuBar1.add(jMenu1); jMenu2.setText("Informacin"); jMenuDesarrollador.setText("Desarrollador"); jMenuDesarrollador.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jMenuDesarrolladorActionPerformed(evt); } }); jMenu2.add(jMenuDesarrollador); jMenuBar1.add(jMenu2); setJMenuBar(jMenuBar1); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(txtDerivada)) .addGroup(layout.createSequentialGroup() .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(txtFuncion)))) .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup() .addGap(24, 24, 24) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.TRAILING) .addGroup(layout.createSequentialGroup().addComponent(jLabel3) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(txtDesde, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jLabel4) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(txtHasta, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jLabel5) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(txtIntervalo, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent(jSeparator2) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout .createSequentialGroup().addGap(0, 0, Short.MAX_VALUE) .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent(btnDerivar, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))))) .addContainerGap())); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(txtFuncion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel1)) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel2).addComponent(txtDerivada, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(txtDesde, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(txtHasta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel3).addComponent(jLabel4).addComponent(jLabel5) .addComponent(txtIntervalo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jButton1).addComponent(btnDerivar)) .addContainerGap())); pack(); }// </editor-fold>//GEN-END:initComponents private void btnDerivarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDerivarActionPerformed try { txtDerivada.setText(Derivar(txtFuncion.getText())); } catch (ParseException e) { System.out.println(e.getMessage()); } }//GEN-LAST:event_btnDerivarActionPerformed private void txtFuncionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtFuncionActionPerformed // TODO add your handling code here: }//GEN-LAST:event_txtFuncionActionPerformed private void txtDerivadaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtDerivadaActionPerformed // TODO add your handling code here: }//GEN-LAST:event_txtDerivadaActionPerformed private void jMenuLimpiasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuLimpiasActionPerformed txtDerivada.setText(""); txtFuncion.setText(""); }//GEN-LAST:event_jMenuLimpiasActionPerformed private void jMenuDesarrolladorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuDesarrolladorActionPerformed JOptionPane.showMessageDialog(null, "Este software fue creado por Matas Mantian", "Informacin del desarrollador", JOptionPane.INFORMATION_MESSAGE); }//GEN-LAST:event_jMenuDesarrolladorActionPerformed private void txtFuncionKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtFuncionKeyTyped }//GEN-LAST:event_txtFuncionKeyTyped private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed double desde = 0d; double hasta = 0d; double intervalo = 0d; boolean correctImput = false; try { desde = Double.parseDouble(txtDesde.getText()); hasta = Double.parseDouble(txtHasta.getText()); intervalo = Double.parseDouble(txtIntervalo.getText()); correctImput = true; } catch (Exception e) { JOptionPane.showMessageDialog(null, ("Error: " + e.getMessage()), "Error", JOptionPane.ERROR_MESSAGE); } String funcion = txtFuncion.getText(); try { String derivada = Derivar(funcion); if (correctImput) { GraficarFuncion(funcion, node_funcion, desde, hasta, intervalo); GraficarFuncion(derivada, diff_simplificada, desde, hasta, intervalo); } } catch (ParseException e) { JOptionPane.showMessageDialog(null, ("Error: " + e.getMessage()), "Error", JOptionPane.ERROR_MESSAGE, null); } }//GEN-LAST:event_jButton1ActionPerformed /** * @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(Interface.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(Interface.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(Interface.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(Interface.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 Interface().setVisible(true); } }); } public void GraficarFuncion(String funcion, Node nodo_funcion, double desde, double hasta, double intervalo) throws ParseException { XYSeries seriesPuntos = new XYSeries("Puntos"); double[] valores_x = increment(desde, intervalo, hasta); double[] valores_y = ObtenerValoresFuncion(valores_x, seriesPuntos, funcion, nodo_funcion); XYSeriesCollection dataset = new XYSeriesCollection(seriesPuntos); JFreeChart graficoXY = ChartFactory.createXYLineChart(funcion, "x", "y", dataset, PlotOrientation.VERTICAL, true, true, false); ChartPanel panelGrafico = new ChartPanel(graficoXY); //Plot2DPanel panelGrafico= new Plot2DPanel(); // panelGrafico.addLinePlot(funcion, valores_x, valores_y); JFrame ventanaGrafico = new JFrame("Grfico"); ventanaGrafico.setSize(500, 500); ventanaGrafico.setContentPane(panelGrafico); ventanaGrafico.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); ventanaGrafico.setLocationRelativeTo(null); ventanaGrafico.setVisible(true); } public double[] ObtenerValoresFuncion(double[] valores_x, XYSeries seriesPuntos, String funcion, Node nodo_funcion) throws ParseException { double[] valores_y = new double[valores_x.length]; for (int i = 0; i < valores_x.length; i++) { djepObject.addVariable("x", valores_x[i]); valores_y[i] = (double) djepObject.evaluate(nodo_funcion); seriesPuntos.add(valores_x[i], valores_y[i]); System.out.println(valores_x[i] + "," + valores_y[i]); } return valores_y; } public String Derivar(String funcion) throws ParseException { String derivada = ""; // djepObject = new DJep(); // djepObject.addStandardFunctions(); // djepObject.addStandardConstants(); // djepObject.addComplex(); // djepObject.setAllowUndeclared(true); // djepObject.setAllowAssignment(true); // djepObject.setImplicitMul(true); // djepObject.addStandardDiffRules(); // djepObject.addVariable("x", 10); // // node_funcion = djepObject.parse(funcion); // Node diff = djepObject.differentiate(node_funcion,"x"); // diff_simplificada = djepObject.simplify(diff); // // Node diff = djepObject.differentiate(node_funcion, "x"); diff_simplificada = djepObject.simplify(diff); derivada = djepObject.toString(diff_simplificada); // GraficarFuncion(funcion, node_funcion); return derivada; } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton btnDerivar; private javax.swing.JButton jButton1; 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.JMenu jMenu1; private javax.swing.JMenu jMenu2; private javax.swing.JMenuBar jMenuBar1; private javax.swing.JMenuItem jMenuDesarrollador; private javax.swing.JMenuItem jMenuLimpias; private javax.swing.JMenuItem jMenuSalir; private javax.swing.JSeparator jSeparator1; private javax.swing.JSeparator jSeparator2; private javax.swing.JTextField txtDerivada; private javax.swing.JTextField txtDesde; private javax.swing.JTextField txtFuncion; private javax.swing.JTextField txtHasta; private javax.swing.JTextField txtIntervalo; // End of variables declaration//GEN-END:variables }