实验五之用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可以清屏。如果发现报错没关系,可以运行成功并进行绘画。

相关推荐
txzrxz3 分钟前
二分图详解
数据结构·c++·算法·图论·深度优先搜索·二分图染色
jinyishu_5 分钟前
C++ 继承全解:从基础到高级特性
开发语言·c++
是个西兰花32 分钟前
Linux:死锁与生产者消费者模型解析
linux·运维·服务器·c++·死锁·生产者消费者模型
柒和远方1 小时前
V043:BFF 层流式输出:从前端直连到生产级流式网关的架构演进
javascript·架构·node.js
用户938515635071 小时前
SSE 流式输出的工程化拐点——为什么你需要一个 BFF 层
javascript·人工智能·全栈
exm-zem2 小时前
从C语言到C++:C++入门1
c语言·c++
不简说2 小时前
JS 代码技巧 vol.4 — 10 个异步并发控制,Promise.all 这帮兄弟的踩坑实录
前端·javascript·面试
fpcc2 小时前
数据结构和算法—数学的应用
数据结构·c++·算法
皓月斯语2 小时前
B4451 [GESP202512 四级] 建造 题解
数据结构·c++·算法·题解
小喻同学i2 小时前
std::thread函数参数传递
开发语言·c++