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

相关推荐
m0_736927049 分钟前
想抓PostgreSQL里的慢SQL?pg_stat_statements基础黑匣子和pg_stat_monitor时间窗,谁能帮你更准揪出性能小偷?
java·数据库·sql·postgresql
Jabes.yang12 分钟前
Java面试大作战:从缓存技术到音视频场景的探讨
java·spring boot·redis·缓存·kafka·spring security·oauth2
Query*26 分钟前
Java 设计模式——适配器模式进阶:原理深挖、框架应用与实战扩展
java·设计模式·适配器模式
PyHaVolask28 分钟前
数据结构与算法分析
数据结构·算法·图论
小王C语言28 分钟前
封装红黑树实现mymap和myset
linux·服务器·算法
Sirens.36 分钟前
Java核心概念:抽象类、接口、Object类深度剖析
java·开发语言·github
Meteors.36 分钟前
23种设计模式——中介者模式 (Mediator Pattern)详解
java·设计模式·中介者模式
望获linux38 分钟前
【实时Linux实战系列】使用 u-trace 或 a-trace 进行用户态应用剖析
java·linux·前端·网络·数据库·elasticsearch·操作系统
焰火19991 小时前
[Java]基于Spring的轻量级定时任务动态管理框架
java·后端
大佬,救命!!!1 小时前
算法实现迭代2_堆排序
数据结构·python·算法·学习笔记·堆排序