一.根据之前的圆盘进行创作彩虹
案例代码如下:
import processing.pdf.*;
import java.util.Calendar;
boolean savePDF=false;
int segmentCount = 360;
int radius = 300;
void setup(){
size(400,300);
}
void draw(){
if(savePDF) beginRecord(PDF,timestamp()+".pdf");
noStroke();
colorMode(HSB,360,width,height);
background(360);
float angleStep =360/segmentCount;
beginShape(TRIANGLE_FAN);
vertex(width/2,height+50);//圆的位置
for(float angle =0;angle<=360;angle+=angleStep){
fill(angle-4,mouseX,mouseY); //圆的颜色
ellipse(width/2,height+50,radius,radius);
fill(angle-320,mouseX,mouseY);
ellipse(width/2,height+50,radius-20,radius-20);
fill(angle-304,mouseX,mouseY);
ellipse(width/2,height+50,radius-40,radius-40);
fill(angle-277,mouseX,mouseY);
ellipse(width/2,height+50,radius-60,radius-60);
fill(angle-165,mouseX,mouseY);
ellipse(width/2,height+50,radius-80,radius-80);
fill(angle-131,mouseX,mouseY);
ellipse(width/2,height+50,radius-100,radius-100);
fill(angle-90,mouseX,mouseY);
ellipse(width/2,height+50,radius-120,radius-120);
fill(360);
ellipse(width/2,height+50,radius-140,radius-140);
noStroke(); //没有边框
fill(angle,mouseX,mouseY);
}
endShape();
if(savePDF){
savePDF=false;
endRecord();
}
}
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("%1$ty%1$tm%1$td_%1$tH%1$tM%1$tS",now);
}
保存并运行如图1
图1
注意:运行时,彩虹是黑色,当鼠标不断左右运动时,彩虹透明度发生变化。这个彩虹只有透明度跟随鼠标的变化而变化,其他的不变。因为我实现不出来那个按下1~5数字出现相关变化的代码。