//Assignment 3 //Global variables xImage b; xImage a; int blockSize = 2; PFont font; //Setup Method---------------------------------------------------------------------------------- void setup() { size(430, 323); background (10); noStroke(); font = loadFont ("Andalus-22.vlw"); textFont (font, 14); a = new xImage("bitrilito.jpg"); image(a, 220, 10); b = new xImage("bitrilito.jpg"); image(b, 10, 10); } //Draw method----------------------------------------------------------------------------------- void draw() { b.mosaic(blockSize); b.updatePixels(); } void keyPressed(){ // las teclas no funcionaban porque esto debe estar implementado aquí una vez que uno lo ve es fácil :-) if (keyPressed == true && key == '+') blockSize = blockSize+1; if (keyPressed == true && key == '-') blockSize = abs(blockSize-1); } void mousePressed() { if (mouseButton == RIGHT) blockSize=blockSize+1; if (mouseButton == LEFT) blockSize=abs(blockSize-1); } //Subclass that contains a mosaic method---------------------------------------------------------- class xImage extends PImage { xImage(){ super(); } xImage(String filename){ PImage X = loadImage(filename); this.width = X.width; this.height = X.height; this.pixels = X.pixels; this.format = RGB; this.cache = null; } color colour (int x , int y) { x = constrain(x, 10, width-1); y = constrain(y, 10, height-1); return pixels[y*width+x]; } void mosaic(int blockSize) { if (blockSize <=1) { fill (255); text ("Almost like without pixelation!", 15, 30); return; } if (blockSize*10 > this.width) { fill (255); text ("Isn't pixelated enough?", 50,30); return; } for (int i = 10; i <= height; i += blockSize) { for (int j = 10; j <= width; j += blockSize) { fill(colour(j + blockSize/2, i + blockSize/2)); rect(j, i, blockSize, blockSize); } } } }