0% found this document useful (0 votes)
3K views

15 Object Oriented

The document describes an object-oriented program in Processing that draws rings. It defines a Ring class with x, y, and r properties to represent the position and radius of each ring. An array of Ring objects is created and their move() and display() methods are called each frame to increment the radius and draw the rings. When the mouse is pressed, a new ring is added at the mouse position.

Uploaded by

Gene Kao
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3K views

15 Object Oriented

The document describes an object-oriented program in Processing that draws rings. It defines a Ring class with x, y, and r properties to represent the position and radius of each ring. An array of Ring objects is created and their move() and display() methods are called each frame to increment the radius and draw the rings. When the mouse is pressed, a new ring is added at the mouse position.

Uploaded by

Gene Kao
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 42

Processing

15
Object-Oriented

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

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

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++;
}
}
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++;
}
}
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
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++;
}
}
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
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++;
}
}
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++;
}
}
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;}
}
}
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
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 ++;
}
}
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
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;
}
}
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 I
Textbook P.395.
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.
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
// Insert Spot class
class 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
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 class
class 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
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();
}
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 class
class 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
Add
sp4
sp5
color
int numSpots = 6;
// Declare and create the array
Spot[] 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
}
}
int numSpots = 6;
// Declare and create the array
Spot[] 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 class
class 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
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);
}
}
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
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
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
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
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
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
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
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

You might also like