# JApplet Grapher Not Painting

• February 27th, 2013, 02:16 PM
Zyrion
JApplet Grapher Not Painting
Code java:

```/***************************************************************************************************************** * 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```
• February 27th, 2013, 02:36 PM
Norm
Re: JApplet Grapher Not Painting
What are the 3 values that are entered by the user?

For testing, just assign the values directly to a, b and c and comment out the JOptionPane calls.
For example:
Code :

` a = "123"; //JOptionPane.showInputDialog( null, "Enter integer A in Ax^2 + Bx + C = 0", "Quadratic Solver" );`
That will make testing much easier. When the drawing part works, restore the JOptionPane calls.
• February 27th, 2013, 05:53 PM
Zyrion
Re: JApplet Grapher Not Painting
The values entered by the user are the variables for the quadratic equation Ax + Bx + C (variables a, b, and c). And I commented it out to test it and it still does not work unfortunately.
• February 27th, 2013, 06:18 PM
Norm
Re: JApplet Grapher Not Painting
Can you post the values of a,b &c to use?
• February 27th, 2013, 08:54 PM
Zyrion
Re: JApplet Grapher Not Painting
It doesn't matter, there supposed to be integers entered by the user. It says it in the code lol.
• February 28th, 2013, 02:18 AM
Zyrion
Re: JApplet Grapher Not Painting
I added the super.paint(g); method to instruct the parent class to invoke it's own paint method so it will draw itself correctly. I commented out the start() method and the paint() method ran correctly. The issue lies in the indexing of the arrays xVals[] and yVals[] in the start() method. I am unsure where that problem is located precisely.
• February 28th, 2013, 06:07 AM
Norm
Re: JApplet Grapher Not Painting
Quote:

The issue lies in the indexing of the arrays xVals[] and yVals[] in the start() method. I am unsure where that problem is located precisely.
If you are getting error messages, copy the full text and paste it here.

One poor technique I see is hardcoding a for loop control value:
Code :

` for( int i = 0; i < 21; i ++) {`
instead of using the array's .length attribute.
• February 28th, 2013, 05:40 PM
Zyrion
Re: JApplet Grapher Not Painting
My compiler BlueJ throws an indexOutofBounds exception.
• February 28th, 2013, 06:29 PM
Norm
Re: JApplet Grapher Not Painting
Quote:

an indexOutofBounds exception.
What array is being indexed? What is the size of the array? What is the value of the index?

The code should use the array's .length attribute to control the for loop. It should NOT use a hardcoded number:
Code :

` for ( int i = 0; i < 21; i ++ ){`
replace the 21 with the name of the array and its .length as already stated in post#7
• February 28th, 2013, 07:23 PM
Zyrion
Re: JApplet Grapher Not Painting
Quote:

Originally Posted by Norm
What array is being indexed? What is the size of the array? What is the value of the index?

The code should use the array's .length attribute to control the for loop. It should NOT use a hardcoded number:
Code :

` for ( int i = 0; i < 21; i ++ ){`
replace the 21 with the name of the array and its .length as already stated in post#7

What array is being indexed? xVals[], and yVals[]
What is the size of the array? xVals.length = 20, yVals.length = 20
What is the value of the index? xVals[] =-10 to 10 and yVals[] = 0 until changed

Because the length of my array is 20 and adding one for the index at 0, I get 21 elements. So the hard coded loop may be bad practice but yields an equal expression to be tested.

Exception thrown: java.lang.ArrayIndexOutOfBoundsException. 20
• February 28th, 2013, 08:01 PM
Norm
Re: JApplet Grapher Not Painting
Using hardcoded numbers is always bad practice.
Quote:

ArrayIndexOutOfBoundsException. 20
The index is past the end of the array. The array has less than 21 elements.
The range of indexes for an array range from 0 to the array length-1.
The code: for(int i=0; i < theArray.length; i++) will always work

Quote:

What is the size of the array? xVals.length = 20, yVals.length = 20
If the array lengths are 20, why does the code use 21 in the for loop?
• February 28th, 2013, 09:27 PM
Zyrion
Re: JApplet Grapher Not Painting
Why does the code use 21 in the for loop?

Code java:

```int[] array = new int[20]; //This yields 21 elements = array[0],array[1],array[2]...array[20]   for( i = 0; i < 21 ; i++ ){ /* In order to access elements indexed from 0 to 20 * ( 21 ) elements you would create a loop with 1 more */ than the arrays length as an expression.   xVals[i] = //...Indexes each element starting from 0 to 20 ( 21 ) elements.       }```

UPDATE
I figured out that int[] array = new int[20]; does not yield twenty elements from 0 to 20 but from 0 to 19.