Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: JApplet Grapher Not Painting

  1. #1
    Member Zyrion's Avatar
    Join Date
    Feb 2013
    Location
    Iowa
    Posts
    106
    My Mood
    Angelic
    Thanks
    2
    Thanked 8 Times in 8 Posts

    Default JApplet Grapher Not Painting

    /*****************************************************************************************************************
     *  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


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,661
    Thanks
    45
    Thanked 1,960 Times in 1,934 Posts

    Default 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:
     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.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member Zyrion's Avatar
    Join Date
    Feb 2013
    Location
    Iowa
    Posts
    106
    My Mood
    Angelic
    Thanks
    2
    Thanked 8 Times in 8 Posts

    Default 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.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,661
    Thanks
    45
    Thanked 1,960 Times in 1,934 Posts

    Default Re: JApplet Grapher Not Painting

    Can you post the values of a,b &c to use?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member Zyrion's Avatar
    Join Date
    Feb 2013
    Location
    Iowa
    Posts
    106
    My Mood
    Angelic
    Thanks
    2
    Thanked 8 Times in 8 Posts

    Default 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.

  6. #6
    Member Zyrion's Avatar
    Join Date
    Feb 2013
    Location
    Iowa
    Posts
    106
    My Mood
    Angelic
    Thanks
    2
    Thanked 8 Times in 8 Posts

    Default 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.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,661
    Thanks
    45
    Thanked 1,960 Times in 1,934 Posts

    Default Re: JApplet Grapher Not Painting

    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:
           for( int i = 0; i < 21; i ++) {
    instead of using the array's .length attribute.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Member Zyrion's Avatar
    Join Date
    Feb 2013
    Location
    Iowa
    Posts
    106
    My Mood
    Angelic
    Thanks
    2
    Thanked 8 Times in 8 Posts

    Default Re: JApplet Grapher Not Painting

    My compiler BlueJ throws an indexOutofBounds exception.

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,661
    Thanks
    45
    Thanked 1,960 Times in 1,934 Posts

    Default Re: JApplet Grapher Not Painting

    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:
      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
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Member Zyrion's Avatar
    Join Date
    Feb 2013
    Location
    Iowa
    Posts
    106
    My Mood
    Angelic
    Thanks
    2
    Thanked 8 Times in 8 Posts

    Default Re: JApplet Grapher Not Painting

    Quote Originally Posted by Norm View Post
    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:
      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

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,661
    Thanks
    45
    Thanked 1,960 Times in 1,934 Posts

    Default Re: JApplet Grapher Not Painting

    Using hardcoded numbers is always bad practice.
    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

    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?
    If you don't understand my answer, don't ignore it, ask a question.

  12. #12
    Member Zyrion's Avatar
    Join Date
    Feb 2013
    Location
    Iowa
    Posts
    106
    My Mood
    Angelic
    Thanks
    2
    Thanked 8 Times in 8 Posts

    Default Re: JApplet Grapher Not Painting

    Why does the code use 21 in the for loop?

    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.
    Last edited by Zyrion; February 28th, 2013 at 09:41 PM. Reason: UPDATE

Similar Threads

  1. Painting ovals
    By Proletariat in forum What's Wrong With My Code?
    Replies: 16
    Last Post: June 8th, 2012, 01:45 PM
  2. [SOLVED] Painting Issues
    By snowguy13 in forum AWT / Java Swing
    Replies: 9
    Last Post: May 23rd, 2012, 09:54 PM
  3. Output in applet keeps painting over the top of itself
    By SashaThompson in forum Java Applets
    Replies: 1
    Last Post: October 9th, 2011, 11:21 AM
  4. [SOLVED] Extends JPanel painting problem
    By Philip in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 28th, 2010, 03:16 PM
  5. Painting swing components to an arbitrary position?
    By ScummyChimp in forum AWT / Java Swing
    Replies: 1
    Last Post: September 1st, 2009, 11:06 PM