15=object-oriented

42
Processing 1 5 Object-Oriented 物物物物

Upload: gene-kao

Post on 20-Apr-2017

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 15=Object-Oriented

Processing

15

Object-Oriented物件導向

Page 2: 15=Object-Oriented

Object-Oriented

Page 3: 15=Object-Oriented

float x, y;int rad = 10;

void setup () { size(300,300); x = 150; y = 150; frameRate(50);}

void draw() { background(255); ellipse(x,y,rad,rad); rad ++;}

Page 4: 15=Object-Oriented

Ring R1, R2, R3;

void setup () { size(300,300); noFill();

R1 = new Ring(); R2 = new Ring(); R3 = new Ring();

R1.x = 100; R1.y = 150; R1.r = 10; R2.x = 150; R2.y = 150; R2.r = 20; R3.x = 200; R3.y = 150; R3.r = 30;}

class Ring { float x, y, r; void display () { ellipse(x,y,r,r); } void move () { r++; } }

void draw() { background(255); R1.move(); R1.display(); R2.move(); R2.display(); R3.move(); R3.display();}

Page 5: 15=Object-Oriented

Ring R1, R2, R3;

void setup () { size(300,300); noFill(); R1 = new Ring(100,150,10); R2 = new Ring(150,150,20); R3 = new Ring(200,150,30);}

void draw() { background(255); R1.move(); R1.display(); R2.move(); R2.display(); R3.move(); R3.display();}

class Ring { float x, y, r; Ring(float xpos, float ypos, float radius) { x = xpos; y = ypos; r = radius; } // Constructor

void display () { ellipse(x,y,r,r); } void move () { r++; } }

Page 6: 15=Object-Oriented

Ring[] Rs;int numRs = 10;int currentR = 0;void setup () { size(300,300); noFill(); Rs = new Ring[numRs]; for (int i = 0; i < numRs; i++) { Rs[i] = new Ring(); Rs[i].start(random(300),random(300),random(1,30)); }}

void draw() { background(255); for (int i = 0; i < numRs; i++) { Rs[i].move(); Rs[i].display(); }}

class Ring { float x, y, r; void start(float xpos, float ypos, float radius) { x = xpos; y = ypos; r = radius; } void display () { ellipse(x,y,r,r); } void move () { r++; } }

Page 7: 15=Object-Oriented

Ring[] Rs;int numRs = 10;int currentR = 0;

void setup () { size(300,300); noFill(); Rs = new Ring[numRs]; for (int i = 0; i < numRs; i++) { Rs[i] = new Ring(); Rs[i].start(random(300),random(300),random(1,30)); }}

void draw() { background(255); for (int i = 0; i < numRs; i++) { Rs[i].move(); Rs[i].display(); }}

class Ring { float x, y, r; void start(float xpos, float ypos, float radius) { x = xpos; y = ypos; r = radius; } void display () { ellipse(x,y,r,r); } void move () { r++; } } Whole Program

Page 8: 15=Object-Oriented

Ring[] Rs;int numRs = 10;int currentR = 0;

void setup () { size(300,300); noFill(); Rs = new Ring[numRs]; for (int i = 0; i < numRs; i++) { Rs[i] = new Ring(0,0,0); Rs[i].start (random(300),random(300),random(1,30)); }}

void draw() { background(255); for (int i = 0; i < numRs; i++) { Rs[i].move(); Rs[i].display(); }}

class Ring { float x, y, r; Ring(float xpos, float ypos, float radius) { x = xpos; y = ypos; r = radius; } void start(float xpos, float ypos, float radius) { x = xpos; y = ypos; r = radius; } void display () { ellipse(x,y,r,r); } void move () { r++; } }

Page 9: 15=Object-Oriented

Ring[] Rs;int numRs = 10;int currentR = 0;

void setup () { size(300,300); noFill(); smooth(); Rs = new Ring[numRs]; for (int i = 0; i < numRs; i++) { Rs[i] = new Ring(); }}

void draw() { background(255); for (int i = 0; i < numRs; i++) { Rs[i].move(); Rs[i].display(); }}

void mousePressed() { Rs[currentR].start(mouseX,mouseY,1); currentR ++; if (currentR >= numRs) {currentR = 0;}}

class Ring { float x, y, r; boolean on = false; void start(float xpos, float ypos, float radius) { x = xpos; y = ypos; r = radius; on = true; } void display () { if (on == true) {ellipse(x,y,r,r);} } void move () { if (r <= 50) {r+=0.25;} else {r=1;} } } Whole Program

Page 10: 15=Object-Oriented

Ring[] Rs;int numRs = 10;int currentR = 0;

