Spot Constructor
// Lesson 11: Object Oriented Coding.
 
/*
  Using a constructor method to create a new instance of Spot,
  instead of having to set individual instance variables, with 'sp.x = 33;'
*/
 
Spot sp;
 
void setup() {
  size(100, 100);
  smooth();
  noStroke();
 
  sp = new Spot(33, 50, 30); // creating new instance of Spot with specific parameters.
}
 
void draw() {
  background(0);
  sp.display();
}
 
// ---------------------------
// Definition of the class Spot
 
class Spot {
  float x;
  float y;
  float diameter;
 
  // Constructor method of the class Spot, with parameters for x, y and diameter
  Spot(float newX, float newY, float newDiameter) {
    x = newX;
    y = newY;
    diameter = newDiameter;
  }
 
  void display() {
    ellipse(x, y, diameter, diameter);
  }
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License