/*****************************************************************************************************************
 *  Anthony Chahine
 *  2/25/2013
 *  Quadratic Equation Grapher
 *  
 *  Takes 3 inputs from user, variables a, b, and c. Then plugs in those values into the formula and creates a table
 *  of y values. Then graphs the cooresponding x and y values onto a graph using lines.
 *****************************************************************************************************************/
import javax.swing.JApplet;
import javax.swing.JOptionPane;
import java.awt.Graphics;
 
public class Graphs extends JApplet {
 
 //creating data values into integer arrays x and y
 private static int[] yVals = new int[20];  
 private static int[] xVals = { -10,-9,-8,-7,-6,-5,-4,-3,-2,-1,0,1,2,3,4,5,6,7,8,9,10 };
 
public void start(){
 
        String a, b, c = new String();   //Strings entered by user for quadratic equation
        int aParsed, bParsed, cParsed = 0;  //ints to be parsed from user-input strings
        a = JOptionPane.showInputDialog( null, "Enter integer A in Ax^2 + Bx + C = 0", "1" );
        b = JOptionPane.showInputDialog( null, "Enter integer B in Ax^2 + Bx + C = 0", "2" );
        c = JOptionPane.showInputDialog( null, "Enter integer C in Ax^2 + Bx + C = 0", "3" );
 
        aParsed = Integer.parseInt( a );
        bParsed = Integer.parseInt( b );
        cParsed = Integer.parseInt( c );
 
        //Calculate y values and enter them sequentially into array
        for ( int i = 0; i < xVals.length + 1; i ++ ){
            yVals[i] = ( aParsed * ( xVals[i] * xVals[i] ) ) + ( bParsed * xVals[i] ) + cParsed;
        }//end of for loop
 
        /* Convert x and y values into java applet coordinates. This can only be used on a 500/500 APPLET
         * as it uses (250,250) as the origin. Then subtracts or adds corresponding to the proper quadrant I
         * II, III, and IV from 250. 
         * Ex. (3,6) would be ( 250 + 3, 250 - 6) and (-3, -6 ) would be ( 250 - 3, 250 + 6 )  
         */
       for( int i = 0; i <  xVals.length + 1; i ++) {
            if ( xVals[i] >= 0 && yVals[i] >= 0 ){
                xVals[i] = 250 + xVals[i];
                yVals[i] = 250 - yVals[i]; }//end if loop ++
            if ( xVals[i] < 0 && yVals[i] >= 0 ){
                xVals[i] = 250 - xVals[i];
                yVals[i] = 250 - yVals[i]; }//end if loop -+
            if ( xVals[i] < 0 && yVals[i] < 0 ){
                xVals[i] = 250 - xVals[i];
                yVals[i] = 250 + yVals[i]; }//end if loop --
            if ( xVals[i] >= 0 && yVals[i] < 0 ){
                xVals[i] = 250 + xVals[i];
                yVals[i] = 250 + yVals[i]; }//end if loop ++
        }//end for loop
}//end of start method
 
 
public void paint ( Graphics g ) {
        super.paint(g);
        //Paint horizontal and vertical lines on 500/500 Applet window - Creates a grid
        int i = 0;
        for( i = 0; i <= 500; i += 50 ) { //scales by 50 per line increment == 10 lines per axis
            g.drawLine( i, 0, i, 500 );
            g.drawLine( 0, i, 500, i );
        }//end for loop
 
        //start graphing converted x and y values
        for( i = 0; i <  xVals.length + 1; i++ ) {
            g.drawLine( xVals[i], yVals[i], xVals[i + 1], yVals[i + 1] ); /* Connects one point to the next point
                                                                    * so xVals[0] and yVals[0] connect to 
                                                                    * xVals[1] and yVals[1]
                                                                    */
        }//end for loop
    }//end of paint method
}//end of class