/*
 * NewJApplet.java
 *
 * Created on January 23, 2008, 10:25 AM
 */

import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
import java.text.*;

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

    double r_value, x_zero, x_fix, ymin, ymax, sfac, xbas;
    int max_it, iymin, iymax;
    double[] xstore;
    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.00");

        r_value = 3.04;
        x_zero = 0.01;

        jLabel1.setText("r= " + df.format(r_value));
        jLabel3.setText("x0= " + df.format(x_zero));
        jSlider1.setValue((int) (r_value * 25));
        jSlider2.setValue((int) (x_zero * 100));

        max_it = 10000;

        iymin = 0;
        iymax = 0;
        ymin = 100;
        ymax = -100;
        sfac = 1.0;
        xbas = 0;
        xstore = new double[max_it];

        jPanel1.repaint();

    }

    /** 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.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

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

        jPanel1.setMaximumSize(new java.awt.Dimension(300, 500));
        jPanel1.setMinimumSize(new java.awt.Dimension(300, 500));

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

        jLabel1.setText("r-value");

        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);
            }
        });

        jLabel3.setText("r-value");

        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()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jSlider1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel1)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jSlider2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(479, 479, 479))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(218, 218, 218)
                        .addComponent(jLabel3)
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(40, 40, 40)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 429, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jSlider1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jSlider2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel3)))
                .addContainerGap(29, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jSlider1MouseDragged(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jSlider1MouseDragged
        r_value = jSlider1.getValue() * 0.04;
        jLabel1.setText("r= " + df.format(r_value));
        jPanel1.repaint();// TODO add your handling code here:
    }//GEN-LAST:event_jSlider1MouseDragged

    private void jSlider2MouseDragged(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jSlider2MouseDragged
        x_zero = jSlider2.getValue() * 0.01;
        jLabel3.setText("x0= " + df.format(x_zero));

        jPanel1.repaint();
    // TODO add your handling code here:
    }//GEN-LAST:event_jSlider2MouseDragged
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    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 {

        int ix, iy, ix1, iy1, ix2, iy2, i2, i, ixo, iyo;
        double x, y, x1, y1;
        int window_w = 400;

        double fun(double x) {
            double y;
            y = r_value * (x - x * x);
            return (y);
        }

        void initialize() {
            x1 = x_zero;
            y1 = fun(x1);
            xstore[0] = x1;
        }

        public void paintComponent(Graphics g) {

            super.paintComponent(g);


            initialize();



            for (i = 1; i < max_it; i++) {
                xstore[i] = fun(xstore[i - 1]);

            }




            g.setColor(Color.blue);
            for (i = 1; i < max_it; i++) {
                x1 = xstore[i - 1];
                y1 = xstore[i];

                ixo = (int) (x1 *window_w);
                iyo = (int) (y1 * window_w);

                g.drawLine(ixo, window_w - ixo, ixo, window_w - iyo);
                g.drawLine(ixo, window_w - iyo, iyo, window_w - iyo);
            }

            g.setColor(Color.red);
            g.drawLine(0, window_w, window_w, 0);

            for (i = 0; i < 100; i++) {
                x =  i  * 0.01;
                y = fun(x);

                x1 =  (i + 1)  * 0.01;
                y1 = fun(x1);

                ixo = (int) (x * window_w);
                iyo = (int) (y  * window_w);
                ix = (int) (x1 * window_w);
                iy = (int) (y1  * window_w);

                g.drawLine(ixo, window_w - iyo, ix, window_w - iy);

            }

            g.setColor(Color.BLACK);
            g.drawLine(0, 0, 0, window_w);
            g.drawLine(0,window_w,window_w,window_w);

            for(i=0;i<6;i++)
            {
            ix=i*window_w/5;
            g.drawString(""+df.format(i*0.2), ix, window_w+20);

            }


        }
    }
}

