欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 汽车 > 时评 > Processing圆圈随鼠标运动

Processing圆圈随鼠标运动

2025/2/24 7:39:34 来源:https://blog.csdn.net/m0_65111980/article/details/141196681  浏览:    关键词:Processing圆圈随鼠标运动

一.案例代码如下:

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

boolean savePDF = false;
float tileCount =20;
color circleColor = color(0);
int circleAlpha = 180;
int actRandomSeed =0;

void setup(){
  size(600,600);
}
void draw(){
  if(savePDF)beginRecord(PDF,timestamp()+".pdf");
  translate(width/tileCount/2,height/tileCount/2);
  background(255);
  smooth();
  noFill();
  randomSeed(actRandomSeed);
  stroke(circleColor,circleAlpha);
  strokeWeight(mouseY/60);
  for(int gridY=0;gridY<tileCount;gridY++){
   for(int gridX=0;gridX<tileCount;gridX++){
     float posX=width/tileCount*gridX;
     float posY =height/tileCount*gridY;
     float shiftX=random(-mouseX,mouseX)/20;
     float shiftY=random(-mouseX,mouseX)/20;
     ellipse(posX+shiftX,posY+shiftY,mouseY/15,mouseY/15);
   }
  }
  if(savePDF)
  {savePDF=false;
  endRecord();
  }
}
void mousePressed(){
  actRandomSeed=(int) random(100000);
}

void keyReleased(){
  if(key=='s'||key=='S') saveFrame(timestamp()+"_##.png");
  if(key=='p'||key=='P') savePDF=true;
}
String timestamp(){
  Calendar now=Calendar.getInstance();
  return String.format("%1$ty%1$tm%1$td_%1$tH%1$tM%1$tS",now);
}

       保存并运行如图1

                                     图1

       注意:案例中按下大写P或者小写p保存PDF,如果按下大写S或者小写s可以保存PNG。运行时大家可以多动动鼠标,会发现有趣的变化。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com