/*
 * An interactive test of the Graphic routines.
 * Can be run either as a standalone application by
 * typing "java LastDemo" or as an applet in the AppletViewer.
 */

//package pic20a;

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.text.DecimalFormat;

public class LastDemo extends Applet {
    LastControls controls;
    LastCanvas canvas;
    
    public void init() {
        setBackground(Color.white);
        setLayout(new BorderLayout());
        add("North", new Label("Y = X * sin (1 / (X ^ A))",Label.CENTER));
        add("Center", canvas = new LastCanvas());
        add("South", controls = new LastControls(canvas));
    }
    
    public void destroy() {
        remove(controls);
        remove(canvas);
    }
    
    public void start() {
        controls.setEnabled(true);
    }
    
    public void stop() {
        controls.setEnabled(false);
    }
    
    public void processEvent(AWTEvent e) {
        if (e.getID() == Event.WINDOW_DESTROY) {
            System.exit(0);
        }
    }
    
    public static void main(String args[]) {
        Frame lastFrame = new Frame("LastDemo");
        lastFrame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        
        LastDemo lastDemo = new LastDemo();        
        lastDemo.init();
        lastDemo.start();
        
        lastFrame.add("Center", lastDemo);
        lastFrame.setSize(400, 400);
        lastFrame.show();
    }
    
    public String getAppletInfo() {
        return "An interactive test of the Graphic routines. Can be run \neither as a standalone application by typing 'java LastDemo' \nor as an applet in the AppletViewer.";
    }
}


class LastCanvas extends Canvas {
    Rectangle r = null;
    DecimalFormat sci = new DecimalFormat("0.00000");
    boolean plot = false;
    double aValue = 1.0;
    double xMax = 0.5;
    
    double f(double x, double a) {
        return (x * Math.sin(Math.pow(x, 0 - a)));
    }
    
    public void paint(Graphics g) {
        r = getBounds();
        int xAxis = (r.height - 1) / 2;
        int yAxis = (r.width - 1) / 2;
        double xStep = 2 * xMax / (r.width - 1);
        double yStep = 2 * xMax / (r.height -1);    //since y-range is x-range
        int z;
        
        if (!plot) {
            g.clearRect(0, 0, r.width, r.height);
        }
        
        g.setColor(Color.black);
        g.drawLine(0, xAxis, r.width, xAxis);   //x axis
        g.drawLine(yAxis, 0, yAxis, r.height);  //y axis
        g.drawString("0", yAxis + 5, xAxis - 5);
        
        g.setColor(Color.pink);
        for (z = 1; z <= (xAxis / 10); z++) {
            g.drawLine(0, xAxis - z * 10, r.width, xAxis - z * 10);
            g.drawLine(0, xAxis + z * 10, r.width, xAxis + z * 10);
        }
        for (z = 1; z <= (yAxis / 10); z++) {
            g.drawLine(yAxis - z * 10, 0, yAxis - z * 10, r.height);
            g.drawLine(yAxis + z * 10, 0, yAxis + z * 10, r.height);
        }
        
        g.setColor(Color.black);
        int sx = 5;
        int sy = r.height - 28;
        g.drawString("X scale = " + sci.format(10 * xStep), sx, sy);
        g.drawString("Y scale = " + sci.format(10 * yStep), sx, sy + 14);
        
        if (plot) {
            g.setColor(Color.blue);
            int y1, y2;
            double x1, x2;
            for (z = 1; z < r.width; z++) {
                x1 = (z - 1) * xStep - xMax;
                x2 = z * xStep - xMax;
                y1 = (int) (xAxis - f(x1, aValue) / yStep );
                y2 = (int) (xAxis - f(x2, aValue) / yStep );
                g.drawLine(z - 1, y1, z, y2);
            }
        }
        
       
    }
    
    public void redraw(boolean plot, double a, double x) {
        this.plot = plot;
        this.aValue = a;
        this.xMax = x;
        repaint();
    }
}


class LastControls extends Panel implements ActionListener {
    TextField aValueInput;
    TextField xRangeInput;
    LastCanvas canvas;
    
    public LastControls(LastCanvas canvas) {
        this.canvas = canvas;
        Button btn = null;
        setLayout(new GridLayout(2, 3));
        btn = new Button("Plot");
        btn.addActionListener(this);
        add(btn);
        add(new Label("-10 < A < 10 : ", Label.RIGHT));
        add(aValueInput = new TextField("1.0", 4));      
        btn = new Button("Clear");
        btn.addActionListener(this);
        add(btn);
        add(new Label("0 <= max X : ", Label.RIGHT));
        add(xRangeInput = new TextField("0.5", 4));       
    }
    
    public void actionPerformed(ActionEvent evt) {
        String label = evt.getActionCommand();
        boolean plot = label.equals("Plot");
        double a = 0, x = 0;
        try {
            a = Double.valueOf(aValueInput.getText().trim()).doubleValue();
            x = Double.valueOf(xRangeInput.getText().trim()).doubleValue();
            if (x < 0) throw new Exception();
        } catch (Exception e) {
            plot = false;
            a = x = 0;
        }
        canvas.redraw(plot, a, x);
    }
}

