แสดงบทความที่มีป้ายกำกับ Lab 2 แสดงบทความทั้งหมด
แสดงบทความที่มีป้ายกำกับ Lab 2 แสดงบทความทั้งหมด

วันอาทิตย์ที่ 30 สิงหาคม พ.ศ. 2558

Lab2_อาการ Syntax Error ต่างๆ

SYNTAX ERROR

Syntax error เกิดจากการที่เราเขียนโค้ดไม่ตรงตามหลักการเขียนที่โปรแกรมกำหนดเอาไว้ การเกิด syntax error ที่เกิดขึ้นได้บ่อยๆ มีดังนี้

1.Expecting EOF,found "}" หรือการใส่ปีกกาเกิน
-เกิดจากที่เราใส่ปีกกาเกินตัวฟังก์ชันของมัน
-แก้ไขโดยการลบปีกกาตัวที่เกินออกไป

2.Missing a semicolon ( ; ) หรือการลืมใส่ semicolon ;
- เกิดจากที่เราลืมใส่ semicolon ต่อท้ายฟังก์ชันบางตัว
-แก้ไขว่าลืมใส่ตรงไหน โดยดูที่แถบด้านขวาที่ขึ้นเป็นขีดๆแดงๆ จากนั้นก็ใส่ให้เรียบร้อย

3.Unexpected token: fffff หรือ การไม่พบสัญลักษณ์ที่เกิดขึ้น
-เกิดจากเราเขียนตัวหนังสือลงไปเลย โดยที่ไม่ใช่การคำนวณ ไม่ใช่ฟังก์ชัน ไม่ใช่สัญลักษณ์ของโปรแกรม
-แก้ไขโดยการลบตัวหนังสือนั้นออก

4.Cannot convert from float to int หรือ การที่ไม่สามารถเปลี่ยนชนิด class ได้
-เกิดจากการใช้ class ผิด Syntax ของมัน เช่น คำสั่ง random นั้น syntax ของมันคือ random(float,float) แต่เราใช้ไปเก็บค่าไว้ในตัวแปร int หรือ เรานำข้อมูล float ไปใส่ใน int มันจึงเก็บค่าไม่ได้
-แก้ไข้โดยการเปลี่ยนชนิดตัวแปรที่ไว้เก็บค่า ให้เป็นชนิด float จึงจะเก็บค่าได้

5. The variable "posX" does not exist หรือ การไม่พบตัวแปร
-เกิดจากเราไม่ได้กำหนดตัวแปรนั้น หรือ เราพิมชื่อตัวแปรนั้นผิด
-แก้ไขโดยเช็คชื่อตัวแปรนั้น หรือกำหนดตัวแปรนั้นขึ้นมา

6.การใส่ค่าไม่ครบใน Parameter
-เกิดจากเราใส่ค่าใน Parameter ไม่ครบจำนวนตามที่เรากำหนดไว้ เช่น void Positive(int posX , int posY); เรากำหนดตัว Argument ไว้เป็น posX และ posY เวลาเราเรียกใช้งานฟังก์ชัน เราใส่ Positive(50);  แบบนี้จะขึ้น syntax error เพราะว่าเรากำหนดไว้ให้ใส่ไป 2 ค่า แต่เราใส่แค่ค่าเดียว
-แก้ไขโดยการเช็คตัว Argument ว่าเรากำหนดไว้กี่ค่า จากนั้นก็ใส่ค่า Parameter ให้ครบตามที่เรากำหนดไว้

Lab2_Positive_Sign

Positvie Sign by Function

//Positive Sign
int colors=0;

