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

相关推荐
小奋斗8 分钟前
深入浅出:JavaScript中防抖与节流详解
javascript·面试
Wcy30765190668 分钟前
web前端第二次作业
前端·javascript·css
何妨重温wdys35 分钟前
矩阵链相乘的最少乘法次数(动态规划解法)
c++·算法·矩阵·动态规划
重启的码农36 分钟前
ggml 介绍 (6) 后端 (ggml_backend)
c++·人工智能·神经网络
重启的码农37 分钟前
ggml介绍 (7)后端缓冲区 (ggml_backend_buffer)
c++·人工智能·神经网络
Zestia1 小时前
页面点击跳转源代码?——element-jumper插件实现
前端·javascript
PineappleCoder1 小时前
大小写 + 标点全搞定!JS 如何精准统计单词频率?
前端·javascript·算法
KasukabeTsumugi1 小时前
TypeScript:联合类型可以转化为元组类型吗?数组如何用联合类型逐项约束?
javascript
雨落倾城夏未凉1 小时前
5.通过拷贝构造函数复制一个对象,假如对象的成员中有个指针类型的变量,如何避免拷贝出来的副本中的该成员之下行同一块内存(等价于默认拷贝构造函数有没有缺点)
c++·后端
雨落倾城夏未凉1 小时前
4.深拷贝VS浅拷贝
c++·后端