// 6.914 HW2 #1 int newLine = 20, charForward = 10; int margin = 15, xPos = margin, yPos = margin+newLine; PFont myFont; void setup(){ size(300, 500); background(255); myFont = createFont("Courier", 16); } void draw(){ fill(0); textFont(myFont); if (xPos > (width-margin-charForward)){ xPos = margin; yPos += newLine; } } void keyPressed(){ if (key == BACKSPACE || key == DELETE){ println("backspace pressed"); xPos -= charForward; stroke(255); fill(255); rect(xPos, yPos - newLine, charForward, newLine+2); fill(0); } else if (key == ENTER){ xPos = margin; yPos += newLine; } else { text(key, xPos, yPos); xPos += charForward; } } // 6.914 HW2 #2, drawing program boolean clear = true; color currentColor; int boxWidth = 38, boxHeight = 29; int strokeDiameters[] = { 6, 12, 20 }; float xStart, yStart; PFont myFont; // what are the colors we want? // in the original group, there were three greens, // two of which were ugly. int[] colorsWeWant = { 5, 15, 35, 55, 65, 75, 85, 95}; void setup(){ colorMode(HSB, 100.0); size(600, 600); background(0); smooth(); frameRate(40); myFont = createFont("Tahoma", 28); textFont(myFont); } void draw(){ // drawing background and white box if clear has been pressed if (clear == true){ noStroke(); noFill(); //background background(0); currentColor = color(0); // white box fill(0, 0, 100); rect(50, 125, width - 100, height - 175); clear = false; // drawing boxes to click in for colors for (int i = 0; i < colorsWeWant.length; i ++){ // first row fill(colorsWeWant[i], 100, 100); rect(50 + i*39, 30, boxWidth, boxHeight); // second row fill(colorsWeWant[i], 100, 70); rect(50 + i*39, 60, boxWidth, boxHeight); } // drawing circles to click for strokeWeights yStart = 30; xStart = width-50-strokeDiameters.length*30; for (int i = 0; i < strokeDiameters.length; i++){ fill(80, 70, 255); noStroke(); ellipse(xStart + 30*i, yStart, strokeDiameters[i], strokeDiameters[i]); } // drawing word "clear" fill(100); text("clear", width-50-strokeDiameters.length*30, 80); } // only draw if mouse is in white box and mouse is pressed if (mousePressed && (mouseX > 50) && (mouseX < (width-50)) && (mouseY > 125) && (mouseY < (height-50))){ stroke(currentColor); line(pmouseX, pmouseY, mouseX, mouseY); } } void mousePressed(){ // drawing boxes to click in for colors for (int i = 0; i < colorsWeWant.length; i ++){ // first row, check for click int boxX = 50+i*39, boxY = 30; if (mouseX>boxX && mouseY>boxY && mouseXboxX && mouseY>boxY && mouseX width-60-strokeDiameters.length*30 && mouseX < width-5-strokeDiameters.length*30 && mouseY > 55 && mouseY < 85){ clear = true; } } // 6.914 HW2 #3, bouncing shapes //setting up variables for position and size int x = 0, y = 20, w = 10; //setting up a boolean for moving sideways boolean movingRight; void setup(){ size(800, 240); // HSB mode for gradient colorMode(HSB,100); } void draw(){ background(0); noStroke(); //drawing a grid of circles for (int i=0; i<10; i++){ for (int j=0; j<10; j++){ //rainbow fill fill(i*10, 90, 100-i*10); rect(x + 20*i, 20 + y*j, w, w); } } if (x < 20) // off left side of screen, with margin movingRight=true; else if (x > width-210) // off right side of screen, with margin movingRight=false; if (movingRight) // moving right x += 3; else // moving left x -= 3; } // 6.914 HW2 #4, shapes that follow the mouse int placesToRemember = 20; float[] posX = new float[placesToRemember], posY = new float[placesToRemember]; float increment = 100/placesToRemember; void setup(){ size(500, 500); colorMode(HSB, 100.0); strokeWeight(10); smooth(); } void draw(){ background(100); // updating array for (int i = 1; i < placesToRemember; i++){ posX[i-1] = posX[i]; posY[i-1] = posY[i]; } // inserting new mouse position posX[placesToRemember-1] = mouseX; posY[placesToRemember-1] = mouseY; // draw the points for (int i = 0; i < placesToRemember; i++){ stroke(increment * i, 100, 100); point(posX[i], posY[i]); } } // 6.914 HW2 #5, continuously moving circle boolean horizontal = true; float xPos = 250, yPos = 250; int velocity = 2, diameter = 40; void setup(){ size(500, 500); } void draw(){ background(0); smooth(); // going off edges if (xPos < 0) xPos = width; else xPos %= width; if (yPos < 0) yPos = height; else yPos %= height; // going in same direction if (horizontal){ xPos += velocity; } else { yPos += velocity; } // draw circle ellipse(xPos, yPos, diameter, diameter); } void keyPressed(){ if (key == CODED){ switch (keyCode){ case UP: velocity = -2; horizontal = false; break; case DOWN: velocity = 2; horizontal = false; break; case LEFT: velocity = -2; horizontal = true; break; case RIGHT: velocity = 2; horizontal = true; break; } } else { if (key == 'd' || key == 'D'){ velocity *= 2; } else if (key == 'h' || key == 'H'){ velocity /= 2; } } }