void setup() {
  size(500, 500);
}
void draw() {
  background(#FF0D0D);//red
  positive();
}
void positive() {
  int dX=120;//diameterX
  int dY=160;//diameterY
  int w=100;//weight
  int h=100;//height
  //rectangular area
  stroke(255);
  strokeWeight(0);
  fill(colors);
  rect(dX, dY, w*3, h);//horizontal rect
  rect(dX+100, dY-100, w, h*3);//vertical rect
}
void mousePressed() {  

  if (colors == 0) {
    colors = 255;
  } else {
    colors = 0;
  }
}

Lab2_Battery_Function

Battery follow the Cursor!!

void setup() {
  size(500, 500);
}
void draw() {
  background(255);//white
  battery(mouseX, mouseY);
}
void battery(int countX, int countY) {
  int space=40;
  int w=180;//width
  int h=90;//height
  stroke(#0041E0);//dark blue
  strokeWeight(8);
  fill(#0393FF);//blue

  //big square
  rect(countX, countY, w, h);
  fill(255);//white
  //batt head
  rect(countX+180, countY+35, w/9, h-70);

  //battery quntity
  fill(#FF0011);//red
  rect(countX, countY, space, h);//#1 batt
  rect(countX+space, countY, space, h);//#2 batt
  rect(countX+space*2, countY, space, h);//#3 batt
  rect(countX+space*3, countY, space, h);//#4 batt
}

Lab2_BMI_Function

BMI's Calculation by Function

//BMI Function
void setup() {
  size(300,300);
  background(#FF8B8B);//pink
  calculation(80,175);
}
void calculation(int w,int h){
  float h_2=(h/100.0)*(h/100.0);//Height^2
  float BMI=w/h_2;//value of BMI
  //terminal output
  print("Value Of BMI="+BMI);
  //explaining text
  textSize(24);//size of text
  text("Weight = "+w+" Kg.",30,100);
  text("Height = "+h+" cm.",30,140);
  text("BMI="+BMI+"",30,180);
}

Lab2_Circle'sCalculation_Function

Circle's Calculation by Function

//Circle's Calculation
int posx=100;
int posy=100;
int dm=160 ;//diameter
void setup() {
  size(500, 500);
  circle(100, 100);
  calculation();
}


void circle(int posx, int posy) {
  background(#FC66C1);//pink <3
  ellipse(posx, posy, dm, dm);//example

}
void calculation() {
  float r=dm/2;//radius
  float r_2=r*r;//radius square
  float A=3.14*r_2;//area
  float cf=2*(3.14)*r;//circumference
  //terminal output
  println("Value of circle's area = "+A);
  print("Value of circumference's circle = "+cf);
  textSize(24);//text size
  text("Circumference's circle = "+cf+" m.", 30, 400);
  text("Area's circle = "+A+" m^2.", 30, 350);
  text("Diameter's circle = "+dm+" m.", 30, 250);
  text("Radius's circle = "+r+" m.", 30, 300);

}

Lab2_Book_Function

Book use Function


//FAV Book Function
//int posX=0; //fill value to move(X)
//int posY=0;//fill value to move(Y)
void setup() {
  size(500, 500);
  background(255);//white
  book(0, 0);
}
void book(int posX, int posY) {//function
  fill(#B3CFF0);//blue
  strokeWeight(8);//thick line
  rect(80+posX, 130+posY, 340, 220);//body of book
  strokeWeight(6);//thin line
  fill(#78B2F7);//dark blue
  rect(230+posX, 130+posY, 40, 220);//spine 
  rect(290+posX, 150+posY, 110, 60);//title's frame
  fill(0);//black
  textSize(30);
  text("Conan", 300+posX, 190+posY);//Title
  strokeWeight(3);//Thinest Line
  line(250+posX, 130+posY, 120+posX, 100+posY);//pages1 r
  line(250+posX, 130+posY, 110+posX, 108+posY);//pages2 r
  line(250+posX, 130+posY, 100+posX, 115+posY);//pages3 r
  line(120+posX, 100+posY, 80+posX, 130+posY);//pages border r
  line(250+posX, 130+posY, 380+posX, 100+posY);//pages1 l
  line(250+posX, 130+posY, 390+posX, 108+posY);//pages2 l
  line(250+posX, 130+posY, 400+posX, 115+posY);//pages3 l
  line(380+posX, 100+posY, 420+posX, 130+posY);//pages border l
}

Lab2_Song_Function

Song use Function 

//FAV Song function
//int posX=0;//fill value to move(X)
//int posY=0;//fill value to move(Y)
void setup() {
  size(500, 500);
  background(0);//black
  symbol(0,0);
}
void symbol(int posX,int posY){//function
  //background's circle
  stroke(255);//black line
  strokeWeight(14);//thick line
  fill(0);//line's color
  ellipse(250+posX, 250+posY, 250, 250);//big circle with nothing  inside of it

  //Linkin Park symbol
  line(260+posX, 125+posY, 185+posX, 290+posY);
  line(185+posX, 290+posY, 325+posX, 290+posY);
  line(325+posX, 290+posY, 275+posX, 180+posY);
  line(275+posX, 180+posY, 195+posX, 360+posY);

  //open the circle
  stroke(0);
  line(258+posX, 100+posY, 238+posX, 140+posY);
  line(215+posX, 350+posY, 200+posX, 380+posY);

  //title
  fill(255);
  textSize(40);
  textAlign(LEFT);
  text("LINKIN PARK", 135+posX, 450+posY);
}

Lab2_Movie_Function

Doraemon(Function)


//MY FAV MOVIE (Function)
//int posX=0;//fill value to move(Horizontal)
//int posY=0;//fill value to move(Vertical)
void setup() {
  size(500, 500);
  background(#FACFF4);
  doraemon(0,0);//function
}
void doraemon(int posX,int posY){
  stroke(#0054A5);
  strokeWeight(3);
  fill(#0005F0);//blue
  textSize(30);
  text("Doraemon : Stand by me", 75, 420);//Title
  fill(#236ED6);//blue
  ellipse(250+posX, 180+posY, 350, 295);//head
  fill(255);//white
  ellipse(250+posX, 210+posY, 250, 205);//face
  ellipse(210+posX, 120+posY, 80, 100);//right hand
  ellipse(290+posX, 120+posY, 80, 100);//left hand
  fill(#F21D1D);//red
  rect(110+posX, 300+posY, 280, 30);//cat collar
  fill(255);//white
  ellipse(120+posX, 300+posY, 60, 60);//right eye
  ellipse(380+posX, 300+posY, 60, 60);//left eye
  fill(#FF292D);//red
  ellipse(250+posX, 175+posY, 45, 40);//nose
  fill(255);//white
  arc(250+posX, 235+posY, 180, 50, 0, PI);//cute smile
}

วันศุกร์ที่ 28 สิงหาคม พ.ศ. 2558

Lab2_Digital_Clock

นาฬิกาดิจิตอล

//digital clock
int cx=250;//centerX
int cy=150;//centerY
void setup() {
  size(500, 500);
}
void draw() {
  background(#A2FAF9);//light blue
  time();
  date();
}
//time function
void time() {
  fill(0);
  textSize(50);

  //hour
  if (hour()<10) {
    text("0"+hour(), cx-150, cy-100);
  } else {
    text(hour(), cx-150, cy-100);
  }

  //minute
  if (minute()<10) {
    text(":"+"0"+minute(), cx-50, cy);
  } else {
    text(":"+minute(), cx-50, cy);
  }

  //second
  if (second()<10) {
    text(":"+"0"+second(), cx+50, cy);
  } else {
    text(":"+second(), cx+50, cy);
  }

  //Hr Min Sec's text
  textSize(15);
  text("Hr", cx-140, cy-50);
  text("Min", cx-40, cy-50);
  text("Sec", cx+60, cy-50);
}

//Date's function
void date() {
  textSize(40);
  //day
  text(day(), cx-150, cy+200);
  //month
  text(month(), cx-50, cy+200);
  //year
  text(year(), cx+50, cy+200);
//date 's text
textSize(15);
  text("Day", cx-140, cy+150);
  text("Month", cx-40, cy+150);
  text("Year", cx+60, cy+150);
}