Processing圆圈随鼠标运动

一.案例代码如下:

import processing.pdf.*;

import java.util.Calendar;

boolean savePDF = false;

float tileCount =20;

color circleColor = color(0);

int circleAlpha = 180;

int actRandomSeed =0;

void setup(){

size(600,600);

}

void draw(){

if(savePDF)beginRecord(PDF,timestamp()+".pdf");

translate(width/tileCount/2,height/tileCount/2);

background(255);

smooth();

noFill();

randomSeed(actRandomSeed);

stroke(circleColor,circleAlpha);

strokeWeight(mouseY/60);

for(int gridY=0;gridY<tileCount;gridY++){

for(int gridX=0;gridX<tileCount;gridX++){

float posX=width/tileCount*gridX;

float posY =height/tileCount*gridY;

float shiftX=random(-mouseX,mouseX)/20;

float shiftY=random(-mouseX,mouseX)/20;

ellipse(posX+shiftX,posY+shiftY,mouseY/15,mouseY/15);

}

}

if(savePDF)

{savePDF=false;

endRecord();

}

}

void mousePressed(){

actRandomSeed=(int) random(100000);

}

void keyReleased(){

if(key=='s'||key=='S') saveFrame(timestamp()+"_##.png");

if(key=='p'||key=='P') savePDF=true;

}

String timestamp(){

Calendar now=Calendar.getInstance();

return String.format("%1ty%1tm%1td_%1tH%1tM%1tS",now);

}

保存并运行如图1

图1

注意:案例中按下大写P或者小写p保存PDF,如果按下大写S或者小写s可以保存PNG。运行时大家可以多动动鼠标,会发现有趣的变化。

相关推荐
HoneyMoose4 小时前
体验一下科大讯飞的语音输入键盘
计算机外设
KL132881526934 小时前
显示器除了卓威还有泰坦,耳机除了飓风还有VSG
游戏·计算机外设·耳机
ACP广源盛139246256731 天前
GSV2231G@ACP#产品参数规格解析与应用分享
嵌入式硬件·计算机外设·音视频
Jwest20212 天前
工业显示器在淬火机床中的应用
计算机外设
TESmart碲视2 天前
2025年 顶级8K DP KVM切换器双PC双显示器解决方案
计算机外设
KL132881526932 天前
【无标题】
游戏·计算机外设·耳机
ACP广源盛139246256733 天前
GSV2125D@ACP#GSV6125#HDMI 2.0 转 DisplayPort 1.4 转换器(带嵌入式 MCU)
嵌入式硬件·计算机外设·音视频
上大科技蔡生3 天前
IU5925+CS2102 / CS2102 / IU5512:超级电容无线鼠标电源芯片方案
计算机外设·电脑·dcdc·超级电容·ldo·充电ic·无线鼠标电源方案
ACP广源盛139246256733 天前
GSV2202D@ACP#DisplayPort 1.4 到 HDMI 2.0 转换器(带嵌入式 MCU)
单片机·嵌入式硬件·计算机外设·音视频
划水的code搬运工小李3 天前
控制底盘采集环境数据(一)- 键盘控制
计算机外设