void setup () { size(300,300); noFill(); Rs = new Ring[numRs]; for (int i = 0; i < numRs; i++) { Rs[i] = new Ring(); }}

void draw() { background(255); for (int i = 0; i < numRs; i++) { Rs[i].move(); Rs[i].display(); }}void mousePressed() { Rs[currentR].start(mouseX,mouseY,1); currentR ++; if (currentR >= numRs) {currentR = 0;}}

class Ring { float x, y, r; boolean on = false; void start(float xpos, float ypos, float radius) { x = xpos; y = ypos; r = radius; on = true; } void display () { if (on == true) {ellipse(x,y,r,r);} } void move () { r++; } }

Page 11: 15=Object-Oriented

Whole Program

Ring[] Rs;int numRs = 10;int currentR = 0;

void setup () { size(300,300); noFill(); Rs = new Ring[numRs]; for (int i = 0; i < numRs; i++) { Rs[i] = new Ring(); }}

void draw() { background(255); for (int i = 0; i < numRs; i++) { Rs[i].move(); Rs[i].display(); }}void mousePressed() { Rs[currentR].start(mouseX,mouseY,1); currentR ++; if (currentR >= numRs) {currentR = 0;}}

class Ring { float x, y, r; boolean on = false; void start(float xpos, float ypos, float radius) { x = xpos; y = ypos; r = radius; on = true; } void display () { if (on == true) {ellipse(x,y,r,r);} } void move () { r++; } }

Page 12: 15=Object-Oriented

Ring[] Rs;int numRs = 10;int currentR = 0;

void setup () { size(300,300); noFill(); smooth(); Rs = new Ring[numRs]; for (int i = 0; i < numRs; i++) { Rs[i] = new Ring(); }}

void draw() { background(255); for (int i = 0; i < numRs; i++) { Rs[i].move(); Rs[i].display(); }}

void mousePressed() { Rs[currentR].start(mouseX,mouseY,1); currentR ++; if (currentR >= numRs) {currentR = 0;}}

class Ring { float x, y, r; boolean on = false; void start(float xpos, float ypos, float radius) { x = xpos; y = ypos; r = radius; on = true; } void display () { if (on == true) {ellipse(x,y,r,r);} } void move () { if (r <= 50) {r+=0.25;} else {r=1;} } }

Page 13: 15=Object-Oriented

Ring[] Rs;int numRs = 10;int currentR = 0;

void setup () { size(300,300); noFill(); smooth(); Rs = new Ring[numRs]; for (int i = 0; i < numRs; i++) { Rs[i] = new Ring(); }}

void draw() { background(255); for (int i = 0; i < numRs; i++) { Rs[i].move(); Rs[i].display(); }}

void mousePressed() { Rs[currentR].start(mouseX,mouseY,1); currentR ++; if (currentR >= numRs) {currentR = 0;}}

class Ring { float x, y, r; boolean on = false; void start(float xpos, float ypos, float radius) { x = xpos; y = ypos; r = radius; on = true; } void display () { if (on == true) {ellipse(x,y,r,r);} } void move () { if (r <= 50) {r+=0.25;} else {r=1;} } }

Whole Program

Page 14: 15=Object-Oriented

Ring[] Rs;int numRs = 10;int currentR = 0;

void setup () { size(300,300); noFill(); smooth(); Rs = new Ring[numRs]; for (int i = 0; i < numRs; i++) { Rs[i] = new Ring(); } frameRate(5);}

void draw() { background(255); for (int i = 0; i < numRs; i++) { Rs[i].move(); Rs[i].display(); }}

void mousePressed() { Rs[currentR].start(mouseX,mouseY,20,0); currentR ++; if (currentR >= numRs) {currentR = 0;}}

class Ring { float x, y, r; boolean on = false; float ang; void start(float xpos, float ypos, float radius, float a) { x = xpos; y = ypos; r = radius; ang = 0; on = true; } void display () { if (on == true) {ellipse(x,y,r,r);} } void move () { x = x + r * cos(ang); y = y + r * sin(ang); ang ++; } }

Page 15: 15=Object-Oriented

Ring[] Rs;int numRs = 10;int currentR = 0;

void setup () { size(300,300); noFill(); smooth(); Rs = new Ring[numRs]; for (int i = 0; i < numRs; i++) { Rs[i] = new Ring(); } frameRate(5);}

void draw() { background(255); for (int i = 0; i < numRs; i++) { Rs[i].move(); Rs[i].display(); }}

void mousePressed() { Rs[currentR].start(mouseX,mouseY,20,0); currentR ++; if (currentR >= numRs) {currentR = 0;}}

