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

相关推荐
用户283209679373 分钟前
从进程线程到 async/await:JS Event-loop事件循环机制底层解析
前端·javascript
guanguan0_015 分钟前
用 AI 做技术方案评审:输入 3 个方案,输出对比矩阵 + 推荐理由
javascript·人工智能·矩阵·ai编程
小欣加油17 分钟前
Leetcode2058 找出临界点之间的最小和最大距离
数据结构·c++·算法·leetcode·职场和发展
王老师青少年编程34 分钟前
2026年山东省小学组【信息学体验营】复赛真题及题解T2:小兔子爬楼梯
c++·真题·山东省·csp·信奥赛·复赛·信息学体验营
Yfhonier37 分钟前
6441. 特别的除法
c++·算法
xiaominlaopodaren1 小时前
three.js地图视口瓦片(二):如何形成完整视口多边形
javascript·gis·three.js
OPEN-F1 小时前
C++17新特性精讲:结构化绑定、if constexpr与实用类型
开发语言·c++
OPEN-F1 小时前
C++并发编程:线程与互斥锁
开发语言·c++
码匠许师傅1 小时前
【设计模式精讲】11.桥接模式(Bridge)
c++·设计模式·桥接模式·uml
小小、码农1 小时前
【网络】套接字(Socket)编程——TCP版
linux·服务器·网络·c++·网络协议·tcp/ip