Java 仅对一个函数使用后台

Java 仅对一个函数使用后台,java,processing,Java,Processing,我当时正在制作一部动画。然后,我有一个关于代码的问题。通常,我的代码比较长。然而,我做了一个简单的代码,它也可以对初学者有用。我的示例代码: int x1 = 0; int x2 = 0; void setup() { size(500, 100); // create a screen } void draw() { background(255); drawRectangle(); drawPoint(); } void drawRectangle() { rect

我当时正在制作一部动画。然后,我有一个关于代码的问题。通常,我的代码比较长。然而,我做了一个简单的代码,它也可以对初学者有用。我的示例代码:

int x1 = 0;
int x2 = 0;

void setup() {
  size(500, 100); // create a screen
}

void draw() {
  background(255);
  drawRectangle();
  drawPoint();
}

void drawRectangle() {
  rect(x1, 20, 20, 20); // rect(x, y, width,height);
  x1 +=5;
}

void drawPoint() {
  point(x2, 20); // point(x, y);
  x2++;
}
所以,我有点和矩形在同一个方向上。
背景
对两者都有影响。但是,背景不应影响这些要点。因为我想用点画一条线。背景应该只影响矩形。 解决方案应该如下所示:

您需要采取一种更简单的方法。不要画一条有多个点的线,而是画一条实际的线!您也只能使用1
x

这是一个小示例程序,它生成以下输出:

int x = 10, y = 50;

void setup() {
  size(400, 400);
  rectMode(CENTER); // So the values you give rect() are seen as the center of the rectangle
}

void draw() {
  background(255);

  drawLine();
  drawRectangle();

  x += 3;
}

void drawLine() {
  strokeWeight(5); // Make the line more visible
  line(0, y, x, y); // Draw a line from the left of the screen to x
  strokeWeight(1); // Return to standard
}

void drawRectangle() {
  fill(255, 0, 0); // Make the color red
  rect(x, y, 20, 20); // Draw the rectangle
}

那么您想在
shape1
后面有一条轨迹吗?是的,shape1应该重复它自己。因此,在输出上有多个矩形交织在一起。(看起来矩形没有背景)Hımm,谢谢。事实上,你的回答对这个问题是正确的。然而,这并没有达到我的目标。也许我问错了。我将投票表决你的答案。然而,有没有办法在draw函数中实现这一点?我不确定我是否理解。也许你可以提供一张你想要达到的目标的图?