W3. Oct. 11th, 2006

From BioV
Jump to navigation Jump to search


W3-01: simple form of Processing application

//field ---------------------------------
float x = random(320);
float y = random(320);

//system events (system methods) --------
void setup() {
  size(320, 320);
  background(0);
}

void draw() {
  background(0);
  
  //call custom methods to draw a line
  setColor();
  drawLine();
}

//custom methods ------------------------
void setColor() {
  stroke(255);
  strokeWeight(3);
}
  
void drawLine() {
  line(x, y, mouseX, mouseY);
}



W3-02: designing a class from W3-01

// declaring three instances of wLine class
wLine Line1 = new wLine();
wLine Line2 = new wLine();
wLine Line3 = new wLine();

void setup() {
  size(320, 320);
  background(0);
}

void draw() {
  background(0);
  
  //call a custom method of Line1, 2 and 3 (defined in wLine class)
  Line1.drawMe();
  Line2.drawMe();
  Line3.drawMe();
}

// design a class named wLine
class wLine {
  //field -------------------------------
  float x = random(320);
  float y = random(320);
  
  //contructor (it is similar to the setup() method of application)
  wLine() {
  }
  
  //methods -----------------------------
  void drawMe() {
    setColor();
    drawLine();
  }
  
  void setColor() {
    stroke(255);
    strokeWeight(3);
  }
  
  void drawLine() {
    line(x, y, mouseX, mouseY);
  }
}



W3-03: creating a subclass

wLine Line1 = new wLine();
rLine Line2 = new rLine();

void setup() {
  size(320, 320);
  background(0);
}

void draw() {
  background(0);
  
  Line1.drawMe();
  Line2.drawMe();
}

// rLine is a subclass of wLine
class rLine extends wLine {
  // rLine is same with wLine
  // because there is no script in rLine class yet
  //(rLine already knows everything about wLine)
}

class wLine {
  //field -------------------------------
  float x = random(320);
  float y = random(320);
  
  //contructor --------------------------
  wLine() {
  }
  
  //methods -----------------------------
  void drawMe() {
    setColor();
    drawLine();
  }
  
  void setColor() {
    stroke(255);
    strokeWeight(3);
  }
  
  void drawLine() {
    line(x, y, mouseX, mouseY);
  }
}



W3-04: customizing a mothod in subclass

wLine Line1 = new wLine();
rLine Line2 = new rLine();

void setup() {
  size(320, 320);
  background(0);
}

void draw() {
  background(0);
  
  Line1.drawMe();
  Line2.drawMe();
}

// rLine is a subclass of wLine
class rLine extends wLine {

  //customize setColor method here
  void setColor() {
    stroke(255, 0, 0); //!
    strokeWeight(1);
  }  
}

class wLine {
  //field -------------------------------
  float x = random(320);
  float y = random(320);
  
  //contructor --------------------------
  wLine() {
  }
  
  //methods -----------------------------
  void drawMe() {
    setColor();
    drawLine();
  }
  
  void setColor() {
    stroke(255);
    strokeWeight(3);
  }
  
  void drawLine() {
    line(x, y, mouseX, mouseY);
  }
}