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 小时前
C++从入门到实战(十)类和对象(最终部分)static成员,内部类,匿名对象与对象拷贝时的编译器优化详解
java·数据结构·c++·redis·后端·算法·链表
一 乐1 小时前
网红酒店|基于java+vue的网红酒店预定系统(源码+数据库+文档)
java·开发语言·数据库·毕业设计·论文·springboot·网红酒店预定系统
写bug的小屁孩1 小时前
移动零+复写零+快乐数+盛最多水的容器+有效三角形的个数
c++·算法·双指针
飞川撸码1 小时前
【LeetCode 热题100】208:实现 Trie (前缀树)(详细解析)(Go语言版)
算法·leetcode·golang·图搜索算法
这就是编程2 小时前
自回归模型的新浪潮?GPT-4o图像生成技术解析与未来展望
人工智能·算法·机器学习·数据挖掘·回归
羑悻的小杀马特2 小时前
【狂热算法篇】探寻图论幽径:Bellman - Ford 算法的浪漫征程(通俗易懂版)
c++·算法·图论·bellman_ford算法
xyliiiiiL3 小时前
从责任链模式聊到aware接口
java·开发语言
Fantasydg5 小时前
DAY 31 leetcode 142--链表.环形链表
算法·leetcode·链表
basketball6165 小时前
C++ STL常用算法之常用排序算法
c++·算法·排序算法
qystca6 小时前
蓝桥云客 岛屿个数
算法·dfs·bfs