class Ring { float x, y, r; boolean on = false; float ang; void start(float xpos, float ypos, float radius, float a) { x = xpos; y = ypos; r = radius; ang = 0; on = true; } void display () { if (on == true) {ellipse(x,y,r,r);} } void move () { x = x + r * cos(ang); y = y + r * sin(ang); ang ++; } }

Whole Program

Page 16: 15=Object-Oriented

Ring[] Rs;int numRs = 10;int currentR = 0;

void setup () { size(300,300); noFill(); Rs = new Ring[numRs]; for (int i = 0; i < numRs; i++) { Rs[i] = new Ring(0,0,0); Rs[i].start (random(300),random(300),random(1,30)); }}

void draw() { background(255); for (int i = 0; i < numRs; i++) { Rs[i].move(); Rs[i].display(); }}

class Ring { float x, y, r; Ring(float xpos, float ypos, float radius) { x = xpos; y = ypos; r = radius; } void start(float xpos, float ypos, float radius) { x = xpos; y = ypos; r = radius; } void display () { ellipse(x,y,r,r); } void move () { r = mouseX-mouseY; } }

Page 17: 15=Object-Oriented

float x = 33;float y = 50;float diameter = 30;

void setup() { size(100, 100); smooth(); noStroke();}void draw() { background(0); ellipse(x, y, diameter, diameter);}

Spot sp; // Declare the object

