/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
import java.text.*;
/*
 * NewJApplet.java
 *
 * Created on Mar 12, 2010, 12:06:40 PM
 */

/**
 *
 * @author root
 */
public class NewJApplet extends javax.swing.JApplet {

    double x, h, y, exval;
    int nmethod, i, j, k, ix, iy;
DecimalFormat df;
    /** Initializes the applet NewJApplet */
    public void init() {
        try {
            java.awt.EventQueue.invokeAndWait(new Runnable() {

                public void run() {
                    initComponents();
                }
            });
        } catch (Exception ex) {
            ex.printStackTrace();
        }


         df = new DecimalFormat("##0.000000");
        jSlider2.setValue(50);
        x = jSlider2.getValue() * Math.PI / 200.0;
        exval = Math.cos(x);
        jLabel1.setText("x=" + df.format(x));
        jSlider1.setValue(1);
        nmethod = 1;

    }

    /** This method is called from within the init() method 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() {

        jPanel1 = new myPanel();
        jSlider1 = new javax.swing.JSlider();
        jSlider2 = new javax.swing.JSlider();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();

        jPanel1.setMaximumSize(new java.awt.Dimension(440, 400));
        jPanel1.setMinimumSize(new java.awt.Dimension(440, 400));
        jPanel1.setPreferredSize(new java.awt.Dimension(440, 400));
        jPanel1.setRequestFocusEnabled(false);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 440, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );

        jSlider1.setMajorTickSpacing(1);
        jSlider1.setMaximum(4);
        jSlider1.setMinimum(1);
        jSlider1.setPaintLabels(true);
        jSlider1.setPaintTicks(true);
        jSlider1.setSnapToTicks(true);
        jSlider1.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseDragged(java.awt.event.MouseEvent evt) {
                jSlider1MouseDragged(evt);
            }
        });

        jSlider2.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseDragged(java.awt.event.MouseEvent evt) {
                jSlider2MouseDragged(evt);
            }
        });

        jLabel1.setText("x");

        jLabel2.setText("order");

        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(jPanel1, 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()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jSlider2, 0, 92, Short.MAX_VALUE)
                            .addComponent(jSlider1, 0, 0, Short.MAX_VALUE)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(13, 13, 13)))
                        .addContainerGap(51, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(16, 16, 16)
                        .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(85, 85, 85)
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jSlider2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(57, 57, 57)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jSlider1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(34, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jSlider2MouseDragged(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jSlider2MouseDragged
        x = jSlider2.getValue() * Math.PI / 200.0;
        jLabel1.setText("x=" + df.format(x));
        exval = Math.cos(x);
        jPanel1.repaint();
    // TODO add your handling code here:
    }//GEN-LAST:event_jSlider2MouseDragged

    private void jSlider1MouseDragged(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jSlider1MouseDragged
        nmethod = jSlider1.getValue();
        jPanel1.repaint();
    // TODO add your handling code here:
    }//GEN-LAST:event_jSlider1MouseDragged
    public double deriv1(double h0, double x) {
// calculate derivative of sin(x)

        double f1, f2;
        f1 = Math.sin(x + h0);
        f2 = Math.sin(x);

        return (f1 - f2) / h0;

    }

    public double deriv2(double h0, double x) {
// calculate derivative of sin(x)
// symmetric difference quotient

        double f1, f2, h;
        h = 0.5 * h0;
        f1 = Math.sin(x + h);
        f2 = Math.sin(x - h);

        return (f1 - f2) / h0;

    }

    public double deriv3(double h0, double x) {
// calculate derivative of sin(x)
// two step widths

        double D0, D1, h1;
        h1 = 0.5 * h0;
        D0 = (Math.sin(x + h0) - Math.sin(x - h0)) * 0.5 / h0;
        D1 = (Math.sin(x + h1) - Math.sin(x - h1)) * 0.5 / h1;

        return (4 * D1 - D0) / 3.0;

    }

    public double deriv4(double h0, double x) {
// calculate derivative of sin(x)
// three step widths

        double D0, D1, D2, h1, h2;
        h1 = 0.5 * h0;
        h2 = 0.5 * h1;

        D0 = (Math.sin(x + h0) - Math.sin(x - h0)) * 0.5 / h0;
        D1 = (Math.sin(x + h1) - Math.sin(x - h1)) * 0.5 / h1;
        D2 = (Math.sin(x + h2) - Math.sin(x - h2)) * 0.5 / h2;

        return (D0 - 20 * D1 + 64 * D2) / 45.0;

    }

    public double derivsel(double h0, double x) {
        double d;
        switch (nmethod) {
            case 1:
                d = deriv1(h0, x);
                break;
            case 2:
                d = deriv2(h0, x);
                break;
            case 3:
                d = deriv3(h0, x);
                break;
            case 4:
                d = deriv4(h0, x);
                break;
            default:
                d = 0;
        }

        return d;

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

    public class myPanel extends javax.swing.JPanel {

        double d, lx, ly;

        public void paintComponent(Graphics g) {


            super.paintComponent(g);

            h = 2.0;
            g.setColor(Color.red);
            for (i = 0; i < 80; i++) {
                h = h * 0.7;
                d = derivsel(h, x);
                lx = Math.log10(h);
                ly = Math.log10(Math.abs(d - exval));
                
                ix = 400 + (int) (lx * 30.0);
                iy = 20 - (int) (ly * 20.0);
                g.fillOval(ix, iy, 5, 5);



            }
            g.setColor(Color.BLACK);

            for (i = 0; i < 13; i++) {

                ix = 400 - i * 30;
                g.drawLine(ix, 20, ix, 320);

                g.drawString("E-" + i, ix - 3, 350);
            }
            for (i = 0; i < 16; i++) {

                iy = 20 + i * 20;
                g.drawLine(40, iy, 400, iy);

                g.drawString("E-" + i, 0, iy);


            }g.drawString("step size h" , 200, 380);
            g.drawString("error of numerical derivative" , 150, 10);
        }
    }}
