案例代码如下:
import processing.pdf.*;
import java.util.Calendar;
boolean savePDF = false;
int tileCount = 20;
int actRandomSeed =0;
int actStrokeCap = ROUND;
void setup(){
size(600,600);
}
void draw(){
if(savePDF)beginRecord(PDF, timestamp()+".pdf");
background(255);
smooth();
noFill();
strokeCap(actStrokeCap);
randomSeed(actRandomSeed);
for (int gridY=0; gridY<tileCount; gridY++)
{
for (int gridX=0; gridX<tileCount; gridX++)
{
int posX = width/tileCount*gridX;
int posY = height/tileCount*gridY;
int toggle =(int)random(0,2);
if(toggle == 0){
strokeWeight(mouseX/20);
line (posX, posY,posX+width/tileCount,posY+height/tileCount);
}
if(toggle == 1){
strokeWeight(mouseY/20);
line(posX, posY+height/tileCount, posX+width/tileCount, posY);
}
}
}
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;
if (key == '1'){
actStrokeCap = ROUND;
}
if (key =='2'){
actStrokeCap= SQUARE;
}
if (key == '3'){
actStrokeCap = PROJECT;
}
}
String timestamp(){
Calendar now= Calendar.getInstance();
return String.format("%1ty%1tm%1td_%1tH%1tM%1tS",now);
}
保存并运行如图1
图1