是否可以在处理中返回"text()"?



我想在函数中返回text(x,y,n,n)

目前,我的代码是这样的:

public void myText(){
text("Hello World", 20, 20);
}

但是,我希望有这样的语法:

public text myText() {
return ("Hello World", 20, 20);
}

public myText(){
return (text("Hello World", 20, 20));
}

好吧,我一直在尝试对这个问题进行大量研究,但结果仍然是negative.

是否有可能这样做,或者是否有任何其他相似之处?

您可以将文本呈现为PGraphics对象。 例如:

void setup() {
size(200, 200);
}
void draw() {
background(0);
PGraphics pgText = myText();
image(pgText, 20, 30);
image(pgText, 20, 60);
}
public PGraphics myText() {
String s = "Hello World";
int h = 20;
textSize(h);
float w = textWidth(s);
PGraphics pg = createGraphics(int(w), h);
pg.beginDraw();
pg.text(s, 0, h);
pg.endDraw();
return pg;
}

听起来你在寻找课程

您可以创建一个类来封装您关心的数据。像这样:

class MyText {
String message;
int x;
int y;
public MyText(String message, int x, int y) {
this.message = message;
this.x = x;
this.y = y;
}
void draw(){
text(message, x, y);
}
}

然后,您可以在草图中使用该类:

MyText myText;
void setup() {
myText = new MyText("hello", 25, 25);
}
void draw() {
myText.draw();
}

无耻的自我推销:这里有一个关于在处理中创建类的教程。

最新更新