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
  • Example Usage:
  • Pattern Class Definition
  • Pattern objects for Project 3
  • Initialize in setup
  • Match Patterns to ButtonGroup Buttons in DrawPattern

Was this helpful?

  1. Project 3
  2. Project 3 - Class Definitions

Pattern

The Pattern class is a wrapper class for geometric shapes based on the Processing PShape object. The Pattern display method has x,y postion input parameters to so that the shape can be displayed at any x,y position. The shapeColor allows programatic modification of the color. The Pattern class allows us to add logic to provide a uniform interface for displaying a variety of PSHape objects, this is necessary because Processing has inconsistent methods for setting the fill and stroke for PShape objects. We'll expand the logic of this class to handle setting fill for all types of PShape objects.

Example Usage:

To create a pattern, first create a PShape object, then pass the PShapeObject into the constructor along with a color that will be used for display

//declare as global objects
Pattern pattern1, pattern2, eraserPattern;

//initialize PShape and Pattern in setup
PShape s1 = createShape( RECT, 0,0,40,40);
pattern0 = new Pattern( s1);  //call constructor

//use similar code to initialize all patterns


//display 
pattern0.fillColor = color(100,100,100); //set fill color
pattern0.display( mouseX, mouseY);

Pattern Class Definition

class Pattern{

  //PROPERTIES
   PShape s;
   color fillColor;
   color strokeColor;

  //CONSTRUCTORS
  Pattern( PShape s ){
    this.s = s;
  }

  Pattern( PShape s, color fillColor){
    this.s = s;
    this.fillColor = fillColor;
  }

  //METHODS
  void display( ){
    s.setStroke( strokeColor);
    s.setFill( fillColor);
    shape( s, 0, 0);
    } 
}//end PatternClass

Pattern objects for Project 3

For this project, you will create 3 global pattern objects:

Pattern pattern0, pattern1, eraserPattern;

Initialize in setup

First you must create 3 PShape objects, then these PShape objects are passed into the Pattern constructor.

//initialize PShape and Pattern in setup
PShape s1 = createShape( RECT, 0,0,40,40);
pattern1 = new Pattern( s1);  //call constructor

Match Patterns to ButtonGroup Buttons in DrawPattern

The code below shows how to connect the ButtonGroup logic with the Pattern logic in the drawPattern( ) function. Notice that we use a temporary variable: Pattern curPattern;, to match-up the activeButton with this temporary variable: curPattern, so that once the switch-case logic has completed, then we display which ever pattern is referenced by the curPattern variable.

void drawPattern( ){

Pattern curPattern = pattern0;  //temporary Pattern variable initially pointing to the pattern0 object.

int activeButton = buttonGroup.activeBtnIndex;

//switch-case control structure determines which pattern corresponds to the current activeButton
switch( activeButton ){
  case 0: //pattern0 and button0
    curPattern = pattern0;

  break;

  case 1: //pattern1 and button1
    curPattern = pattern1; //first we need to set the pattern

  break;

 case 2: //eraser pattern and button
    curPattern = eraserPattern;
    curPattern.fillColor = backgroundColor;
    curPattern.strokeColor = backgroundColor;
 break;

 default:
   println("no match on switch case");

}//end of switch statement logic

  //now display the curPattern
  if( curPattern != eraserPattern){
  //set fill if this is not the eraser
  curPattern.fillColor = color( 150,100,100); //then we can set 
  }
    color - this will use slider values to determine fill
  curPattern.display( ); //display at mouse position
}
PreviousButtonGroupNextPShapes - SVG, Vertex Shapes

Last updated 5 years ago

Was this helpful?