Processing用线段绘画

案例代码如下:

import processing.pdf.*;
import java.util.Calendar;

boolean recordPDF = false;
 color col = color(181,157,0,100);
 float lineLength =0;
 float angle = 0;
 float angleSpeed=1.0;

void setup(){
  size(displayWidth,displayHeight);
  background(255);
  smooth();
  cursor(CROSS);
  }
   void draw(){
    if(mousePressed){
      pushMatrix();
      strokeWeight(1.0);
      noFill();
      stroke(col);
      translate(mouseX,mouseY);
      rotate(radians(angle));
      line(0,0,lineLength,0);
      popMatrix();
      angle +=angleSpeed;
    }
   }
  void mousePressed(){
    lineLength = random(70,200);
  }
   void keyReleased(){
     if(key==DELETE||key==BACKSPACE) background(255);
     if(key=='s'||key=='S') saveFrame(timestamp()+"_##.png");
     if(key=='d'||key=='D'){
       angle=angle+180;
       angleSpeed = angleSpeed*-1;
   }
      if(key==' ') col = color(random(255),random(255),random(255),random(80,150));
      if(key=='1') col = color(181,157,0,100);
      if(key=='2') col = color(0,130,164,100);
      if(key=='3') col = color(87,35,129,100);
      if(key=='4') col = color(197,0,123,100);
      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) lineLength +=5;
       if(keyCode ==DOWN) lineLength -=5;
       if(keyCode ==LEFT) angleSpeed -=0.5;
       if(keyCode ==RIGHT) angleSpeed +=0.5;
     }
    String timestamp(){
  Calendar now=Calendar.getInstance();
  return String.format("%1$ty%1$tm%1$td_%1$tH%1$tM%1$tS",now);
}

       画的不满意可以按Delete或者Backspace清屏,按小写e或者大写E可以保存PDF,按小写s或者大写S可以保存PNG。

       代码保存如图1

                                       图1

  • 15
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 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、付费专栏及课程。

余额充值