Strings and Arrays
Hello,
I've hit a wall. I know I am doing something stupid, but cannot find the exact location of the illogical act. :)
The result I am after is an array of rectangles with a different line of text in each.
For future reasons, I am using Strings for the text.
So I have a Node class defined by array that takes in the string from an array (fed in via an argument) and creates a rect with the string in (there is further code to word wrap and line space as defined in the arguments).
It works for the first rect and string node, but further ones in the array do not have the next text string, but all of them. So defining 2 nodes gives 2 nodes of 2 rects with the first 2 lines of text in each. 3 gives, 3, 3, and 3, etc.
Lines 65-on can be ignored, as they are not germane to the problem....
I suspect where I am going wrong is here on line 25-26, but cannot prove it:
for (int i = 0; i < myNode.length; i++) { myNode[i] = new Node (newString[i],150,8); }
Can anyone see the obvious howler?
- float posX;
- float posY;
- color r = (int) random (50,200);
- color g = (int) random (50,200);
- color b = (int) random (50,200);
- color a = (int) 100;
- //node array for rect and String
- Node[] myNode = new Node[2];
- PFont font;
- int fontSize = 24;
- //String Array
- String[] newString = {"test1", "testtttttttttttttt2", "test3"};
- void setup(){
- size(1000,1000);
- smooth();
- font = createFont("Calibri", fontSize);
- textFont(font,fontSize);
- //for loop to create nodes and populate with strings
- //initialise node via constructor/newstring lines by array of list above
- for (int i = 0; i < myNode.length; i++) {
- myNode[i] = new Node (newString[i],150,8);
- }
- }
- void draw(){
- background (100);
- //for loop to run display from node class
- for (int i = 0; i < myNode.length;i++) {
- myNode[i].display();
- }
- }
- class Node { //Node class
- String nodeString = ""; //empty string to insert as holder
- int specificWidth; //max width of string before return
- int lineSpacing; //tweak to match font size
- //constructor for Node
- Node(String nodeString_,int specificWidth_,int lineSpacing_) {
- nodeString= nodeString_;
- specificWidth = specificWidth_;
- lineSpacing = lineSpacing_;
- }
- void display() {
- float posX = mouseX;
- float posY = mouseY;
- //for loop to draw nodes and strings
- for (int i = 0; i < myNode.length; i++) {
- rectMode(CORNER);
- fill (r,g,b,a);
- rect(posX+(50*i),posY+(50*i),specificWidth,calculateTextHeight(nodeString, specificWidth, fontSize, lineSpacing));
- fill(255);
- text (nodeString,posX+(50*i),posY+(50*i),specificWidth,1000);
- }
- }
- //function for calculating text height from string, which is split into rows based on specificWidth
- int calculateTextHeight(String nodeString, int specificWidth, int fontSize, int lineSpacing) {
- String[] wordsArray;
- String tempString = "";
- int numLines = 0;
- float textHeight;
- wordsArray = split(nodeString, " ");
- for (int i=0; i < wordsArray.length; i++) {
- if (textWidth(tempString + wordsArray[i]) < specificWidth) {
- tempString += wordsArray[i] + " ";
- }
- else {
- tempString = wordsArray[i] + " ";
- numLines++;
- }
- }
- numLines++;
- textHeight = numLines * (textDescent() + textAscent() + lineSpacing);
- return(round(textHeight));
- }
- }