nfg_day8

code PImage img; PImage drawImg; float circleSize = 30;
 * Ex.9 mask drawing**

void setup { img = loadImage("photo0.jpg"); size(img.width,img.height); drawImg = createImage(img.width,img.height,RGB); }

void draw { image(drawImg,0,0); if(mousePressed){ noStroke; fill(255); ellipse(mouseX,mouseY,circleSize,circleSize); } drawImg = get(0,0,width,height); drawImg.loadPixels; img.mask(drawImg); image(img,0,0); stroke(255,0,0); noFill; ellipse(mouseX,mouseY,circleSize,circleSize); } code

code color a = color(255,0,0); color b = color(0,0,255); color c = blendColor(a,b,ADD); color d = a;
 * Ex.8 blend color**

void setup{ size(640,360); }

void draw{ fill(d); rect(20,20,100,100); }

void mousePressed{ d = c; }

void mouseReleased{ d = a; }

code

code float posX = 320; float posY = 180; int numImages = 30; PImage[] images = new PImage[numImages]; int currentImage = 0; boolean playSwitch = false;
 * Ex.7 photo array with blend modes**

void setup{ size(640,360); for(int i=0;i<images.length;i++){ images[i] = loadImage("photo"+i+".jpg"); } }

void draw{ image(images[0],0,0,width,height); blender(images[currentImage],posX,posY,"DIFFERENCE"); if(playSwitch && currentImage < numImages-1){ currentImage++; } else { playSwitch=false; currentImage=0; } println("playSwitch: " + playSwitch); }

void mousePressed{ playSwitch = true; }

void blender(PImage p, float x, float y, String s){ int blendMode = 0; String[] blendModes = { "BLEND", "ADD", "SUBTRACT", "LIGHTEST", "DARKEST", "DIFFERENCE", "EXCLUSION", "MULTIPLY", "SCREEN", "OVERLAY", "HARD_LIGHT", "SOFT_LIGHT", "DODGE", "BURN" }; int[] blendModeCodes = { 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192 };  for(int i=0;i<blendModes.length;i++){ if(s==blendModes[i]){ blendMode = blendModeCodes[i]; } }  blend(p,0,0,p.width,p.height,int(x)-(p.width/2),int(y)-(p.height/2),p.width,p.height,blendMode); }

code

code import processing.opengl.*;
 * Ex.6 3D demo**

float posX = 320; float posY = 180; float rot = 0;

void setup{ size(640,360,OPENGL); smooth; ellipseMode(CENTER); }

void draw{ background(200); ellipse(220,180,40,40); ellipse(420,180,40,40); pushMatrix; translate(posX,posY,mouseX-320); rotateY(radians(rot)); ellipse(0,0,40,40); popMatrix; rot+=10; }

code

code int numImages = 30; PImage[] images = new PImage[numImages]; int currentImage = 0; boolean playSwitch = false;
 * Ex.5 play through array and stop**

void setup{ size(640,360); for(int i=0;i<images.length;i++){ images[i] = loadImage("photo"+i+".jpg"); } }

void draw{ image(images[currentImage],0,0); if(playSwitch && currentImage < numImages-1){ currentImage++; } println("playSwitch: " + playSwitch); }

void mousePressed{ playSwitch = true; }

code

code boolean page1 = true; boolean page2 = false; boolean page3 = false;
 * Ex.4 multiple stages**

void setup{ size(640,360); }

void draw{ if(page1){ drawPage1; } else if(page2){ drawPage2; } else if(page3){ drawPage3; } }

void drawPage1{ background(0); noStroke; fill(150,0,0); rectMode(CENTER); rect(120,120,300,50); }

void drawPage2{ background(150,0,0); noStroke; fill(150,150,0); rectMode(CENTER); rect(320,120,300,50); }

void drawPage3{ background(150,150,0); noStroke; fill(0); ellipseMode(CENTER); for(int i=0;i<100;i++){ ellipse(random(width),random(height),10,10); } }

void mousePressed{ if(page1){ if(hitDetect(mouseX,mouseY,0,0,120,120,300,50)){ page1=false; page2=true; page3=false; } } else if(page2){ if(hitDetect(mouseX,mouseY,0,0,320,120,300,50)){ page1=false; page2=false; page3=true; } } else if(page3){ } }

