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。运行时大家可以多动动鼠标,会发现有趣的变化。

相关推荐
驱动开发0074 小时前
Windows_Hello_Configuration_Analysis Windows Hello 配置过程分析 setup包分析
windows·驱动开发·云计算·计算机外设·usb重定向
就是蠢啊6 小时前
51单片机——TFTLCD显示器(一)
嵌入式硬件·计算机外设·51单片机
uuleaf16 小时前
12款键盘练习打字小游戏:键盘练习游戏合集
游戏·计算机外设·编程打字
枫叶丹417 小时前
【Qt开发】Qt事件(三)-> QMouseEvent 鼠标事件
c语言·开发语言·c++·qt·microsoft·计算机外设
科技每日热闻1 天前
光影为序,专业为纲丨飞利浦商用显示器&尼康共绘影像创作新图景
科技·计算机外设·生活
禾仔仔2 天前
USB MSC从理论到实践(模拟U盘为例)——从零开始学习USB2.0协议(六)
嵌入式硬件·mcu·计算机外设
无限大.2 天前
为什么键盘有“机械“和“薄膜“之分?——按键的触感革命
计算机外设
小果子^_^3 天前
div或按钮鼠标经过或鼠标点击后效果样式
前端·css·计算机外设
LCG米3 天前
蓝牙HID设备开发:基于英飞凌CY8CKIT-149实现CapSense触摸控制的PC键盘
计算机外设
YJlio3 天前
文件工具学习笔记(12.8):MoveFile 实战——重启后重命名/删除顽固文件
笔记·学习·计算机外设