We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › int[] adds 0's from one method to the next
Page Index Toggle Pages: 1
int[] adds 0's from one method to the next (Read 473 times)
int[] adds 0's from one method to the next
Apr 8th, 2009, 9:20am
 
I am trying to write application that splits up user input text and creates a visualisation from it. I have allocated x,y coordinates to certain parameters that appear in words. According to the user input i am collecting these coordinates in an arraylist, then put them into an int[] and from there draw shapes at the specific positions. This works fine. I then want to link the shapes with lines. This doesn't work. Suddenly the program draws more lines than its supposed to, and seems to get 0 values from somewhere. I have put the code below. can anybody help?


void drawInCorrectPosition(String[] two, String[] twoGrams, String[] lines, color[] c1, color[] c2, float[] r, float[] g, float[] b, int[] car1, int[] car2)
 {

   ArrayList thatXPos = new ArrayList(); //new arraylist to store all the x values and then when drawing run throught these x values


   for (int i= 0; i < twoGrams.length; i++) // compare user input with preset list
   {
     for (int j = 0; j < two.length; j++)
     {
       if(twoGrams[i].equals(two[j])  == true)
       {
         int xpos = leftmargin + ( i * 3);
         thatXPos.add(new Integer(xpos)); 
         int[] xPosCount = new int[thatXPos.size()];  
         for (int k = 0 ; k < thatXPos.size(); k++)        
         {
           xPosCount[k] = ((Integer)thatXPos.get(k)).intValue();      

           int y = height - 200;                    
           noStroke();
           fill((c1[j]+c2[j])/ 2);
           rect (xpos,y, 30, 40);   //put a rectangle for every stored number in the int[]

           callLines(y, two, twoGrams, xPosCount, xpos);    
         }
       }
     }
   }
 }

 void callLines(int y, String[] two, String[] twoGrams, int[] xPosCount, int xpos)  
 {
   for (int i = 0; i < xPosCount.length-1; i++)  //connect the rectangle with u-shaped line
   {
     int lineStartX = (xPosCount[i]+30)+i*4;
     int lineEndX = xPosCount[i+1]+30;
     
     stroke(100);
     noFill();
     beginShape();
     vertex(lineStartX, y+40);
     vertex(lineStartX, y+51);

     vertex(lineEndX, y+51);
     vertex(lineEndX, y+40);
     endShape();
   }
 }

Re: int[] adds 0's from one method to the next
Reply #1 - Apr 8th, 2009, 9:53am
 
I don't have time to run it right now, but a quick glance at the code make me suspecting that the callLines(y, two, twoGrams, xPosCount, xpos) call should be outside the loop on k.
Re: int[] adds 0's from one method to the next
Reply #2 - Apr 11th, 2009, 9:06pm
 
<removed by me for double posting, sorry didn't mean to hijack.
Re: int[] adds 0's from one method to the next
Reply #3 - Apr 12th, 2009, 2:22am
 
I believe your problem has nothing to do with this one, I will reply there instead.
Page Index Toggle Pages: 1