在处理中使用鼠标按下的随机字符串的ArrayList(也旋转)

问题描述 投票:0回答:1

我是编码和处理方面的新手(所以请耐心等待)。我需要完成一个班级项目,并且需要有关代码中一个组件的帮助。我的代码中有一个 mousePressed 函数,允许随机文本字符串以旋转方式显示。但是,我希望即使在观看者释放鼠标的任何随机位置释放鼠标后,文本也会旋转。我希望观看者能够执行此操作(基本上无限次),但所有文本都会同时旋转,而不仅仅是一次旋转一串文本。我知道我需要一些 ArrayList 函数来开始,但我不知道从哪里开始,因为我的代码中有很多移动的部分。

我希望这是有道理的:)但是任何反馈/示例都将非常感激!

谢谢!

    String [] sentences= {
          "Hello",
          "Good morning",
          "Good night",
    };
    float theta;
    int index = 0;
    
    void setup() {
          background (0);
          size(700, 700);
          textSize(20);
    }

    void draw() {
          background(0);
          pushMatrix();
          fill(255);
          textAlign(CENTER);
          translate(mouseX, mouseY);
          rotate(theta);
          text(sentences[index], 0, 0);
          popMatrix();
        
          theta += 0.02;
    }

    void mousePressed () {
          index = int(random(3));
    }

我已经尝试将 ArrayList 与 PVector 一起使用,但代码似乎无法识别我使用的一些变量。因此,我可以使用的 ArrayList 示例将非常有帮助:)

java random arraylist processing mouseevent
1个回答
0
投票

检查此代码。

// Import the ArrayList class
import java.util.ArrayList;

ArrayList<PVector> textPositions = new ArrayList<PVector>();
ArrayList<String> textStrings = new ArrayList<String>();

void setup() {
  size(700, 700);
  textSize(20);
}

void draw() {
  background(0);

  // Iterate through all stored text positions and strings
  for (int i = 0; i < textPositions.size(); i++) {
    PVector pos = textPositions.get(i);
    String textString = textStrings.get(i);

    pushMatrix();
    fill(255);
    textAlign(CENTER);
    translate(pos.x, pos.y);
    rotate(theta);
    text(textString, 0, 0);
    popMatrix();
  }

  theta += 0.02;
}

void mousePressed () {
  // Add the current mouse position and a random string to the ArrayLists
  PVector mousePos = new PVector(mouseX, mouseY);
  textPositions.add(mousePos);
  textStrings.add(sentences[int(random(3))]);
}

您需要首先导入此代码。

import processing.core.PVector;
© www.soinside.com 2019 - 2024. All rights reserved.