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

相关推荐
初次见面我叫泰隆几秒前
Qt——1、初识Qt
开发语言·c++·qt
梦65015 分钟前
Vue 单页面应用 (SPA) 与 多页面应用 (MPA) 对比
前端·javascript·vue.js
兵哥工控16 分钟前
MFC 对话框Alt+F4退出程序实例
c++·mfc
清铎20 分钟前
大模型训练_week3_day15_Llama概念_《穷途末路》
前端·javascript·人工智能·深度学习·自然语言处理·easyui
王老师青少年编程30 分钟前
2024年9月GESP真题及题解(C++七级): 小杨寻宝
c++·题解·真题·gesp·csp·七级·小杨寻宝
岛泪34 分钟前
把 el-cascader 的 options 平铺为一维数组(只要叶子节点)
前端·javascript·vue.js
凯子坚持 c1 小时前
C++大模型SDK开发实录(一):spdlog日志封装、通用数据结构定义与策略模式应用
数据结构·c++·sdk·策略模式
lendsomething1 小时前
graalvm使用实战:在java中执行js脚本
java·开发语言·javascript·graalvm
小屁猪qAq1 小时前
设计模式总纲
开发语言·c++·设计模式
Howrun7771 小时前
C++标准线程库-全面讲解
开发语言·c++