java 在处理过程中使用for循环将句子居中放置在画布上

stszievb  于 2023-03-11  发布在  Java
关注(0)|答案(1)|浏览(92)

我还在学习,当我输入一个新的句子时,我试图把一个句子一个字一个字地拆分,并试图使它在处理过程中位于画布的中心。我试图使用“for”循环来实现这一点,但我无法确定如何将X和Y坐标定位到循环中,然后循环将句子一个字一个字地拆分,并使其位于画布的中心。我可以手动将句子一个字一个字地拆分,但当我尝试更新成一个新句子时,它会搞砸。到目前为止,这是我所做的。

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);
  }
}
k3bvogb1

k3bvogb11#

假设你有一个很好的小函数,它为你返回一个适当宽度的字符串数组列表:

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;
  }
}

现在,您可以将您的谜语字符串作为 splitText 方法的参数传递,并获得短字符串的ArrayList:

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

您的rider()现在应该只调用一个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);
  }
}

最后的代码是:

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;
  }
}

相关问题