实验五之用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("%1$ty%1$tm%1$td_%1$tH%1$tM%1$tS", now);
}

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

                                         图1

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

  • 18
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
使用 Processing 绘画梵高的星空,可以参考以下步骤: 1. 打开 Processing 开发环境,创建一个新的 Sketch,并保存为 `vangogh_star_sky`。 2. 在 Sketch 中,使用 `size()` 函数设置画布的大小,建议设置为 800x600。 ```processing void setup() { size(800, 600); } ``` 3. 添加一个变量 `starNum` 用于控制星星的数量,可以根据需要进行调整。 ```processing int starNum = 200; ``` 4. 创建一个 `Star` 类,用于表示星星的位置和大小。在 `Star` 类中,添加一个构造函数用于初始化星星的位置和大小,并添加一个 `show()` 方法用于绘制星星。 ```processing class Star { float x, y, size; Star() { x = random(width); y = random(height); size = random(1, 4); } void show() { noStroke(); fill(255, 255, 200); ellipse(x, y, size, size); } } ``` 5. 在 Sketch 中,创建一个 `stars` 数组,用于存储所有的星星,并在 `setup()` 函数中使用 `for` 循环创建指定数量的星星。 ```processing Star[] stars = new Star[starNum]; void setup() { size(800, 600); for (int i = 0; i < starNum; i++) { stars[i] = new Star(); } } ``` 6. 在 Sketch 中,使用 `draw()` 函数绘制星空。在 `draw()` 函数中,首先绘制背景色,然后遍历所有的星星并绘制出来。 ```processing void draw() { background(0); for (int i = 0; i < starNum; i++) { stars[i].show(); } } ``` 7. 最后,保存并运行代码,即可在屏幕上看到绘制出来的梵高星空。 完整代码如下: ```processing int starNum = 200; class Star { float x, y, size; Star() { x = random(width); y = random(height); size = random(1, 4); } void show() { noStroke(); fill(255, 255, 200); ellipse(x, y, size, size); } } Star[] stars = new Star[starNum]; void setup() { size(800, 600); for (int i = 0; i < starNum; i++) { stars[i] = new Star(); } } void draw() { background(0); for (int i = 0; i < starNum; i++) { stars[i].show(); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梦之妹

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值