gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:05:18 25 4
gpt4 key购买 nike

我正在尝试通过以下代码在 Processing 中创建一个边缘模糊的圆形画笔。圆形是逐像素绘制的,因为在实际版本中,我尝试使用从 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();
}
}
}
}

最佳答案

创建一个将单个点绘制到 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();
}

setup 中为单独的 PGraphics 对象画一个点

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);
}


[...] and tried to get the color from the white part to draw on the black part.

DrawPen 函数添加一个 color 参数并在绘制之前清除笔 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);
}
}

关于java - 模糊的边缘不断恢复 100% 的不透明度(处理中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58213839/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com