boolean hitDetect(float x1, float y1, float w1, float h1, float x2, float y2, float w2, float h2) { w1 /= 2; h1 /= 2; w2 /= 2; h2 /= 2; if(x1 + w1 >= x2 - w2 && x1 - w1 <= x2 + w2 && y1 + h1 >= y2 - h2 && y1 - h1 <= y2 + h2) { return true; } else { return false; } }

code

code float x1 = 320; float y1 = 200; float x2 = 30; float y2 = 30; float x3 = 400; float y3 = 180; float pointSize = 20; boolean point1clicked = false; boolean point2clicked = false; boolean point3clicked = false;
 * Ex.3 drag and drop**

float targetX = 300; float targetY = 300; float targetSize = 100;

color bgColor = color(200);

void setup{ size(640,360); }

void draw{ background(bgColor); noStroke; fill(155,0,0); ellipseMode(CENTER); ellipse(targetX,targetY,targetSize,targetSize);

if(point1clicked){ x1 = mouseX; y1 = mouseY; } if(point2clicked){ x2 = mouseX; y2 = mouseY; } if(point3clicked){ x3 = mouseX; y3 = mouseY; }

strokeWeight(pointSize); stroke(255,0,0); point(x1,y1); stroke(0,255,0); point(x2,y2); stroke(0,0,255); point(x3,y3);

//if we bring a point to the target if(hitDetect(x1,y1,pointSize,pointSize,targetX,targetY,targetSize,targetSize)){ bgColor = color(255,0,0); } else if(hitDetect(x2,y2,pointSize,pointSize,targetX,targetY,targetSize,targetSize)){ bgColor = color(0,255,0); } else if(hitDetect(x3,y3,pointSize,pointSize,targetX,targetY,targetSize,targetSize)){ bgColor = color(0,0,255); } else { bgColor = color(200); }

println("1: " + point1clicked + "  2: " + point2clicked + "   3: " + point3clicked); }

void mousePressed{ if(hitDetect(mouseX,mouseY,0,0,x1,y1,pointSize,pointSize)){ point1clicked = true; point2clicked = false; point3clicked = false; } if(hitDetect(mouseX,mouseY,0,0,x2,y2,pointSize,pointSize)){ point2clicked = true; point1clicked = false; point3clicked = false; } if(hitDetect(mouseX,mouseY,0,0,x3,y3,pointSize,pointSize)){ point3clicked = true; point1clicked = false; point2clicked = false; } }

void mouseReleased{ point1clicked = false; point2clicked = false; point3clicked = false; }

boolean hitDetect(float x1, float y1, float w1, float h1, float x2, float y2, float w2, float h2) { w1 /= 2; h1 /= 2; w2 /= 2; h2 /= 2; if(x1 + w1 >= x2 - w2 && x1 - w1 <= x2 + w2 && y1 + h1 >= y2 - h2 && y1 - h1 <= y2 + h2) { return true; } else { return false; } } code

code import gifAnimation.*;
 * Ex.2 swapping out GIFs**

Gif sonic, zelda, current;

void setup{ size(200,200); zelda = new Gif(this,"test.gif"); sonic = new Gif(this,"sonic.gif"); current = sonic; current.play; noCursor; }

void draw{ background(200); imageMode(CENTER); image(current,mouseX,mouseY); }

void keyPressed{ if(key==' '){ current=zelda; current.play; } if(key=='a'){ current.stop; } } code

code float x1 = 10; float y1 = 10;
 * Ex.1 racing game**

float x2 = 10; float y2 = 200;

float goalX = 500;

float player1speed = 5; float player2speed = 20;

color bgColor = color(200);

void setup{ size(640,360); }

void draw{ background(bgColor); stroke(0); strokeWeight(20); point(x1,y1); point(x2,y2); x1 += random(player1speed); if(keyPressed){ x2 += random(player2speed); } strokeWeight(2); stroke(255,0,0); line(goalX,0,goalX,height); if(x1>goalX && x2<=goalX){ bgColor = color(255,0,0); } else if(x2>goalX){ bgColor = color(0,255,0); } } code