实验五之用Processing绘画

1.案例代码如下:

import generativedesign.*;

import processing.pdf.*;

import java.util.Calendar;

Tablet tablet;

boolean recordPDF = false;

float x = 0, y = 0;

float stepSize = 5.0;

PFont font;

String letters = "Sie hören nicht die folgenden Gesänge, Die Seelen, denen ich die ersten sang,Zerstoben ist das freundliche Gedränge, Verklungen ach! der erste Wiederklang.";

int fontSizeMin = 15;

float angleDistortion = 0.0;

int counter = 0;

void setup() {

size(displayWidth, displayHeight);

background(255);

smooth();

tablet = new Tablet(this);

x = mouseX;

y = mouseY;

font = createFont("ArnhemFineTT-Normal",10);

textFont(font,fontSizeMin);

cursor(CROSS);

}

void draw() {

if (mousePressed) {

float pressure = gamma(tablet.getPressure()*1.1, 2.5);

float d = dist(x,y, mouseX,mouseY);

textFont(font,fontSizeMin+ 200 * pressure);

char newLetter = letters.charAt(counter);

stepSize = textWidth(newLetter);

if (d > stepSize) {

float angle = atan2(mouseY-y, mouseX-x);

pushMatrix();

translate(x, y);

rotate(angle + random(angleDistortion));

fill(0);

textAlign(LEFT);

text(newLetter, 0, 0);

popMatrix();

counter++;

if (counter > letters.length()-1) counter = 0;

x = x + cos(angle) * stepSize;

y = y + sin(angle) * stepSize;

}

}

}

void mousePressed() {

x = mouseX;

y = mouseY;

}

void keyReleased() {

if (key == 's' || key == 'S') saveFrame(timestamp()+"_##.png");

if (key == DELETE || key == BACKSPACE) background(255);

if (key =='r' || key =='R') {

if (recordPDF == false) {

beginRecord(PDF, timestamp()+".pdf");

println("recording started");

recordPDF = true;

}

}

else if (key == 'e' || key =='E') {

if (recordPDF) {

println("recording stopped");

endRecord();

recordPDF = false;

background(255);

}

}

}

void keyPressed() {

if (keyCode == UP) angleDistortion += 0.1;

if (keyCode == DOWN) angleDistortion -= 0.1;

}

float gamma(float theValue, float theGamma) {

return pow(theValue, theGamma);

}

String timestamp() {

Calendar now = Calendar.getInstance();

return String.format("%1ty%1tm%1td_%1tH%1tM%1tS", now);

}

保存并运行发现是空白的,此时只需要用鼠标绘画出自己想要的内容即可。我写的是乒乓球世界第一孙颖莎的大写字母。如图1

图1

绘画完成后想保持PNG可以按S或者小写s,想保存PDF按大写E或者小写e。如果觉得画的不好看,按下Backspace可以清屏。如果发现报错没关系,可以运行成功并进行绘画。

相关推荐
阿正的梦工坊3 分钟前
JavaScript 微任务与宏任务完全指南
开发语言·javascript·ecmascript
自信1504130575942 分钟前
重生之从0开始学习c++之模板初级
c++·学习
历程里程碑1 小时前
2. Git版本回退全攻略:轻松掌握代码时光机
大数据·c++·git·elasticsearch·搜索引擎·github·全文检索
极客智造1 小时前
深度解析 C++ 类继承与多态:面向对象编程的核心
c++
2301_799073024 小时前
基于 Next.js + 火山引擎 AI 的电商素材智能生成工具实战——字节跳动前端训练营成果
javascript·人工智能·火山引擎
零号全栈寒江独钓4 小时前
基于c/c++实现linux/windows跨平台获取ntp网络时间戳
linux·c语言·c++·windows
CSCN新手听安4 小时前
【linux】高级IO,以ET模式运行的epoll版本的TCP服务器实现reactor反应堆
linux·运维·服务器·c++·高级io·epoll·reactor反应堆
松☆6 小时前
C++ 算法竞赛题解:P13569 [CCPC 2024 重庆站] osu!mania —— 浮点数精度陷阱与 `eps` 的深度解析
开发语言·c++·算法
kyriewen116 小时前
项目做了一半想重写?这套前端架构让你少走3年弯路
前端·javascript·chrome·架构·ecmascript·html5
(Charon)6 小时前
【C++/Qt】C++/Qt 实现 TCP Server:支持启动监听、消息收发、日志保存
c++·qt·tcp/ip