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);
此外,每当按下任何鼠标(按下而不是拖动)时,颜色都会发生变化。可能您想在按下鼠标右键时更改颜色并在按下鼠标左键时进行绘制:
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();
}
添加回答
举报