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

相关推荐
CQU_JIAKE几秒前
11.1[A]
算法
文丑颜不良啊几秒前
LeetCode1581~1600题目解析
java·leetcode
香蕉大香蕉(06敬畏市场)1 分钟前
MySQL 单点详解(06)
java·数据库·mysql·博客·实习
小郎君65810 分钟前
(六)el-cascader 的展示值为什么不能直接 join:emitPath、路径还原与多选去重
vue.js·算法
lqbzlll14 分钟前
Spring Boot 开启虚拟线程后接口反而变慢了?4 个排查方向和对应修法
java·spring boot·后端
七夜zippoe19 分钟前
LangChain4j vs Spring AI:Java AI 框架怎么选(对比表)
java·人工智能·spring·ai·langchain4j
悠仁さん24 分钟前
【C++】set map 使用
java·前端·c++
yuniko-n1 小时前
【JUC】Thread 内存泄露
java
freejackman1 小时前
【架构篇】双遍历 + 多栈协同:手撸 SQL 血缘引擎的架构思考
java·大数据·数据库·hive·sql·架构·spark
企业数字化笔记1 小时前
业务单据怎样接入流程中心?启动条件、实例关联与失败回退
java·spring boot·mysql