使用Processing制作一个时钟

使用Processing制作一个时钟

第一次写文章,有点小激动,话不多说,现在开始~

近期开始学习计算机动画,使用的是processing软件,软件的下载和安装都非常简单,今天贴在这里的是使用processing写的第一次作业——一个时钟动画

运行结果

附上运行结果的图片,图片是静态的,实际的效果是动态的,时间与北京时间一致
在这里插入图片描述

编程代码

直接贴上代码.


Boolean flag = true;

void setup () {
  size(500, 500);
}

class Time {
  int currentSecond; // second
  int currentMinute; // minute
  int currentHour; // hour
  float currentMinuteFloat; // minute,minute() + currentSecond / 60.0;
  float currentHourFloat; // hour,hour() + currentMinuteFloat / 60.0;

  Time(int s, int m, int h, float mf, float hf) {
    currentSecond = s;
    currentMinute = m;
    currentHour = h;
    currentMinuteFloat = mf;
    currentHourFloat = hf;
  }

}

void draw () {
  // calculate current time
  int s = second();
  int m = minute();
  int y = hour();
  float mf = minute() + s / 60.0;
  float hf = hour() + mf / 60.0;
  Time time = new Time(s, m, y, mf, hf);
  // clock  
  drawClock();
  // date
  showDate();  
  // hour
  hourPointer(time);
  // minute
  minutePointer(time);  
  // second and ellipse
  secondPointer(time);
}

//表盘
void drawClock() {
  ellipseMode(CENTER);
  strokeWeight(10);
  stroke(0);
  fill(255);
  ellipse(width/2, height/2, 270, 270); 

  for (int i = 1; i <= 60; i++) {
    pushMatrix(); 
    translate(width/2, height/2);  
    rotate(radians(i * 6));
    if (i % 5 == 0) { 
      strokeWeight(3);
      line(120, 0, 130, 0);
    } else { 
      strokeWeight(1);
      line(125, 0, 130, 0);
    }
    popMatrix(); 
  }

  // 0-12
  textSize(25);
  fill(0);
  stroke(255);
  strokeWeight(1);
  text(12, 235, 155); //12
  text(3, 350, 260) ;//3
  text(9, 135, 260) ;//9
  text(6, 245, 365) ;//6
}



// start or not
void keyPressed() {
  if (key == 's' || key == 'S') {
    flag = true;
  } else if (key=='t' || key=='T') {
    flag = false;
  }
}


// show current date
void showDate() {
  String currentDate = year() + "-" + month() + "-" + day();
  textSize(25);
  text(currentDate, width - 320, 220);
}



// hour
void hourPointer(Time time) {
  pushMatrix();
  translate(width/2, height/2);
  float angleHour = radians(270);
  if ((time.currentHourFloat >= 3 && time.currentHourFloat <= 12) || (time.currentHourFloat >= 15 && time.currentHourFloat <= 24)) {
    angleHour = radians(30 * (time.currentHourFloat - 3));
  } else {
    angleHour = radians(30 * (time.currentHourFloat - 1) + 300);
  }
  if (flag) { 
    rotate(angleHour);
  } else {
    rotate(radians(270));
  }
  stroke(0);
  strokeWeight(6.5);
  line(0, 0, 70, 0);
  popMatrix();
}



// minute
void minutePointer(Time time) {
  pushMatrix();
  translate(width/2, height/2);
  float angleMinute = radians(270);
  if ((time.currentMinuteFloat >= 0 && time.currentMinute <= 15)) {
    angleMinute = radians(270 + 6 * time.currentMinuteFloat);
  } else {
    angleMinute = radians(6 * (time.currentMinuteFloat - 15));
  }
  if (flag) { 
    rotate(angleMinute);
  } else {
    rotate(radians(0));
  }
  stroke(0);
  strokeWeight(4);
  line(0, 0, 90, 0);
  popMatrix();
}



// second and ellipse
void secondPointer(Time time) {
  pushMatrix();
  translate(width/2, height/2);
  float angleSecond = radians(270);
  if ((time.currentSecond >= 0 && time.currentSecond <= 15)) {
    angleSecond = radians(270 + 6 * time.currentSecond);
  } else {
    angleSecond = radians(6 * (time.currentSecond - 15));
  }
  if (flag) { 
    rotate(angleSecond);
  } else {
    rotate(radians(180));
  }
  stroke(0, 0, 0);
  strokeWeight(1.5);
  line(0, 0, 115, 0);
  fill(255, 0, 0);
  popMatrix();
}

结束了,第一次写文章,希望对阅读之后的你有些许帮助~~

  • 6
    点赞
  • 36
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值