Difference between revisions of "W3. Oct. 11th, 2006"

From BioV
Jump to navigation Jump to search
 
m
 
(2 intermediate revisions by 2 users not shown)
Line 36: Line 36:
 
<b>W3-02</b>: designing a class from W3-01
 
<b>W3-02</b>: designing a class from W3-01
 
<pre>
 
<pre>
 +
// declaring three instances of wLine class
 
wLine Line1 = new wLine();
 
wLine Line1 = new wLine();
 
wLine Line2 = new wLine();
 
wLine Line2 = new wLine();
Line 48: Line 49:
 
   background(0);
 
   background(0);
 
    
 
    
   //call a custom method of Line1 (defined in wLine class)
+
   //call a custom method of Line1, 2 and 3 (defined in wLine class)
 
   Line1.drawMe();
 
   Line1.drawMe();
 
   Line2.drawMe();
 
   Line2.drawMe();
Line 104: Line 105:
 
   // rLine is same with wLine
 
   // rLine is same with wLine
 
   // because there is no script in rLine class yet
 
   // because there is no script in rLine class yet
 +
  //(rLine already knows everything about wLine)
 
}
 
}
  

Latest revision as of 17:04, 12 October 2006


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);
  }
}