gpt4 book ai didi

java - 仅对一项功能使用背景

转载 作者:行者123 更新时间:2023-12-04 09:37:42 25 4
gpt4 key购买 nike

我正在 processing 上制作动画.然后,我有一个关于代码的问题。通常,我的代码比较长。但是,我制作了一个简单的代码,对初学者也很有用。我的示例代码:

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++;
}
所以,我有相同方向的点和矩形。 background两者都有影响。但是,背景不应该影响点数。因为我想用点画一条线。背景应该只影响矩形。
解决方案应如下所示:
enter image description here

最佳答案

您将需要对此采取更简单的方法。与其画一条多点的线,不如画一条实际的线!而且您只能使用 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
}
An example

关于java - 仅对一项功能使用背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62483423/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com