1

特定のフレーズの各単語を、提供されたサンプル テキストにある対応する単語と照合しています。各インスタンスでベジエ曲線を描画することにより、特定のフレーズの単語とサンプル テキストを一致させています。私の問題は次のとおりです。サンプルのテキストを配列に配置した後、各単語の間に奇妙なスペースを入れずにテキストを再度描画する良い方法がわかりません。現在、配列から各単語を描画していますが、あまり効果的ではありません。おそらく私のアプローチ全体が間違っています...どんな提案でも大歓迎です。これは私にとってすべて新しいことです。ありがとう!

String sampleText = "this is where my sample text goes";

String phrasePart1 = "this";
String phrasePart2 = "is";
String phrasePart3 = "text";

float x = 30;
float y = 70;

size(1000, 800);
background(#FFFFFF);
smooth();

String[] wordSet = split(sampleText, " ");

fill(0);

text(phrasePart1, width/2-30, 30);
text(phrasePart2, width/2+10, 30);
text(phrasePart3, width/2+30, 30);

for (int i = 0; i < wordSet.length; i++) {

  textSize(6);
  text(wordSet[i], x, y);


  if (wordSet[i].equals(phrasePart1)) {
    noFill();
    stroke(153);
    smooth();
    bezier(width/2-35, 27, 30, 30, 40, random(30, 200), x+5, y-9);
  }

    if (wordSet[i].equals(phrasePart2)) {
    noFill();
    stroke(153);
    smooth();
    bezier(width/2+13, 33, 670, 30, 680, random(30, 200), x+5, y-9);
    }

  if (wordSet[i].equals(phrasePart3)) {
    noFill();
    stroke(153);
    smooth();
    bezier(width/2+50, 27, 670, 30, 680, random(30, 200), x+5, y-9);
  }

  x = x + wordSet[i].length()*3+4;


  if (x > width-50) {
    x = 30;
    y = y + 12;
  }
}
4

1 に答える 1

2

交換してみる

x = x + wordSet[i].length()*3+4;

forループの一番下にtextWidthを

x = x + textWidth(wordSet[i] + " " );
于 2013-10-27T12:55:54.790 に答える