Processing练习之鼠标控制线段粗细

案例代码如下:

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

相关推荐
码云社区1 分钟前
JAVA二手车交易二手车市场系统源码支持微信小程序+微信公众号+H5+APP
java·开发语言·微信小程序·二手交易·闲置回收
crescent_悦2 分钟前
C++:The Largest Generation
java·开发语言·c++
indexsunny14 分钟前
互联网大厂Java面试实战:从Spring Boot到微服务的技术问答解析
java·spring boot·redis·微服务·消息队列·电商
paeamecium1 小时前
【PAT甲级真题】- Student List for Course (25)
数据结构·c++·算法·list·pat考试
Book思议-1 小时前
【数据结构】栈与队列全方位对比 + C 语言完整实现
c语言·数据结构·算法··队列
SteveSenna1 小时前
项目:Trossen Arm MuJoCo
人工智能·学习·算法
NAGNIP1 小时前
一文搞懂CNN经典架构-DenseNet!
算法·面试
希望永不加班1 小时前
SpringBoot 过滤器(Filter)与请求链路梳理
java·spring boot·后端·spring
道法自然|~1 小时前
BugCTF黄道十二宫
算法·密码学
Lyyaoo.1 小时前
【JAVA基础面经】抽象类/方法与接口
java·开发语言