Processing 时钟

参考了别人家的时钟代码,添加了表盘,日期(星期和农历暂未添加),效果如下:
Processing时钟
代码如下:

int y,mo,d,h,m,s;   //y:年 mo:月 d:日 h:时 m:分 s:秒
PFont myFont;

void setup(){
    size(300,300);
    background(204);
}

void draw(){
    background(204);
    s = second();   // Values from 0 - 59
    m = minute();   // Values from 0 - 59
    h = hour();     // Values from 0 - 23
    d = day();      // Values from 1 - 31
    mo = month();   // Values from 1 - 12
    y = year();     // 2014, 2015, 2016 etc.

    myFont=createFont("FFScala",18);    //字体和大小
    myClockDraw();
}

void myClockDraw(){
    translate(width/2,height/2);    //移动原点到坐标中心
    fill(255);  //white
    ellipse(0,0,200,200);
    //Sets the color used to draw lines and borders around shapes. 
    stroke(0);      //Black
    textFont(myFont);   //使用上面定义的字体
    fill(0);    //Black
    text("12",-10,-75);//在指定坐标位置显示字符串
    text("3",78,6);
    text("6",-7,88);
    text("9",-88,6);
    text(y+"-"+mo+"-"+d,-40,-23);   //显示 年-月-日

    //绘制表盘
    for(int i=1;i<=60;i++){
        pushMatrix();
        rotate(PI*2.0*i/60.0);  //也可使用radians()函数把角度转换为弧度
        stroke(0);
        if(i%15==0){
            strokeWeight(3);
            line(0,-90,0,-100);
        }else if( i%5 ==0){
            strokeWeight(2);
            line(0,-92,0,-100);
        }else{
            strokeWeight(1);
            line(0,-95,0,-100);
        }
        popMatrix();
    }

    //The pushMatrix() function saves the current coordinate system 
    //to the stack and popMatrix() restores the prior coordinate system.
    //和popMatrix()联用,因为出现了rotate()函数
    pushMatrix();   
    //把秒数转换为弧度(rotate()范围在0~2*PI之间)
    //基准线为为下面的line()的方向,顺时针为正方向
    rotate(PI*2*s/60+PI);   
    stroke(0);  //Black
    strokeWeight(1);    //线宽为1
    line(0,0,0,90);     //秒针
    popMatrix();

    pushMatrix();
    rotate(PI*2*m/60+PI);
    stroke(0);
    strokeWeight(3);
    line(0,0,0,70);     //分针
    popMatrix();

    pushMatrix();
    rotate(PI*2*h/12+PI);
    stroke(0);
    strokeWeight(5);
    line(0,0,0,50);     //时针
    popMatrix();

}

参考自(看链接,似乎是来自台湾 昆山科技大学):
Processing-时钟

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值