void setup(){
size(200, 200); //หน้าจอแสดงผลขนาด 200x200
background(0); //พื้นหลังสีดำ
}
Button Name =new Button("Annt"); //ประกาศ object Name =new Button( ) ขึ้นมาเป็นButton (ซึ่งเป็นclassที่สร้างไว้) เป็นตัวแปรแบบ Local Variable
Button Hello = new Button ();
void draw(){
Name.display(); //เป็นการเรียกใช้ method ภายในฟังชันโดยกำหนด object ที่ต้างการใช้ไว้ข้างหน้า method
Hello.display();
}
void mousePressed(){
Name.click(); //เป็นการเรียกใช้ method ภายในฟังชันโดยกำหนด object ที่ต้างการใช้ไว้ข้างหน้า method
Hello.click();
}
class Button {
String name;
int x; //ประกาศ Attribute x ขึ้นมาเป็นจำนวนเต็ม ซึ่งเป็นตัวแปรแบบ Local Variable
int y; //ประกาศ Attribute y ขึ้นมาเป็นจำนวนเต็ม ซึ่งเป็นตัวแปรแบบ Local Variable
int z; //ประกาศ Attribute z ขึ้นมาเป็นจำนวนเต็ม ซึ่งเป็นตัวแปรแบบ Local Variable
Button (String n ){
this.name=n;
this.x=50;
this.y=50;
this.z=50;
}
Button (){
this.name="Hello";
this.x=120;
this.y=50;
this.z=40;
}
void display() { //method ที่ใช้ในการแสดงภาพขึ้นCanvas
strokeWeight(3);
if (this.x==50){
fill(255, 20, 147);
}
else{
fill(0, 191, 255);
}
rect(this.x, this.y, this.z, this.z);
}
void click() {
if (mouseX>=this.x&&mouseX<=this.x+this.z&&mouseY>=this.y&&mouseY<=this.y+this.z){
println(this.name);
}
}
}
