i think i really got it this time, nature of code in

This commit is contained in:
shiffman
2012-09-04 01:47:20 +00:00
parent b5b67a12b5
commit 1d86e1f5bf
541 changed files with 35009 additions and 0 deletions
@@ -0,0 +1,36 @@
// Learning Processing
// Daniel Shiffman
// http://www.learningprocessing.com
// Example 22-1: Inheritance
class Circle extends Shape {
// Inherits all instance variables from parent + adding one
color c;
Circle(float x_, float y_, float r_, color c_) {
super(x_,y_,r_); // Call the parent constructor
c = c_; // Also deal with this new instance variable
}
// Call the parent jiggle, but do some more stuff too
void jiggle() {
super.jiggle();
// The Circle jiggles its size as well as its x,y location.
r += random(-1,1);
r = constrain(r,0,100);
}
// The changeColor() function is unique to the Circle class.
void changeColor() {
c = color(random(255));
}
void display() {
ellipseMode(CENTER);
fill(c);
stroke(0);
ellipse(x,y,r,r);
}
}
@@ -0,0 +1,28 @@
// Learning Processing
// Daniel Shiffman
// http://www.learningprocessing.com
// Example 22-1: Inheritance
class Shape {
float x;
float y;
float r;
Shape(float x_, float y_, float r_) {
x = x_;
y = y_;
r = r_;
}
void jiggle() {
x += random(-1,1);
y += random(-1,1);
}
// A generic shape does not really know how to be displayed.
// This will be overridden in the child classes.
void display() {
point(x,y);
}
}
@@ -0,0 +1,25 @@
// Learning Processing
// Daniel Shiffman
// http://www.learningprocessing.com
// Example 22-1: Inheritance
class Square extends Shape {
// Variables are inherited from the parent.
// We could also add variables unique to the Square class if we so desire
Square(float x_, float y_, float r_) {
// If the parent constructor takes arguments then super() needs to pass in those arguments.
super(x_,y_,r_);
}
// Inherits jiggle() from parent
// The square overrides its parent for display.
void display() {
rectMode(CENTER);
fill(175);
stroke(0);
rect(x,y,r,r);
}
}
@@ -0,0 +1,27 @@
// Learning Processing
// Daniel Shiffman
// http://www.learningprocessing.com
// Example 22-1: Inheritance
// Object oriented programming allows us to defi ne classes in terms of other classes.
// A class can be a subclass (aka " child " ) of a super class (aka "parent").
// This is a simple example demonstrating this concept, known as "inheritance."
Square s;
Circle c;
void setup() {
size(200,200);
smooth();
// A square and circle
s = new Square(75,75,10);
c = new Circle(125,125,20,color(175));
}
void draw() {
background(255);
c.jiggle();
s.jiggle();
c.display();
s.display();
}