模糊边缘不断恢复 100% 不透明度(处理中)

我试图通过以下代码在处理中创建边缘模糊的圆形画笔。圆形是逐像素绘制的,因为在实际版本中,我尝试使用从PGraphic pg.


PFont font;

PGraphics pg;

int X;

int Y;

int rad = 20;


void setup (){

    size(800, 800, P2D);

    background(0);

    noStroke();


    pg = createGraphics(800, 800, JAVA2D);

    pg.beginDraw();

    pg.fill(255);

    pg.noStroke();

    pg.textFont(font);

    pg.textSize(400);

    pg.pushMatrix();

    pg.translate(width/2, height/2-140);

    pg.textAlign(CENTER, CENTER);

    pg.text("b", 0 , 0);

    pg.popMatrix();

    pg.endDraw();

}


void draw () {

    image(pg,0,0);

}


void mousePressed(){

    X = mouseX;

    Y = mouseY;

}


void mouseDragged(){


    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(mouseX+x,mouseY+y,color(255,255,255, alpha));

                pg.set(mouseX-x,mouseY+y,color(255,255,255, alpha));

                pg.set(mouseX+x,mouseY-y,color(255,255,255, alpha));

                pg.set(mouseX-x,mouseY-y,color(255,255,255, alpha));

                pg.endDraw();

            }

        }

    }

}

https://img1.mukewang.com/650ab4e300010fd504620484.jpg

泛舟湖上清波郎朗
浏览 86回答 2
2回答

牧羊人nacy

创建一个在对象上绘制单个点的函数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在其中的单独对象上绘制一个点setupPGraphics 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对象 ( ):pgvoid mouseDragged(){    pg.beginDraw();    pg.image(pg_pen, mouseX-rad, mouseY-rad);    pg.endDraw();}为了追求draw功能的完整性:void draw () {    background(0);     image(pg,0,0);}[...]并尝试从白色部分获取颜色以在黑色部分上绘制。color向该函数添加一个参数,并在绘制之前DrawPen清除笔:PGraphicsvoid 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);    }}

缥缈止盈

添加“背景(0);” 在“图像(pg,0,0);”之前 在你的绘图方法中,这样你每次都会重置背景,如果你不这样做,程序将继续在彼此之上绘制每帧图像,这将使低不透明度像素每帧缓慢获得不透明度,直到达到 100 %void draw () {&nbsp; background(0);&nbsp; 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 (){&nbsp; size(800, 800, P2D);&nbsp; background(0);&nbsp; noStroke();&nbsp; pg_all = createGraphics(800, 800, JAVA2D);&nbsp; pg_all.beginDraw();&nbsp; pg_all.endDraw();&nbsp; pg = createGraphics(800, 800, JAVA2D);&nbsp; pg.beginDraw();&nbsp; for (int x=0; x<rad; x++) {&nbsp; &nbsp; for (int y=0; y<rad; y++) {&nbsp; &nbsp; &nbsp; float distance = sqrt(pow(x,2)+pow(y,2));&nbsp; &nbsp; &nbsp; float alpha = 255-map(distance,0,rad,0,255);&nbsp; &nbsp; &nbsp; if (sqrt(pow(x,2)+pow(y,2)) < rad){&nbsp; &nbsp; &nbsp; &nbsp; pg.beginDraw();&nbsp; &nbsp; &nbsp; &nbsp; pg.set(20+x,20+y,color(255,255,255, alpha));&nbsp; &nbsp; &nbsp; &nbsp; pg.set(20-x,20+y,color(255,255,255, alpha));&nbsp; &nbsp; &nbsp; &nbsp; pg.set(20+x,20-y,color(255,255,255, alpha));&nbsp; &nbsp; &nbsp; &nbsp; pg.set(20-x,20-y,color(255,255,255, alpha));&nbsp; &nbsp; &nbsp; &nbsp; pg.endDraw();&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; }&nbsp; pg.endDraw();}void draw () {&nbsp; &nbsp;&nbsp; background(0);&nbsp; image(pg_all,0,0);&nbsp;}void mouseDragged(){&nbsp; X = mouseX;&nbsp; Y = mouseY;&nbsp; pg_all.beginDraw();&nbsp; pg_all.image(pg,X-rad,Y-rad);&nbsp; pg_all.endDraw();}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java