# A Simple 2D Processing Sketch for Beginners

Here’s a simple Processing sketch that renders some 2D graphics. Maybe it’ll inspire or help you if you’re looking to get into 2D graphics using Processing.

As part of my Computer Graphics course, I’m required to write multiple 2D and 3D programs throughout the semester. This was my very first assignment!

# Processing Source Code

Here’s the source by itself:

```// Polygon
PShape polygon1;  // Initialize the polygon's PShape object

// Initialize three explicitly-defined points, used later
// when drawing primitives.
int[] pointOne = new int;
int[] pointTwo = new int;
int[] pointThree = new int;

// directionFlagOne is used for direction switching
// regarding the green square, whose coordinates are based off
// of pointOne.
int directionFlagOne = 1;

// directionFlagOne is used for direction switching
// regarding the yellow rectangle, whose coordinates are based off
// of pointTwo and pointThree.
int directionFlagTwo = 1;

// The int textColor is used in the "hidden text" section of this code
// Initializing the int here.
int textColor = 0;

void setup() {
// Set the framerate to 30
frameRate(30);
// Create a 500x500 window, sufficient for all requirements
size(500, 500, P2D); // Uses the Processing 2D Renderer

// Set up polygon1 - fill color and vertices
polygon1 = createShape();
polygon1.beginShape();
polygon1.fill(255, 255, 255); // polygon1 is white.
polygon1.noStroke();
polygon1.vertex(0, 0);
polygon1.vertex(0, 100);
polygon1.vertex(50, 50);
polygon1.vertex(100, 100);
polygon1.vertex(100, 0);
polygon1.endShape();
// Done setting up polygon1

// Define initial values for the three explicitly-defined points
// defined above setup() function.
pointOne = 25;
pointOne = 275;
pointTwo = 25;
pointTwo = 325;
pointThree = 475;
pointThree = 425;
} // End setup call

void draw() {
background(0); // Background color is black.

// Green Line Segment
stroke(0, 255, 0);
line(10, 10, 10, 250);

// Turn stroke off for the rest of the shapes
noStroke();

// A small, green square that rotates and slides
// back and forth along the bottom half of the screen. Uses one
// of the three explicitly-defined points initialized above.
// Logic (if/else if statements) for direction switching
if (pointOne >= 475){
directionFlagOne = -1; // Reverse direction if boundary hit
}
else if (pointOne <= 25){
directionFlagOne = 1;
}
pointOne += 5 * directionFlagOne;
rectMode(CENTER);
fill(0, 255, 0);
rect(pointOne, pointOne, 25, 25);
// End code for small, green square

// The corners of the following yellow rectangle are defined by the
// explicitly-defined points "pointTwo" and "pointThree."
// These points change over time.
rectMode(CORNERS);
fill(255, 255, 0);
rect(pointTwo, pointTwo, pointThree, pointThree);
// The following code changes the coordinates of pointTwo and pointThree.
// Logic (if/else if statements) for direction switching
if (pointTwo >= 475){
directionFlagTwo = -1; // Reverse direction if boundary hit
}
else if (pointTwo <= 25){
directionFlagTwo = 1;
}
// pointTwo and pointThree define the corners of the yellow square
// The y-coordinates of pointTwo and pointThree are derived from the value
// of the sin() function.
pointTwo += 5 * directionFlagTwo;
pointThree += 5 * -1 * directionFlagTwo;
// End code for yellow rectangle

// Red Rectangle
rectMode(CORNER);
fill(255, 0, 0);
rect(20, 10, 100, 50);

// Blue Ellipse
fill(0, 0, 255);
ellipse(70, 100, 100, 50);

// 5-sided Polygon
shape(polygon1, 20, 150);

// Text
fill(255, 255, 255);
String textString1 = "Press the 'g' key to show a fun message!";
textAlign(CENTER);
textSize(20);
text(textString1, 250, 490);

// "Hidden Text" section
// Changes color of text based upon state of "g" key.
if (key == 'g' || key == 'G') {
textColor = 255;
}
// textColor changes depending on the state of the "g" key
fill(textColor);
textAlign(CENTER);
// textString2 contains the hidden text
String textString2 = "\"Computer Graphics\" is awesome!";
// Write the text to the bottom of the screen
text(textString2, 250, 460);
// End "hidden text" section

// Color-changing rectangles - police-light style!
// Color changes based on frame count
rectMode(CORNER);
fill(255 - frameCount%255, 0, frameCount%255);
rect(200, 25, 50, 20);
fill(frameCount%255, 0, 255 - frameCount%255);
rect(150, 25, 50, 20);

// Magenta rectangle whose width changes
rectMode(CENTER);
fill(255, 0, 255);
rect(200, 100, frameCount%100, 20);

// Light blue rectangle whose height changes
rectMode(CENTER);
fill(0, 255, 255);
rect(200, 200, 100, frameCount%20);

} // End draw call```