我正在通過 Processing 學習 Java。該代碼執行以下操作。1) 調用 Setup 并初始化大小為 700,300 的窗口。2) 使用設置中的 for 循環初始化多個點,并給出隨機速度。3)自動調用draw函數。它是一個循環函數。它一次又一次地被調用。它每次都用一個黑色矩形填充空間,并繪制所有圓圈并更新它們的位置。4) 由于 rect() 命令在每次調用 draw() 時都會清除屏幕,因此它必須只顯示一個粒子并且沒有軌跡。 但確實如此。我遇到了其中一個教程,代碼如下Spot[] spots; // Declare arrayvoid setup() { size(700, 100); int numSpots = 70; // Number of objects int dia = width/numSpots; // Calculate diameter spots = new Spot[numSpots]; // Create array for (int i = 0; i < spots.length; i++) { float x = dia/2 + i*dia; float rate = random(0.1, 2.0); // Create each object spots[i] = new Spot(x, 50, dia, rate); } noStroke();}void draw() { fill(0, 12); rect(0, 0, width, height); fill(255); for (int i=0; i < spots.length; i++) { spots[i].move(); // Move each object spots[i].display(); // Display each object }}class Spot { float x, y; // X-coordinate, y-coordinate float diameter; // Diameter of the circle float speed; // Distance moved each frame int direction = 1; // Direction of motion (1 is down, -1 is up) // Constructor Spot(float xpos, float ypos, float dia, float sp) { x = xpos; y = ypos; diameter = dia; speed = sp; } void move() { y += (speed * direction); if ((y > (height - diameter/2)) || (y < diameter/2)) { direction *= -1; } } void display() { ellipse(x, y, diameter, diameter); }}它產生這個輸出:我不明白為什么它會創建這些痕跡,而這些痕跡就會消失。直覺上,只有一個點應該是可見的,因為for (int i=0; i < spots.length; i++) {spots[i].move(); // Move each objectspots[i].display(); // Display each object}請指出使這發生的代碼行?我沒有線索。參考:https ://processing.org/tutorials/arrays/@Arrays:Casey Reas 和 Ben Fry
1 回答

桃花長相依
TA貢獻1860條經驗 獲得超8個贊
場景永遠不會被清除,因此在新幀中將新點添加到場景中時,在前一幀中繪制的點仍然存在。
說明
fill(0, 12); rect(0, 0, width, height);
在整個視圖上繪制一個透明的黑色矩形。因此,前幾幀的斑點似乎隨著時間的推移而淡出。由于“較舊”的斑點已經被透明矩形覆蓋了很多次,它們變成了深灰色?!拜^年輕”的斑點只是被覆蓋了幾次,呈現出淺灰色。立即繪制的斑點是白色的,因為白色填充顏色 ( fill(255);
)
如果你增加混合矩形的 alpha 值,那么斑點會更快地“消失”并且它們的“尾巴”會更短。
例如
fill(0, 50);
添加回答
舉報
0/150
提交
取消