Digital Craft: Drawing Tool Finalized

After the second iteration of my drawing tool, I decided to take out the “try pressing B” text and instead added another function such that if you press any key, the background would be filled with rectangles that would slowly cover up the entire background image the more keys you press.

The final code:

PImage img;
int rectWidth;

void setup() {
size(965, 785);
img = loadImage(“garden.jpg”);
background(img);

textSize(32);
text(“Draw”, 20, 50);

textSize(32);
text(“& Relax”, 50, 100);

fill(0, 0, 0, 255);
rect(230, 100, 440, 100);

fill(0, 0, 0, 191);
noStroke();
rect(230, 250, 440, 100);

fill(0, 0, 0, 127);
rect(230, 400, 440, 100);

fill(0, 0, 0, 95);
rect(230, 550, 440, 100);

rectWidth = width/4;

}

void draw() {
if (mousePressed == true) {
stroke(255);
strokeWeight(1);
line(mouseX-10, mouseY-20, pmouseX, pmouseY);
}
if (mousePressed == true) {
stroke(255);
strokeWeight(2);
line(mouseX, mouseY, pmouseX-20, pmouseY-10);
}
if (mousePressed == true) {
fill(255);
noStroke();
} else {
fill(0);
}

textSize(32);
text(“Draw”, 20, 50);
textSize(32);
text(“& Relax”, 50, 100);

}

void keyPressed() {
int keyIndex = -1;
if (key >= ‘A’ && key <= ‘Z’) {
keyIndex = key – ‘A’;
} else if (key >= ‘a’ && key <= ‘z’) {
keyIndex = key – ‘a’;
}
if (keyIndex == -1) {
// If it’s not a letter key, clear the screen
background(0);
} else {
// It’s a letter key, fill a rectangle
fill(millis() % 255);
float x = map(keyIndex, 0, 25, 0, width – rectWidth);
rect(x, 0, rectWidth, height);
}
}

Leave a reply

Skip to toolbar