Week 10
教學內容為複習前幾周以及text和textSize(顯示文字和文字內容,可用fill改變顏色),frameCount計數frame,mills計數,hour,minute,second分別顯示時分秒,textFont更改字型,PFont,字串,倒計時互動,用圖片秀字
1.textSize和text顯示文字和文字內容
textSize可以調整文字大小,text可以顯示文字內容,50,50為text的位置
size(500,500);
fill(255,0,0);//填充顏色
textSize(80);//文字大小
text("Hello",50,50);//文字內容與位置
2.準備互動加上setup和draw準備後續
void setup(){
size(500,500);
fill(255,0,0);//填充顏色
textSize(80);//文字大小
}
void draw(){
text("Hello",50,100);//文字內容與位置
}
3.frameCount計數frameframeCount顯示當前frame數,1秒60frame,用+連接
void setup(){
size(500,500);
fill(255,0,0);//填充顏色
textSize(80);//文字大小
}
void draw(){
background(255);//白
text("Time"+frameCount,50,100);//文字內容與位置
}//每秒60frame,過了幾個frame
4.mills較為準確的計時
frameCount會delay,比較不準確,通常會使用mills()計秒數
void setup(){
size(500,500);
fill(255,0,0);//填充顏色
textSize(80);//文字大小
}
void draw(){
background(255);//白
text("Time:"+frameCount/60,50,100);//不太準確,會delay
text("Mills:"+millis()/1000,50,200);//較為準確
}//mills千分
5.hour()minute()和second()
使用hour,minute,second函式呼叫,用text顯示出時分秒
void setup(){
size(500,500);
fill(255,0,0);//填充顏色
textSize(80);//文字大小
}
void draw(){
background(255);//白
text(hour()+":"+minute()+":"+second(),50,100);//時分秒
text("Mills:"+millis()/1000,50,200);//較為準確
}//mills千分
目前暫時不能顯示中文,需要之後加程式
text("中文"+"Mills:"+millis()/1000,50,200);
7.印出系統目前所有字型println(PFont.list());
使用PFont,creatFont,textFont印出中文字
PFont myFont;//宣告變數myFont
void setup(){
size(500,500);
myFont=createFont("標楷體",100);//字型名稱以及大小
textFont(myFont);//更改字型為標楷體
text("中文",100,100);
}
宣告兩個變數,使用mousePressed函式進行切換
PFont font1,font2;//宣告多個變數
void setup(){
size(500,500);
font1=createFont("標楷體",100);
textFont(font1);
font2=createFont("微軟正黑體 Bold",100);
}
void draw(){
background(0);
text("中文",100,100);
if(mousePressed) textFont(font2);//按下切換為font2
else textFont(font1);
}
10.字串使用字串String產生%02d的效果
void setup(){
size(500,500);
fill(255,0,0);//填充顏色
textSize(80);//文字大小
}
void draw(){
background(255);//白
String hh=nf(hour(),2);
String mm=nf(minute(),2);
String ss=nf(second(),2);
text(hh+":"+mm+":"+ss,50,100);
}
11.倒計時換算現在和下課,相減得出倒計時
void setup(){
size(500,500);
fill(255);//填充顏色
textSize(80);//文字大小
}
void draw(){
background(#075BB2);
String hh=nf(hour(),2);
String mm=nf(minute(),2);
String ss=nf(second(),2);
text(hh+":"+mm+":"+ss,50,100);
text("15:40:00",50,200);
int now=hour()*60*60+minute()*60+second();//計算現在時間
int next=15*60*60+40*60+0;//換算下課時間
int remain=next-now;//相減
text(remain,50,300);
}
12.修改倒計時格式將倒計時改爲hour,minute,second的形式,使用餘數和除法
PFont font1;
void setup(){
size(800,500);
fill(255);//填充顏色
textSize(80);//文字大小
}
void draw(){
background(#075BB2);
String hh=nf(hour(),2);
String mm=nf(minute(),2);
String ss=nf(second(),2);
font1=createFont("標楷體",100);
textFont(font1);
text("現在:"+hh+":"+mm+":"+ss,50,100);
text("上課:15:50:00",50,200);
int now=hour()*60*60+minute()*60+second();
int next=15*60*60+50*60+0;
int remain=next-now;
text("剩餘:"+remain,50,300);
hh=nf(remain/60/60%60,2);//取餘數
mm=nf(remain/60%60,2);
ss=nf(remain%60,2);
text("剩下:"+hh+":"+mm+":"+ss,50,400);
}
for回圈按順序顯示數字0-9
PImage []img=new PImage[10];
void setup(){
size(1120,450);
for(int i=0;i<10;i++) img[i]=loadImage(i+".png");//載入圖片檔案名稱
}
void draw(){
for(int i=0;i<10;i++){
int x=(i%5)*224 , y=(i/5)*225;//圖片位置x,y
image(img[i],x,y);
}
用圖片顯示出倒計時的數字
PImage [] img = new PImage[10];
PFont font;
void setup(){
size( 1120, 450 );//把全部的圖,都秀出來
for(int i=0; i<10; i++) img[i]=loadImage(i+".png");
fill(255);
font = createFont("標楷體",80);
textFont(font);
}
void draw(){
background(#075BB2);
String hh=nf(hour(),2);
String mm=nf(minute(),2);
String ss=nf(second(),2);
text("現在:"+hh+":"+mm+":"+ss, 50,100);
text("下課:15:40:00", 50,200);
int now = hour()*60*60 + minute()*60 + second();//現在總秒數
int next = 16 *60*60 + 40 *60 + 0;//下課總秒數
int remain = next-now;//剩下的時間 (秒數來算)
if(remain<0) remain += 24*60*60;
text("剩下:"+remain, 50,300);
hh = nf(remain/60/60%60, 2);
mm = nf(remain/60%60, 2);
ss = nf(remain%60, 2);
text("剩下:"+hh+":"+mm+":"+ss, 50,400);
int x=800;
while(remain>0){
int n = remain%10;//得到餘數
remain = remain / 10;//取上一位數
image( img[n], x,100,100,100);
x-=100;//x的位置往左移動100
}
}
沒有留言:
張貼留言