CS1335 Java and Processing
  • CS 1335 Computer Science 1
  • Getting Started
    • Processing IDE
    • Java vs Javascript
    • Review: Processing, Functions
    • HSB Color Mode
      • HSB Color Wheel
        • Example Code
      • HSB Color Palette Tool
    • Recursion
      • Recursion Call-Stack
      • Example Code
        • Example Code Feb 5 S20
        • Feb 12 Code
  • Project 1
    • Subjective Modeling of Emotions
    • Emotions represented using color, form, space
      • Kandinsky Color - Emotion
      • Emotional Intelligence
    • Project 1: PShapes
      • Example Code
      • Inspiration
    • PShape with Cutout - Inner Contour
    • VertexShape - Recursion
    • Project 1: Recursive Drawing
    • Project 1: Programmatic Variations in Color
      • Recursion with rotate, scale
      • Plan Region Size, Color
    • Map Function
    • Transforms for Mirroring
    • Project1-Steps
  • Grid Based Designs
    • Computational Design
      • Generative Design
    • Artist: Victor Vasarely
    • Grid Pattern Design
    • 1D - Array of PShapes for Grid Layout
      • Truchet Tiling
      • Example Code
    • PShapes in Grid Regions
    • Grid Region Logic
    • Pattern Preview - Transforms: Translate & Scale
  • Project 2
    • Project 2 - 2D Arrays for Gradient Logic
      • 2D Array Grid with Labels
    • Grid Patterns using 2D Array Indexes: i, j
      • Example Class Code
    • lerpColor( ) and map( ) Functions
    • Demo Lerp Colors
    • 2D Arrays with lerpColor
    • Create PShape 2D Array
    • Function: Populate2DArray( )
    • Function: DisplayShapeMatrix()
    • Transforms for Position, Rotation, Scale of ShapeMatrix Elements
    • Project 2 - Steps
    • Animation for ShapeMatrix
      • Animation w/Noise
  • Object Oriented Programming
    • Introduction to Objects
    • OOP vs Data-Flow
    • Button States
    • Buttons as Objects
      • Button Class
    • Create Object Instances
    • Button Types
    • Modeling Buttons: States and Events
    • OOP - Inheritance
    • OOP - Polymorphism
    • Child-Class: PImageButton
    • PShape - SVG Objects
    • Menu of Buttons
    • ButtonGroup - Final Version
    • Slider Controller
    • UML Class Diagram
  • Project 3
    • Project 3 - Logic, Steps
    • Example Code S20
      • Code Wed Apr 1
      • Code Wed Apr 8 v1
      • Code Wed Apr 8 v2
      • Code Mon Apr 13
      • Code Wed Apr 15
      • Code Mon Apr 20
      • Code Wed Apr 22
      • Code Mon Apr 27
      • Code Wed Apr 29
    • Project 3 - Class Definitions
      • Button
      • PImageButton
      • ButtonGroup
      • Pattern
        • PShapes - SVG, Vertex Shapes
        • Setting Colors For Patterns
        • Pattern - With Child-PShapes
      • Slider
      • Particles
  • Java Syntax
    • Java Syntax
      • Typed-Variables
      • Float - Integer Conversion Errors
      • Modulus
      • Functions
      • Object Reference Data Types
      • Arrays
        • Class Example Code
      • Switch-Case Statement
      • Ternary Operator
      • Class
      • Learning Science
    • UML Class Diagram
    • Glossary
  • Resources and References
    • Resources
    • Random Inspiration
      • Ulm School
      • Heart-Mind, Mind, Body
      • Statistical Uncertainty
Powered by GitBook
On this page

Was this helpful?

  1. Object Oriented Programming

Introduction to Objects

PreviousAnimation w/NoiseNextOOP vs Data-Flow

Last updated 5 years ago

Was this helpful?

To begin our look at Object oriented programming, we'll look at the simple task of animating a ball moving across the Processing canvas.

First, we'll look at the code in the Ball Class Definition, then we'll look at the main tab code to see how to create an instance of a Ball Class object.

Below is a UML (Unified Modeling Language) Class diagram which gives an overview of the important information about the Ball class. In the top section is the class name, the second section shows the instance variables or properties of the class. The bottom section shows the class methods, which are functions that belong to the class.

//Definition for Ball Class

class Ball{
  //instance variables or properties
  float x; 
  float y;
  float size;

   //additional variables (these ones are not shown in UML Class Diagram)
  float speedX;
  float speedY;
  color ballColor;

  //////Class Constructors - to initialize instance variables
  /////Overloaded versions - each with a unique parameter list
  Ball(){  //default constructor takes no input parameters
    this( 5, 5, 50); //calls the Ball constructor Ball(5,5,50)
  }

  Ball(float _x, float y, float size ){
    x = _x;  //initialize instance variable: x
    this.y  = y;  //initialize instance variable: y
    this.size = size;
    speedX = 5;  //the same for every object
    speedY = 10;  //the same for every object
    ballColor = color( 100); //default ball color is gray
  }

  Ball(float _x, float y, float size, float speedX, float speedY, color ballColor ){
    x = _x;  //initialize instance variable: x
    this.y  = y;
    this.size = size;
    this.speedX = speedX;  //the same for every object
    this.speedY = speedY;  //the same for every object
    this.ballColor = ballColor;
  }

  ////////////Methods or functions

  //displays the ball
  void display(){
    fill( ballColor);
    ellipse( x, y, size, size); //diameter
  } //end display method

  //determines new position for ball each time this is executed
  void move(){
    if( x > width || x < 0){
      speedX *= -1;
    }
     if( y > height || y < 0){
      speedY *= -1;
    }
    x += speedX;
    y += speedY;
  } // end move method

} //end of class Ball

////Main Tab Code:

Ball ball1;   //ball1 = null
//declare a variable that can point to a ball object's data in heap memory

Ball[] balls; //declare the array of Ball objects

void setup() {
  size( 600, 600);
  colorMode(HSB, 360,100,100);
  ///create 1 Ball object instance
  ball1 = new Ball(20, 20, 30, 15, 5, color(255, 0, 0)); //new is the keyword used to create an object instance

balls = new Ball[100]; //initialize the array

  for ( int i=0; i< balls.length; i++) {
    float x = random(0, width);
    float y = random(0, height);
    float hue = random(0, 360);
    balls[i] = new Ball(x, y, 20, 5, 10, color( hue, 100, 100)); //called the constructor - we have an object instance
  } //end of for
}//end of setup


void draw() {
  background(255);

  //have each ball execute it's move method
  ball1.move();

  //have each ball display itself
  ball1.display();

  //use for-loop to move and display all balls
  for ( int i=0; i< balls.length; i++) {
    balls[i].move();
    balls[i].display();
  } //end for


} //end draw

Link to Zip file of Example Code Below
UML Class Diagram Specification