วันเสาร์ที่ 21 กันยายน พ.ศ. 2556

Solar




int i=0;
int[] moon= {
  0, 0, 1, 2, 39, 30, 21, 8
};
int[] radius= {
  10, 15, 15, 12, 50, 40, 30, 27
};
int[] distant= {
  8, 50, 90, 130, 170, 230, 280, 330
};
color[] c= {
  #903CD6, #3F2CF2, #07B711, #DEA241, #FF9D24, #D2E3A3, #8CD2E3, #83AABC
};
String[] planet = {
  "Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Ouranos", "Neptune"
};

void setup() {
  background(0);
  size(500, 300);
}
void draw() {
  fill(random(209), random(54), random(12));
  ellipse(0, 150, 140, 140);
  ellipse(distant[5]+90, 150, 55, 7);
  drawstar();
  //drawmoon();
}
int a;
void drawstar() {

  while (i<distant.length) {
    fill(c[i]);
    text(planet[i], distant[i]+75, 200);
    ellipse(distant[i]+90, 150, radius[i], radius[i]);
    fill(255, 255, 0);

    if (a<moon[i]) {
      ellipse(distant[i]+90+a, 100, 3, 3);
      a=a+1;
    }else
     {a=0;}
    i=i+1;
  }
}

Fraction

class Fraction {
  int n;
  int d;
  Fraction(int a, int b) {
    n=a;
    d=b;
  }
  void add(int z) {
    this.n=this.d*z+this.n;
  }
  void minus(int z) {
    this.n=this.n-z*this.d;
  }
  void mul(int s) {
    this.n=this.n*s;
  }
  String toString() {
    String s =this.n+"/"+this.d;
    return s;
  }
  void reciprocal() {
    int temp;
    temp=n;
    n=d;
    d=temp;
  }
}
void setup() {
  background(0);
  Fraction f =new Fraction(9, 5);
  Fraction g =new Fraction(9, 8);
  g.add(5);
  f.minus(8);
  f.mul(3);
  textSize(10);
  text(f.toString(),10,40);
  text(g.toString(),10,70);
    text(g.toString(),10,70);
}

สามารถรันโค้ดได้ที่ : http://processingjs.org/tools/processing-helper.html


Array:Easy

Question : Complete the methods that draw a bar chart from an array of integer data
                สร้างฟังก์ชันวาดแผนภูมิจาก array ของข้อมูลจำนวนเต็ม

Answer :
void setup() {
  size (450, 450);
  background(25);
}
void draw () {
  int x = 50;
  int i = 0;
  int y = 50;
  int [] B = {13,20, 14,10, 22,28, 32 } ;
                 
  while (i < B.length) {
    textSize(10);
    fill(#1B9AF7);
    rect (x, 380, 20, -B[i]*10);
    fill (#FC0AA8);
    text(B[i]+"", x,400);
    i = i + 1;
    x = x + 50;
  }
  textSize(19);
  fill (#FC0AA8);
  text("integer data ", 50, 50);
}
source : Java for students
Douglas Bell and Mike Parr. -- 2nd ed. page 253
ISBN : 0-13010922-3


Function : Easy

   Question : Code a method which draws a circle,giveen the coordinates of the centre and the radius.It header should be
    สร้างฟังก์ชันที่วาดวงกลมโดยมีการกำหนด centre  และ radius

         private void circle(Graphics g,int xCentre, int yCentre, int radius)

   Answer :

          void setup() {
             size(200, 200);
             background(0);
          }
          void draw() {
             fill(#F5076F);
             circle(100, 100, 100);
          }
          void circle(int xCentre, int yCentre, int radius) {
             ellipse(xCentre, yCentre, radius, radius);
          }

source : Java for students
Douglas Bell and Mike Parr. -- 2nd ed. page 253
ISBN : 0-13010922-3



Chessboard

void setup() {
  background(0);
  size(400, 400);//
}
  int [][]a= { {  0, 100,200, 300 }, {  50, 50,50,50},{150,150,150, 150},{250,250,250,250},{350,350,350,350 } };//มีการประกาศและกำหนดเป็น array
  int i=0;//
  void draw(){
    drawOthello();//
  }

void drawOthello() {//

  while (i<a[0].length) {
    rect(a[0][i],a[2][i] , 50, 50);
    rect(a[0][i],a[1][i] , 50, 50);
    rect(a[0][i],a[3][i] , 50, 50);
    rect(a[0][i],a[4][i] , 50, 50);
    rect(a[0][i]+50,a[2][i]-50 , 50, 50);
    rect(a[0][i]+50,a[1][i]-50 , 50, 50);
    rect(a[0][i]+50,a[3][i]-50 , 50, 50);
    rect(a[0][i]+50,a[4][i]-50 , 50, 50);
    i=i+1;
  }

}




Button






...
class button {
  int x;
  int y;
  String name;
  int i;
  button(String n, int x, int y) {
    this.name=n;
    this.x=x;
    this.y=y;
  }
  void display() {
    fill(#E535A5);
    rect(x, y, 160, 50);
    textSize(20);
    fill(0);
    if ((mousePressed&&mouseX>=x&&mousePressed&&mouseX<=x+80)&&(mousePressed&&mouseY>=y&&mousePressed&&mouseY<=y+50)) {
      text("Panadda ", 100, 110);
    }else {
      text("Click ", 120, 110);
    }
  }
  void click() {
    if ((mousePressed&&mouseX>=x&&mousePressed&&mouseX<=x+80)&&(mousePressed&&mouseY>=y&&mousePressed&&mouseY<=y+50)) {
      if (i%8==0) {
        println(this.name);
      }
      i=i+1;
    }
  }
}
void setup() {
  size(300, 300);
}
void draw() {
  background(0);
  button a =new button("Panadda", 65, 80);
  a.display();
  a.click();
}

For loop

This program prints a four-line triangle of asterisks(*)
เครื่องหมายดอกจันทร์เรียงกันเป็ยสามเหลี่ยมจำนวนสี่แถว

for(int i=1;i<=4;i++){
  for(int j=1;j<=i;j++)
  print("*");
  println();
}