Project 3 - Logic, Steps
For Project 3, you will create a simple drawing application where 4 buttons allow you to choose which pattern to draw. One additional button will clear the drawing canvas. 3 Sliders will be used to change the hue, saturation, brightness of the pattern that's drawn.
The code on this page, and the image below shows a first iteration for this project
Overall Project Logic - Including Slider Logic
Setup: Initialize Global Variables
ButtonGroup, ClearButton, Sliders, Patterns, Background Color
In Setup, create an array btnArray of 4 Buttons that function as a ButtonGroup to control which pattern is drawn.
Initialize each Button: btnArray[ i ] by calling the Button or PImageButton constructor.
Pass the array of buttons into the ButtonGroup constructor.
In draw: add logic to displayButtons( ) , each frame.
Add logic in mouseClicked( ) to have btnGroup.clicked( mouseX, mouseY) execucted
Within mouseClicked, if the activeButton has changed, execute changePattern( ), custom method.
In the main tab: you will define a function: ChangePattern( ) that will connect the active button with a corresponding simple Pattern (PShapes) that can be drawn at the mouse position, the active pattern can be changed by selecting one of 4 pattern buttons.
Individual Button to Clear Canvas
Create an additional 5th Button that will clear the canvas, but is not part of the ButtonGroup buttons.
Pattern Objects to draw patterns
Create 2 Global Pattern Object variables, Initialize in Setup:
Pattern eraserPattern; //initialize in setup with PShape eraser, fillColor is backgroundColor
Pattern currentPattern; // will point to current active pattern - set to eraserPattern to initialize in setup
draw( ): Animation Logic
Checks if Sliders have changed value, if using a scale slider, then the patterns must also be changed to reflect the new value of len. Checks if Mouse is pressed. If so, then display current active pattern, also requires translation of origin to mouse position, patterns are drawn at origin.
CheckSliders( ) must occur before ChangePattern( )
CheckSlider( ) method, checks each slider to see if it's value has changed. Also includes logic for color dependency between sliders:
satSlider's hue is set by the hueSlider's sliderVal;
brightSlider's hue is set by the hueSlider's sliderVal;
brightSlider's sat is set by the satSlider's sliderVal;
ChangePatternColor( )
Sliders modify Color for Patterns in ChangePatternColor( )
Before displaying the currentPattern, we'll use custom sliders to set the hue, saturation, brightness of the globalColor variable that's used when displaying patterns. This logic must be executed before displaying the currentPattern. See code details below below:
Simple method that changes the values for the global pattern color using the Slider's sliderVal.
Defines hue, sat, bright values: Uses those values to set the global patternFillColor
float hue = hueSlider.sliderVal;
float sat = 100; //..initialize using satSlider
float bright= 100; //..initialize using brightSlider
patternFillColor = color( hue, sat, bright );//set from slider values
ChangePattern( ) Switch-case Control Structure
Switch-case structure allows one pattern to be set as active by using the activeBtnIndex of the ButtonGroup, to set an active pattern to be drawn on the canvas. If using a Length slider, then use a len variable that's modified by the lengthSlider, then used when creating each PShape.
DisplayPattern( )
Sets the pattern color depending if it's the eraser or not, then calls curPattern.display()
ClearCanvas( )
A function to draw a rectangle of canvas-size, and backgroundColor, hides all previous patterns, called from mouseClicked when the clearButton is clicked. After the clearButton is clicked, this method is executed, then the clearButton must be reset( ).
Main Tab Logic:
Use Functions to organize main-tab Logic
Use Classes to structure project logic.
Detailed Project Logic:
Declare Global Variables in Main Tab
Logic In Main Tab:
Logic to Initialize Objects in setup( )
Set Canvas Size: min: 800 x 600
Set colorMode - HSB
Initialize objects by calling constructors:
declare an Array of Buttons
Button[] btnArray;
initialize Array of Buttons
btnArray = new Button[4];
initialize each Array element by calling Button , PImageButton Constructors You need to figure out Button constructor parameter values so that buttons are positioned in a vertical stack.
initialize btnGroup by calling ButtonGroup Constructor and pass buttonArray as a parameter
btnGroup = new ButtonGroup( btnArray);
initialize clearButton by calling Button Constructor - use either Button or PImageButton
clearButton = new Button( ...parameters... );
initialize eraserPattern, currentPattern Objects
Create 1 PShape, pass one to Pattern Constructor
Example initialization for 2 Patterns shown below
2 pattern objects required
eraserPattern
initialized with PShapecurrentPattern
initialized by pointing to eraserPattern's object data in heap: sets to same memory address
Initialize Sliders Initialize Sliders by calling constructors with proper input parameters to determine position, size, min, max range
Logic In draw( ):
if mousePressed
checkSliders( ); //check before drawPattern, when mousePressed
changePattern( ); //if using scale slider and checkSliders returns true
translate(mouseX, mouseY);
displayPattern( );
resetMatrix();
displayButtons( ) //always draw menu of Buttons
displaySliders( ) //always draw menu of Sliders
Logic in changePattern( ):
connects buttons to determine currentPattern
sets len for currentPattern if using a length slider's sliderVal
eraserPattern color is not changed by sliders, should have fillColor, strokeColor set to global backgroundColor;
use switch-case structure
switch: check which buttonGroup button is active
Logic in displayPattern( )
Logic in drawButtonMenu( ):
Draw 5 buttons: 4 buttons in buttonGroup, 1 clearButton
draw a menu-background (rectangle)
buttonGroup.display();
clearButton.display();
Logic In MouseClicked( ):
btnGroup.clicked( parameters )
if
clearButton.click( parameters )
if clearButton is selected
clearCanvas( ) - draw rectangle over the canvas surface
reset the clearButton
Detailed Logic for Adding Sliders to Drawing Application
In this project, sliders will be used to control Hue, Saturation, Brightness of the patterns drawn.
The sliders must be checked each time the draw-loop executes if the mousePressed, to see if the sliderVal has changed.
Sliders must always be checked for changes in sliderVal before drawing any patterns.
checkSliders( )
is a custom function in the main tab, that contains logic for checking each slider.
Declare global variables:
Slider hueSlider, satSlider, brightSlider;
Initialize 3 Sliders in setup( ) by calling child class constructors: example
hueSlider = new HueSlider( 40, height-100, 200, 50, 0, 360 );
Incorporate Sliders into project using custom functions:
void checkSliders( );
void setPatternColor( );
void drawSliders ( );
in displayPattern( ) - set currentPattern fillColor using patternFillColor right before currentPattern is displayed:
currentPattern.display()
see drawPattern above for integration of this example code:
Logic in DisplaySliders( )
This code is similar to DrawButtons, we simply want to draw a rectangle behind the sliders, and then have each slider display itself by calling the over-ridden display( ) method.
Logic in CheckSliders( )
In CheckSliders, we must check to see if each slider has been pressed. We need to set the hue value for the satSlider using the current sliderVal of the hueSlider since the display of satSlider should change when the hueSlider is changed. Similarly for the brightSlider, it must have it's hue and sat reset to reflect current hueSlider and satSlider values.
Additional Logic
clearButton
The clearButton is a regular button that is used to clear the canvas. The clearButton must be declared as a global object-reference variable. The clearButton behaves like a door-bell, meaning it never displays that it is in the selected state. When the clearButton is clicked, it clears the canvas, then the reset( ) method is called to turn the button off.
Initialize in setup() The clearButton is not part of the buttonGroup buttons, but it's parameters should be defined so that the button is displayed below the other buttons that are part of the buttonGroup. `
Add code in displayButtons( ), to display the clearButton
Add code in mouseClicked( ) Add code to check if the clearButton has been clicked. Then, check to see if the clearButton is currently selected, if it is, then call custom function: clearCanvas( ), then turn-off the clearButton by calling the reset( ) method.
Logic in clearCanvas( ):
called when clearButton has been clicked and has on==true
set fill to global background color: backgroundColor
draw a rectangle, the size of the entire canvas, to clear the canvas.
rect( 0, 0, width, height)
Main Tab Final Structure
Last updated