我有很多圈子裏我的處理程序隨機定位。每個人都是這樣發起的:在處理中,matplotlib中是否有類似zorder的屬性?
ellipse(x, y, radius, radius);
我想知道是否可以以某種方式爲每個圓圈指定z-index值。較高Z指數的圓圈顯示較低圓圈的圓圈。
我有很多圈子裏我的處理程序隨機定位。每個人都是這樣發起的:在處理中,matplotlib中是否有類似zorder的屬性?
ellipse(x, y, radius, radius);
我想知道是否可以以某種方式爲每個圓圈指定z-index值。較高Z指數的圓圈顯示較低圓圈的圓圈。
我現在不能測試此權利,但我相信你應該看看fill()
方法。此函數中的第四個參數指定不透明度(AKA alpha透明度)。它在Processing.org/learning/color中描述。
編輯:這thread上繪製順序似乎要問和你同樣的問題。玩弄圖像透明度的建議,但它似乎是一個更好的答案是利用Processing Layar Library。這裏是some examples - 第一個似乎是你的問題的線。
引用的層的實例的Advanced部分:
動態添加,刪除,或重新排序圖層
,能夠動態地添加,移除或重新排序層的 草圖的執行期間但在這樣做時應該非常小心。由AppletLayers對象所管理的層的 列表可以通過獲得的ListIterator對象操縱 。如何列表可以通過迭代器 操作語義應仔細閱讀該 Javadoc for java.util.ListIterator進行研究。究其原因,很難 操縱草圖執行過程中的列表是因爲 層列表被通過,以使它們重複。最好的 位置來操縱圖層列表是在主草圖的繪製() 方法中,而不是在圖層的draw()方法或鼠標或鍵盤 事件處理中。
另一種選擇是使用數組或列表來跟蹤你的橢圓的,所以你有兩個步驟:
下面是一個使用類型ArrayList一個例子:
ArrayList<Ellipse> ellipses = new ArrayList<Ellipse>();
void setup(){
size(400,400,P2D);
smooth();
fill(192,0,0,192);
reset();
}
void reset(){
ellipses.clear();
for(int i = 0 ; i < 40; i++){
float radius = random(5,25);
Ellipse e = new Ellipse(i*10,random(20,380),radius,radius,i);
ellipses.add(i,e);
}
}
void draw(){
background(255);
for(Ellipse e : ellipses) e.draw();
}
void keyPressed(){
reset();
}
class Ellipse{
float x,y,w,h;
int zIndex;
Ellipse(float ax,float ay,float aw,float ah,int az){
x = ax;
y = ay;
w = aw;
h = ah;
zIndex = az;
}
void draw(){
ellipse(x,y,w,h);
}
}
注意的ArrayList是相當靈活的,你可以添加什麼索引,你需要輕鬆的元素。
如果你想使用數組,你需要確保你可以根據你所選擇的屬性對數組進行排序,所以你需要實現Comparable interface:
int numEllipses = 40;
Ellipse[] ellipses = new Ellipse[numEllipses];//this will store original/unsorted data
Ellipse[] ellipsesSorted = new Ellipse[numEllipses];//this will store the sorted data
void setup(){
size(400,400,P2D);
smooth();
fill(192,0,0,192);
reset();
}
void reset(){
for(int i = 0 ; i < numEllipses; i++){
float radius = random(5,25);
int index = (int)random(0,numEllipses);
Ellipse e = new Ellipse(i*10,random(20,380),radius,radius,index);
ellipses[i] = e;
}
//copy original data into an array to be sorted
arrayCopy(ellipses, ellipsesSorted);
//sort the array - uses the compareTo method behind the scenes
Arrays.sort(ellipsesSorted);
}
void draw(){
background(255);
for(Ellipse e : ellipsesSorted) e.draw();
}
void keyPressed(){
reset();
}
class Ellipse implements Comparable<Ellipse>{
float x,y,w,h;
int zIndex;
Ellipse(float ax,float ay,float aw,float ah,int az){
x = ax;
y = ay;
w = aw;
h = ah;
zIndex = az;
}
void draw(){
ellipse(x,y,w,h);
}
int compareTo(Ellipse e)
{
if(e.zIndex > this.zIndex) return 1;
else if(e.zIndex < this.zIndex) return -1;
else return 0;
}
String toString(){
return "z index: "+zIndex;
}
}