/*
 * -------------------------------------------------------------------------
 *    $Id: Area.java,v 1.7 2004/09/01 18:05:26 estewart Exp $
 * -------------------------------------------------------------------------
 *    Copyright (c) 1999 Visual Numerics Inc. All Rights Reserved.
 *
 *    This software is confidential information which is proprietary to
 *    and a trade secret of Visual Numerics, Inc.  Use, duplication or
 *    disclosure is subject to the terms of an appropriate license
 *    agreement.
 *
 *    VISUAL NUMERICS MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE
 *    SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING
 *    BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY,
 *    FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. VISUAL
 *    NUMERICS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 *    AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR
 *    ITS DERIVATIVES.
 *--------------------------------------------------------------------------
 */

package com.imsl.demo.javagrande;
import com.imsl.chart.*;
import com.imsl.math.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;


public class Area extends Applet
implements MouseListener, MouseMotionListener {
    private Chart    chart = null;
    private AxisXY    axis;
    static private final int npoints = 100;
    private double    x[], y[];
    private double    upperLimit = 2.0;
    private Data    dataLow, dataHigh;
    private Data    dataValue;
    private Data    dataError;
    Quadrature.Function f;
    
    private void solve() {
        Quadrature q = new Quadrature();
        q.setRule(1);
        double value = q.eval(f, 0.0, upperLimit);
        dataValue.setTitle("integral = "+value);
    }
    
    
    public void init() {
        f = new Quadrature.Function() {
            public double f(double x) {
                return 1.0/(1.0+x*x);
            }
        };
        
        x = new double[npoints];
        y = new double[npoints];
        for (int k = 0;  k < npoints;  k++) {
            x[k] = 10.0*k / (npoints-1);
            y[k] = f.f(x[k]);
        }
        
        chart = new Chart(this);
        axis = new AxisXY(chart);
        axis.getAxisX().getAxisTitle().setTitle("x");
        axis.getAxisY().getAxisTitle().setTitle("y");
        
        double px[] = {5};
        double py[] = {0.5};
        dataValue = new Data(axis, px, py);
        dataValue.setLabelType(Data.LABEL_TYPE_TITLE);
        dataValue.setTitle("integral = ");
        dataValue.setTextColor(Color.blue);
        
        double ex[] = {5};
        double ey[] = {0.4};
        dataError = new Data(axis, ex, ey);
        dataError.setLabelType(Data.LABEL_TYPE_TITLE);
        dataError.setTextColor(Color.red);
        
        updateUpperLimit();
        
        addMouseListener(this);
        addMouseMotionListener(this);
    }
    
    
    public void update(Graphics g) {
        chart.update(g);
    }
    
    public void paint(Graphics g) {
        chart.paint(g);
    }
    
    public void mouseMoved(MouseEvent event) {
    }
    
    public void mouseDragged(MouseEvent event) {
        double point[] = {0, 0};
        axis.mapDeviceToUser(event.getX(), event.getY(), point);
        upperLimit = point[0];
        updateUpperLimit();
    }
    
    public void mouseClicked(MouseEvent event) {
        double point[] = {0, 0};
        axis.mapDeviceToUser(event.getX(), event.getY(), point);
        upperLimit = point[0];
        updateUpperLimit();
    }
    
    private void updateUpperLimit() {
        int        nSplit;
        for (nSplit = 0;  nSplit < npoints-1;  nSplit++) {
            if (x[nSplit] > upperLimit)  break;
        }
        
        int nLow = Math.min(npoints, nSplit+1);
        double xs[] = new double[nLow];
        double ys[] = new double[nLow];
        System.arraycopy(x, 0, xs, 0, nLow);
        System.arraycopy(y, 0, ys, 0, nLow);
        if (dataLow != null)
            dataLow.remove();
        dataLow = new Data(axis, xs, ys);
        dataLow.setDataType(Data.DATA_TYPE_FILL);
        dataLow.setFillColor(Color.blue);
        
        int nHigh = Math.min(npoints, npoints-nSplit);
        xs = new double[nHigh];
        ys = new double[nHigh];
        System.arraycopy(x, nSplit, xs, 0, nHigh);
        System.arraycopy(y, nSplit, ys, 0, nHigh);
        if (dataHigh != null)
            dataHigh.remove();
        dataHigh = new Data(axis, xs, ys);
        dataHigh.setDataType(Data.DATA_TYPE_FILL);
        dataHigh.setFillColor(Color.yellow);
        
        solve();
        dataError.setTitle("");
        repaint();
    }
    
    public void mousePressed(MouseEvent event) {
    }
    
    public void mouseReleased(MouseEvent event) {
    }
    
    public void mouseEntered(MouseEvent event) {
    }
    
    public void mouseExited(MouseEvent event) {
    }
    
    
    public static void main(String argv[]) {
        Area ex = null;
        
        Frame frame = new Frame();
        ex = new Area();
        frame.add(ex);
        ex.init();
        frame.setSize(new Dimension(400,400));
        frame.show();
    }
    
}