void setup() { size(100, 100); smooth(); noStroke(); sp = new Spot(); // Construct the object sp.x = 33; // Assign 33 to the x field sp.y = 50; // Assign 50 to the y field sp.diameter = 30; // Assign 30 to the diameter field}

void draw() { background(0); ellipse(sp.x, sp.y, sp.diameter, sp.diameter);}

class Spot { float x, y; // The x- and y-coordinate float diameter; // Diameter of the circle}

Structure 4: Objects ITextbook P.395.

Page 18: 15=Object-Oriented

Spot sp; // Declare the object

void setup() { size(100, 100); smooth(); noStroke(); sp = new Spot(); // Construct the object sp.x = 33; // Assign 33 to the x field sp.y = 50; // Assign 50 to the y field sp.diameter = 30; // Assign 30 to the diameter field}

void draw() { background(0); ellipse(sp.x, sp.y, sp.diameter, sp.diameter);}

class Spot { float x, y; // The x- and y-coordinate float diameter; // Diameter of the circle}

Spot sp; // Declare the object

void setup() { size(100, 100); smooth(); noStroke(); sp = new Spot(); // Construct the object sp.x = 33; // Assign 33 to the x field sp.y = 50; // Assign 50 to the y field sp.diameter = 30; // Assign 30 to the diameter field}

void draw() { background(0); sp.display();}

class Spot { float x, y, diameter; void display() { ellipse(x, y, diameter, diameter); }}

Program from last page.

Page 19: 15=Object-Oriented
Page 20: 15=Object-Oriented

Spot sp; // Declare the object

void setup() { size(100, 100); smooth(); noStroke(); // Construct the object sp = new Spot(33, 50, 30, 1.5);}

void draw() { fill(0, 15); rect(0, 0, width, height); fill(255); sp.move(); sp.display();}

1/2

Page 21: 15=Object-Oriented

// Insert Spot classclass Spot { float x, y; // X-coordinate, y-coordinate float diameter; // Diameter of the circle float speed; // Distance moved each frame int direction = 1; // Direction of motion (1 is down, -1 is up) // Constructor

Spot(float xpos, float ypos, float dia, float sp) { x = xpos; y = ypos; diameter = dia; speed = sp; }

void move() { y += (speed * direction); if ((y > (height - diameter/2)) || (y < diameter/2)) { direction *= -1; } } void display() { ellipse(x, y, diameter, diameter); }} 2/2

Page 22: 15=Object-Oriented

Spot sp; // Declare the object

void setup() { size(100, 100); smooth(); noStroke(); // Construct the object sp = new Spot(33, 50, 30, 1.5);}

void draw() { fill(0, 15); rect(0, 0, width, height); fill(255); sp.move(); sp.display();}

// Insert Spot classclass Spot { float x, y; // X-coordinate, y-coordinate float diameter; // Diameter of the circle float speed; // Distance moved each frame int direction = 1; // Direction of motion (1 is down, -1 is up) // Constructor Spot(float xpos, float ypos, float dia, float sp) { x = xpos; y = ypos; diameter = dia; speed = sp; }

void move() { y += (speed * direction); if ((y > (height - diameter/2)) || (y < diameter/2)) { direction *= -1; } } void display() { ellipse(x, y, diameter, diameter); }}

Whole Program

Page 23: 15=Object-Oriented
Page 24: 15=Object-Oriented

Spot sp1, sp2, sp3; // Declare the objects

void setup() { size(100, 100); smooth(); noStroke(); sp1 = new Spot(20, 50, 40, 0.5); // Construct sp1 sp2 = new Spot(50, 50, 10, 2.0); // Construct sp2 sp3 = new Spot(80, 50, 30, 1.5); // Construct sp3}

void draw() { fill(0, 15); rect(0, 0, width, height); fill(255); sp1.move(); sp2.move(); sp3.move(); sp1.display(); sp2.display(); sp3.display();}

Page 25: 15=Object-Oriented

Spot sp1, sp2, sp3; // Declare the objects

void setup() { size(100, 100); smooth(); noStroke(); sp1 = new Spot(20, 50, 40, 0.5); // Construct sp1 sp2 = new Spot(50, 50, 10, 2.0); // Construct sp2 sp3 = new Spot(80, 50, 30, 1.5); // Construct sp3}

void draw() { fill(0, 15); rect(0, 0, width, height); fill(255); sp1.move(); sp2.move(); sp3.move(); sp1.display(); sp2.display(); sp3.display();}

// Insert Spot classclass Spot { float x, y; // X-coordinate, y-coordinate float diameter; // Diameter of the circle float speed; // Distance moved each frame int direction = 1; // Direction of motion (1 is down, -1 is up) // Constructor Spot(float xpos, float ypos, float dia, float sp) { x = xpos; y = ypos; diameter = dia; speed = sp; }

void move() { y += (speed * direction); if ((y > (height - diameter/2)) || (y < diameter/2)) { direction *= -1; } } void display() { ellipse(x, y, diameter, diameter); }} Whole Program

Page 26: 15=Object-Oriented

Addsp4sp5

color

Page 27: 15=Object-Oriented
Page 28: 15=Object-Oriented

int numSpots = 6;// Declare and create the arraySpot[] spots = new Spot[numSpots];

void setup() { size(100, 100); smooth(); noStroke(); for (int i = 0; i < spots.length; i++) { float x = 10 + i*16; float rate = 0.5 + i*0.05; // Create each object spots[i] = new Spot(x, 50, 16, rate); }}

void draw() { fill(0, 12); rect(0, 0, width, height); fill(255); for (int i = 0; i < spots.length; i++) { spots[i].move(); // Move each object spots[i].display(); // Display each object }}

Page 29: 15=Object-Oriented

int numSpots = 6;// Declare and create the arraySpot[] spots = new Spot[numSpots];

void setup() { size(100, 100); smooth(); noStroke(); for (int i = 0; i < spots.length; i++) { float x = 10 + i*16; float rate = 0.5 + i*0.05; // Create each object spots[i] = new Spot(x, 50, 16, rate); }}

void draw() { fill(0, 12); rect(0, 0, width, height); fill(255); for (int i = 0; i < spots.length; i++) { spots[i].move(); // Move each object spots[i].display(); // Display each object }}

// Insert Spot classclass Spot { float x, y; // X-coordinate, y-coordinate float diameter; // Diameter of the circle float speed; // Distance moved each frame int direction = 1; // Direction of motion (1 is down, -1 is up) // Constructor Spot(float xpos, float ypos, float dia, float sp) { x = xpos; y = ypos; diameter = dia; speed = sp; }

void move() { y += (speed * direction); if ((y > (height - diameter/2)) || (y < diameter/2)) { direction *= -1; } } void display() { ellipse(x, y, diameter, diameter); }} Whole Program

Page 30: 15=Object-Oriented
Page 31: 15=Object-Oriented

eye e;

void setup () { size(1000,600); ellipseMode(CENTER); smooth(); e = new eye(); e.x = 500; e.y = 300; e.d = 100;}

void draw () { background(255); e.display();}

class eye { float x,y,d; // d for diameter void display() { translate(x,y); fill(0); ellipse(0,0,d,d); fill(255); ellipse((mouseX-x)/(d/5),(mouseY-y)/(d/10),d/2,d/2); fill(0); ellipse((mouseX-x)/(d/5),(mouseY-y)/(d/10)+(d/10),d/10,d/5); }}

Page 32: 15=Object-Oriented

eye e;

void setup () { size(1000,600); ellipseMode(CENTER); smooth(); e = new eye(); e.x = 500; e.y = 300; e.d = 100;}

void draw () { background(255); e.display();}

class eye { float x,y,d; // d for diameter void display() { translate(x,y); fill(0); ellipse(0,0,d,d); fill(255); ellipse((mouseX-x)/(d/5),(mouseY-y)/(d/10),d/2,d/2); fill(0); ellipse((mouseX-x)/(d/5),(mouseY-y)/(d/10)+(d/10),d/10,d/5); }}

Whole Program

Page 33: 15=Object-Oriented
Page 34: 15=Object-Oriented

int n = 10;eye[] e = new eye[n];

void setup () { size(1000,600); ellipseMode(CENTER); smooth(); for (int i = 0; i< e.length; i++) { e[i] = new eye(50+100*i,50,100,10); }}

void draw () { background(255); for (int i = 0; i < e.length; i++) { e[i].display(); }} 1/2

Page 35: 15=Object-Oriented

class eye { float x,y,d,sp; // d for diameter eye(float xpos,float ypos,float diameter,float speed) { x = xpos; y = ypos; d = diameter; sp = speed; } void display() { pushMatrix(); translate(x,y); fill(0); ellipse(0,0,d,d); fill(255); ellipse((mouseX-x)/(d/5),(mouseY-y)/(d/10),d/2,d/2); fill(0); ellipse((mouseX-x)/(d/5),(mouseY-y)/(d/10)+(d/10),d/10,d/5); popMatrix(); }}

2/2

Page 36: 15=Object-Oriented

int n = 10;eye[] e = new eye[n];

void setup () { size(1000,600); ellipseMode(CENTER); smooth(); for (int i = 0; i< e.length; i++) { e[i] = new eye(50+100*i,50,100,10); }}

void draw () { background(255); for (int i = 0; i < e.length; i++) { e[i].display(); }}

class eye { float x,y,d,sp; // d for diameter eye(float xpos,float ypos,float diameter,float speed) { x = xpos; y = ypos; d = diameter; sp = speed; } void display() { pushMatrix(); translate(x,y); fill(0); ellipse(0,0,d,d); fill(255); ellipse((mouseX-x)/(d/5),(mouseY-y)/(d/10),d/2,d/2); fill(0); ellipse((mouseX-x)/(d/5),(mouseY-y)/(d/10)+(d/10),d/10,d/5); popMatrix(); }} Whole Program

Page 37: 15=Object-Oriented
Page 38: 15=Object-Oriented
Page 39: 15=Object-Oriented

int n = 10;eye[] e = new eye[n];

void setup () { size(1000,600); ellipseMode(CENTER); smooth(); for (int i = 0; i< e.length; i++) { e[i] = new eye(50+100*i,50,100,0.2+0.2*i); }}

void draw () { background(255); fill(0); for (int i = 0; i < e.length; i++) { e[i].moving(); e[i].display(); }}

1/3

Page 40: 15=Object-Oriented

class eye { float x,y,d,sp; // d for diameter int direction = 1; eye(float xpos,float ypos,float dia,float speed) { x = xpos; y = ypos; d = dia; sp = speed; } void moving() { y += (sp * direction); if ((y > (height - d / 2.0)) || (y < (d / 2.0))) { direction *= -1; } }

2/3

Page 41: 15=Object-Oriented

void display() { pushMatrix(); translate(x,y); fill(0); ellipse(0,0,d,d); fill(255); ellipse((mouseX-x)/(d/3),(mouseY-y)/(d/3),d/2,d/2); fill(0); ellipse((mouseX-x)/(d/3),(mouseY-y)/(d/3)+(d/10),d/10,d/5); popMatrix(); }}

3/3

Page 42: 15=Object-Oriented

int n = 10;eye[] e = new eye[n];

void setup () { size(1000,600); ellipseMode(CENTER); smooth(); for (int i = 0; i< e.length; i++) { e[i] = new eye(50+100*i,50,100,0.2+0.2*i); }}

void draw () { background(255); fill(0); for (int i = 0; i < e.length; i++) { e[i].moving(); e[i].display(); }}

class eye { float x,y,d,sp; // d for diameter int direction = 1; eye(float xpos,float ypos,float dia,float speed) { x = xpos; y = ypos; d = dia; sp = speed; } void moving() { y += (sp * direction); if ((y > (height - d / 2.0)) || (y < (d / 2.0))) { direction *= -1; } } void display() { pushMatrix(); translate(x,y); fill(0); ellipse(0,0,d,d); fill(255); ellipse((mouseX-x)/(d/3),(mouseY-y)/(d/3),d/2,d/2); fill(0); ellipse((mouseX-x)/(d/3),(mouseY-y)/(d/3)+(d/10),d/10,d/5); popMatrix(); }}

Whole Program