案例代码如下:
import processing.pdf.*;
import java.util.Calendar;
boolean recordPDF = false;
void setup(){
size(720,720);
smooth();
noFill();
background(255);
}
void draw(){
if(mousePressed){
translate(width/2,height/2);
int circleResolution=(int)map(mouseY,0,height-50,3,11);
float radius = mouseX-width/2+0.5;
float angle =TWO_PI/circleResolution;
strokeWeight(2);
stroke(0,25);
beginShape();
for(int i=0;i<=circleResolution;i++)
{
float x=0+cos(angle*i)*radius;
float y=0+sin(angle*i)*radius;
vertex(x,y);
}
endShape();
}
}
void keyReleased(){
if(key==DELETE||key==BACKSPACE) background(255);
if(key=='s'||key=='S')saveFrame(timestamp()+"_##.png");
if(key=='r'||key=='R'){
if(recordPDF==false){
beginRecord(PDF,timestamp()+".pdf");
println("recording started");
recordPDF=true;
smooth();
noFill();
background(255);
}
}else if(key=='e'||key=='E'){
if(recordPDF)
{
println("recording stopped");
endRecord();
recordPDF=false;
background(255);
}
}
}
String timestamp(){
Calendar now =Calendar.getInstance();
return String.format("%1$ty%1$tm%1$td_%1$tH%1$tM%1$tS",now);
}
保存并运行如图1
图1