2 回答

TA貢獻1862條經驗 獲得超7個贊
創建一個在對象上繪制單個點的函數PGraphics:
void DrawPen(PGraphics pg, int cptX, int cptY, int r) {
? ? pg.beginDraw();
? ? for (int x = 0; x < r; ++x) {
? ? ? ? for (int y = 0; y < r; ++y) {
? ? ? ? ? float distance = sqrt(x*x + y*y);
? ? ? ? ? float alpha = 255-map(distance,0,r,0,255);
? ? ? ? ? if (distance < r) {
? ? ? ? ? ? ? pg.set(cptX+x,cptY+y,color(255,255,255, alpha));
? ? ? ? ? ? ? pg.set(cptX-x,cptY+y,color(255,255,255, alpha));
? ? ? ? ? ? ? pg.set(cptX+x,cptY-y,color(255,255,255, alpha));
? ? ? ? ? ? ? pg.set(cptX-x,cptY-y,color(255,255,255, alpha));
? ? ? ? ? }
? ? ? ? }
? ? }
? ? pg.endDraw();
}
PGraphics在其中的單獨對象上繪制一個點setup
PGraphics pg;
PGraphics pg_pen;
int rad = 20;
void setup (){
? ? size(800, 800, P2D);
? ? pg = createGraphics(800, 800, JAVA2D);
? ? pg.beginDraw();
? ? // [...]
? ? pg.endDraw();
? ? pg_pen = createGraphics(2*rad, 2*rad, JAVA2D);
? ? DrawPen(pg_pen, rad, rad, rad);
}
拖動鼠標時,將其混合到當前鼠標位置處的pg_pen公共PGraphics對象 ( ):pg
void mouseDragged(){
? ? pg.beginDraw();
? ? pg.image(pg_pen, mouseX-rad, mouseY-rad);
? ? pg.endDraw();
}
為了追求draw功能的完整性:
void draw () {
? ? background(0);?
? ? image(pg,0,0);
}
[...]并嘗試從白色部分獲取顏色以在黑色部分上繪制。
color向該函數添加一個參數,并在繪制之前DrawPen清除筆:PGraphics
void DrawPen(PGraphics pg, int cptX, int cptY, int r, color c) {
? ? pg.beginDraw();
? ? pg.clear();
? ? for (int x = 0; x < r; ++x) {
? ? ? ? for (int y = 0; y < r; ++y) {
? ? ? ? ? float distance = sqrt(x*x + y*y);
? ? ? ? ? float alpha = 255-map(distance,0,r,0,255);
? ? ? ? ? if (distance < r) {
? ? ? ? ? ? ? color pc = color(red(c),green(c),blue(c), alpha);
? ? ? ? ? ? ? pg.set(cptX+x,cptY+y,pc);
? ? ? ? ? ? ? pg.set(cptX-x,cptY+y,pc);
? ? ? ? ? ? ? pg.set(cptX+x,cptY-y,pc);
? ? ? ? ? ? ? pg.set(cptX-x,cptY-y,pc);
? ? ? ? ? }
? ? ? ? }
? ? }
? ? pg.endDraw();
}
獲取鼠標按下事件回調中的顏色并改變畫筆的顏色:
void mousePressed() {
? ? color c = pg.get(mouseX, mouseY);
? ? println(c);
? ? DrawPen(pg_pen, rad, rad, rad, c);
}
請注意,顏色是從pg對象獲取的,而不是從屏幕獲取的。如果你想從屏幕上獲取顏色,那么它必須是(不帶.pg):
color c = get(mouseX, mouseY);
此外,每當按下任何鼠標(按下而不是拖動)時,顏色都會發生變化??赡苣朐诎聪率髽擞益I時更改顏色并在按下鼠標左鍵時進行繪制:
void mousePressed() {
? ? if (mouseButton == RIGHT) {
? ? ? ? color c = pg.get(mouseX, mouseY);
? ? ? ? println(c);
? ? ? ? DrawPen(pg_pen, rad, rad, rad, c);
? ? }
}

TA貢獻2041條經驗 獲得超4個贊
添加“背景(0);” 在“圖像(pg,0,0);”之前 在你的繪圖方法中,這樣你每次都會重置背景,如果你不這樣做,程序將繼續在彼此之上繪制每幀圖像,這將使低不透明度像素每幀緩慢獲得不透明度,直到達到 100 %
void draw () {
background(0);
image(pg,0,0);
}
編輯:測試你的代碼后,我注意到你創建這些圓圈的方式存在問題,使其運行速度超級慢(每一幀你都通過雙循環來繪制一個圓圈)并且還給出了奇怪的黑邊問題,所以這就是我所做的:
首先我使用了你的變量 pg 并在啟動時在它上面畫了一個圓圈,然后我聲明了另一個 PGraphics 'pg_all',其中我每次調用 mousedrag 方法都畫了一個 pg,我在多個背景上測試了它,它看起來工作正常,在這里是最終的代碼,如果您不理解某個部分或想要以不同的方式進行操作,請告訴我:
PFont font;
PGraphics pg;
PGraphics pg_all;
int X;
int Y;
int rad = 20;
void setup (){
size(800, 800, P2D);
background(0);
noStroke();
pg_all = createGraphics(800, 800, JAVA2D);
pg_all.beginDraw();
pg_all.endDraw();
pg = createGraphics(800, 800, JAVA2D);
pg.beginDraw();
for (int x=0; x<rad; x++) {
for (int y=0; y<rad; y++) {
float distance = sqrt(pow(x,2)+pow(y,2));
float alpha = 255-map(distance,0,rad,0,255);
if (sqrt(pow(x,2)+pow(y,2)) < rad){
pg.beginDraw();
pg.set(20+x,20+y,color(255,255,255, alpha));
pg.set(20-x,20+y,color(255,255,255, alpha));
pg.set(20+x,20-y,color(255,255,255, alpha));
pg.set(20-x,20-y,color(255,255,255, alpha));
pg.endDraw();
}
}
}
pg.endDraw();
}
void draw () {
background(0);
image(pg_all,0,0);
}
void mouseDragged(){
X = mouseX;
Y = mouseY;
pg_all.beginDraw();
pg_all.image(pg,X-rad,Y-rad);
pg_all.endDraw();
}
添加回答
舉報