1

still learning I'm trying to get a sentence split into word by word and trying to make it to the center of the canvas in processing anytime when I enter a new sentence. I'm trying to do this using 'for' loops and I can't determine how to position the X and Y co-ordinate into the loop which then split the sentence word by word and make to the center of the canvas. I was able manually split the sentence into word by word but it will mess up when I try update into a new sentence. By far this is what I have done.

import javax.swing.JOptionPane;
String Answer; // FOR GET AN INPUT
String Secret = "Sponge"; // CORRECT ANSWER
int textposX, textposY; //X AND Y CO-ORDINATES OF THE TEXT
final int ansBoxHeight = 50; // HEIGHT OF THE ANSWER BOX
final int ansBoxWidth = 200; // WIDTH OF THE ANSWER BOX
int boxPosX, boxPosY; // ANSWER BOX X AND Y CO-ORDINATES
String riddle = "WHAT IS: FULL OF HOLES BUT STILL HOLDS WATER, WHAT AM I?";

void setup() {
  size(500, 500);
  textposX = width/2;
  textposY = height/2;
}

void draw() {
  answerBox();
  riddle();
  answerComparison();
}

void riddle() {
  fill(0);
  textSize(50);
  for (int textposX = 0; textposX<=450; textposX = textposX+120) {
    for (int textposY = 0; textposY>=450; textposY = textposY+100) {
      text(riddle, textposX, textposY);
    }
  }
  //text("WHAT IS:", textposX-100, textposY-200);
  //text("FULL OF HOLES", textposX-150, textposY-90);
  //text("BUT", textposX-50, textposY-50);
  //text("STILL HOLDS", textposX-130, textposY-10);
  //text("WATER?", textposX-70, textposY+30);
  //text("WHAT AM I;", textposX-110, textposY+120);
}

void answerBox() {
  noFill();
  rect(boxPosX+150, boxPosY+440, ansBoxWidth, ansBoxHeight);
  if (mouseX>boxPosX+150 && mouseX<boxPosX+150+ansBoxWidth+ansBoxHeight && mouseY>boxPosY+440 && mouseY<boxPosY+440+ansBoxWidth+ansBoxHeight && mousePressed) {
    noLoop();
    Answer = JOptionPane.showInputDialog("Correct Answer Is;");
    rect(boxPosX+150, boxPosY+440, ansBoxWidth, ansBoxHeight);
    text(Answer, textposX-70, textposY+230);
  }
}

void answerComparison() {
  if (Secret.equals(Answer) == true) {
    fill(0);
    background(0);
  }
}

1 Answers1

3

Imagine that You have nice small function which return for You an ArrayList of Strings of proper width:

public static class TextSplitter {
  public static ArrayList<String> splitText(String inputText, int maximumLength) {
    String[] inputTable = split(inputText, ' ');
    ArrayList<String> outputTable = new ArrayList<String>();
    String line = "";
    for (String str : inputTable) {
      line = line + " " + str;
      if (line.length() >= maximumLength) {
        outputTable.add(line);
        line = "";
      }
    }
    outputTable.add(line);
    return outputTable;
  }
}

Now, You can pass Your riddle String as an argument of splitText method and get ArrayList of short strings:

ArrayList<String> textList = TextSplitter.splitText(riddle, 12);

Your riddle() now should just call a splitText()

void riddle() {
  fill(0);
  textSize(50);
  stroke(0);
  textAlign(CENTER);
  int textScrPos = 50;
  ArrayList<String> textList = TextSplitter.splitText(riddle, 12);
  for (String str : textList) {
    textScrPos += 64;
    text(str, width/2, textScrPos);
  }
}

So, the final code:

import javax.swing.JOptionPane;
String Answer; // FOR GET AN INPUT
String Secret = "Java"; // CORRECT ANSWER
int textposX, textposY; //X AND Y CO-ORDINATES OF THE TEXT
final int ansBoxHeight = 50; // HEIGHT OF THE ANSWER BOX
final int ansBoxWidth = 200; // WIDTH OF THE ANSWER BOX
int boxPosX, boxPosY; // ANSWER BOX X AND Y CO-ORDINATES
String riddle = "WHAT IS THIS: high-level, class-based, object-oriented programming language?";

void setup() {
  size(500, 500);
  textposX = width/2;
  textposY = height/2;
}

void draw() {
  answerBox();
  riddle();
  answerComparison();
}

void riddle() {
  fill(0);
  textSize(50);
  stroke(0);
  textAlign(CENTER);
  int textScrPos = 0;
  ArrayList<String> textList = TextSplitter.splitText(riddle, 12);
  for (String str : textList) {
    textScrPos += 64;
    text(str, width/2, textScrPos);
  }
}

void answerBox() {
  noFill();
  rect(boxPosX+150, boxPosY+440, ansBoxWidth, ansBoxHeight);
  if (mouseX>boxPosX+150
    && mouseX<boxPosX+150+ansBoxWidth+ansBoxHeight
    && mouseY>boxPosY+440
    && mouseY<boxPosY+440+ansBoxWidth+ansBoxHeight
    && mousePressed) {
    noLoop();
    Answer = JOptionPane.showInputDialog("Correct Answer Is;");
    rect(boxPosX+150, boxPosY+440, ansBoxWidth, ansBoxHeight);
    text(Answer, textposX-70, textposY+230);
  }
}

void answerComparison() {
  if (Secret.equals(Answer) == true) {
    fill(0);
    background(0);
  }
}

public static class TextSplitter {
  public static ArrayList<String> splitText(String inputText, int maximumLength) {
    String[] inputTable = split(inputText, ' ');
    ArrayList<String> outputTable = new ArrayList<String>();
    String line = "";
    for (String str : inputTable) {
      line = line + " " + str;
      if (line.length() >= maximumLength) {
        outputTable.add(line);
        line = "";
      }
    }
    outputTable.add(line);
    return outputTable;
  }
}
Mruk
  • 171
  • 1
  • 11