Search code examples
arraysarraylistrandomprocessingtext-size

How to create an instance of a class and store them in an array or arraylist? (processing)


I have this program that I need 50 random words from an external file to appear and each of them randomly moves to a random place... I was already able to make each word move separately from each other to a random place but the problem is that only the first word in the external file appears 50 times and that is the only word that appears... not 50 random words! just 50 words that are all the same... so I tried to put int index = int(random(allWords.length)); under draw and inside for but this may cause that it will happen 50 times, 60 times per second and that's not what I want to happen... someone suggested that Instead, I could probably just want to generate the random words once in the setup() function and I could do this by creating instances of the class I created and store them in an array or an ArrayList. The thing is I'm still not that familiar with that so does anybody has tips on how I can do that or maybe a link where I can have a guide on how to do that?

here is my code if anyone wants to see what my problem is...

    String [] allWords;
    int x = 120;
    int y = 130;
    int index = 0 ;
    word [] words;

    void setup () {

      size (500, 500);
      background (255); //background : white

      String [] lines = loadStrings ("alice_just_text.txt");
      String text = join(lines, " "); //make into one long string
      allWords = splitTokens (text, ",.?!:-;:()03 "); //splits it by word

      words = new word [allWords.length];

      for (int i = 0; i < 50; i++) {
        words[i] = new word (x, y);
      }
    }

    void draw() {

      background (255);

      for (int i = 0; i < 50; i++) {  //produces 50 words
        words[i].display();
        words[i].move();
        words[i].avgOverlap();
      }
    }

    class word {
      float x;
      float y; 

      word(float x, float y) {
        this.x = x;
        this.y = y;
      }

      void move() {

        x = x + random(-3, 3); //variables sets random positions
        y = y + random(-3, 3); //variables sets random positions
      }

      void display() {
        fill (0); //font color: black
        textAlign (CENTER, CENTER);
        text (allWords[index], x, y, width/2, height/2 );
      }

      void ran () {
        textSize (random(10, 80)); //random font size
      }

    }

Solution

  • You're already creating instances of a class and storing it inside an array in this for loop:

    for (int i = 0; i < 50; i++) {
        words[i] = new word (x, y);
    }
    

    The problem is that you only have a single index variable, so every instance of your Word class uses the same index value!

    You probably want to pass an individual index into each instance of Word you're creating:

    for (int i = 0; i < 50; i++) {
        words[i] = new word (x, y, i);
    }
    

    Or you could pass the String value that you want each particular instance to use:

    for (int i = 0; i < 50; i++) {
        words[i] = new word (x, y, allWords[i]);
    }
    

    Then you'd need to modify your Word constructor to take the extra parameter, as well as the display() function to use that parameter.

    Please note that classes should start with an upper-case letter, so it should be Word instead of word.

    Also, please try to isolate your problem to a MCVE that we can copy and paste to run ourselves. This will make your life easier, and it will make it easier for us to help you. Start over with a blank sketch and only add enough code so we can see your problem. Use a hard-coded array of String values instead of a file, for example.