Difference between revisions of "W3. Oct. 11th, 2006"
Jump to navigation
Jump to search
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> | ||
+ | // setting three instances for the same class | ||
wLine Line1 = new wLine(); | wLine Line1 = new wLine(); | ||
wLine Line2 = new wLine(); | wLine Line2 = new wLine(); |
Revision as of 21:48, 11 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
// setting three instances for the same 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 (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 } 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); } }