/* ----------------------------------------------------------------------------- |drawingtool by grrrr, feel free to use or to change the code(copyleft) |(this is a processing-file to build a javapplet) |visit www.processing.org |visit www.GRRRR.net */ int rectX, rectY; // Position of black button int rect2X, rect2Y; // Position of grey button int rect3X, rect3Y; // Position of draw button int rect4X, rect4Y; // Position of flood button int rect5X, rect5Y; // Position of night button int circleX, circleY; // Position of white button int rectSize = 12; // Diameter of black button int rect2Size = 12; // Diameter of grey button int rect3Size = 12; // Diameter of draw button int rect4Size = 12; // Diameter of flood button int rect5Size = 12; // Diameter of night button int circleSize = 12; // Diameter of white button boolean zeichne = true; color rectColor, circleColor, rect2Color; color currentColor; boolean rectOver = false; boolean rect2Over = false; boolean rect5Over = false; boolean circleOver = false; BImage bg; void setup() { size(1000, 737); // must be the same size as backgroundpicture rectColor = color(0, 0, 0); circleColor = color(255, 255, 255); rect2Color = color(128, 128, 128); currentColor = rectColor; circleX = 0; circleY = 0; rectX = 24; rectY = 0; rect2X = 12; rect2Y = 0; rect3X = 36; rect3Y = 0; rect4X = 48; rect4Y = 0; rect5X = 60; rect5Y = 0; ellipseMode(CENTER_DIAMETER); bg = loadImage("usa_abc.gif"); // backgroundpicture to draw on background(bg); } void loop() { update(mouseX, mouseY); stroke(0); fill(rectColor); rect(rectX, rectY, rectSize, rectSize);// draws the black button fill(circleColor); rect(circleX, circleY, circleSize, circleSize);// draws the white button fill(rect2Color); rect(rect2X, rect2Y, rect2Size, rect2Size);// draws the grey button fill(255); rect(rect3X, rect3Y, rect3Size, rect3Size); line(rect3X+2, rect3Y+8, rect3X+9, rect3Y+1); line(rect3X+3, rect3Y+8, rect3X+9, rect3Y+2); line(rect3X+4, rect3Y+8, rect3X+10, rect3Y+2); line(rect3X+4, rect3Y+9, rect3X+10, rect3Y+3); line(rect3X+4, rect3Y+10, rect3X+11, rect3Y+3); line(rect3X+2, rect3Y+10, rect3X+4, rect3Y+10); line(rect3X+2, rect3Y+8, rect3X+2, rect3Y+10); // draws the pencil button fill(255); rect(rect4X, rect4Y, rect4Size, rect4Size); line(rect4X+1, rect4Y+6, rect4X+6, rect4Y+1); line(rect4X+2, rect4Y+6, rect4X+6, rect4Y+2); line(rect4X+2, rect4Y+7, rect4X+7, rect4Y+2); line(rect4X+3, rect4Y+7, rect4X+7, rect4Y+3); line(rect4X+3, rect4Y+8, rect4X+8, rect4Y+3); line(rect4X+4, rect4Y+8, rect4X+8, rect4Y+4); line(rect4X+4, rect4Y+9, rect4X+9, rect4Y+4); line(rect4X+9, rect4Y+5, rect4X+9, rect4Y+10); line(rect4X+10, rect4Y+5, rect4X+10, rect4Y+9); // draws the flood button fill(0); rect(rect5X, rect5Y, rect5Size, rect5Size); stroke(255); line(rect5X+2, rect5Y+4, rect5X+2, rect5Y+7); line(rect5X+3, rect5Y+3, rect5X+3, rect5Y+8); line(rect5X+4, rect5Y+2, rect5X+4, rect5Y+2); line(rect5X+4, rect5Y+6, rect5X+4, rect5Y+9); line(rect5X+5, rect5Y+7, rect5X+5, rect5Y+9); line(rect5X+6, rect5Y+8, rect5X+8, rect5Y+8); line(rect5X+6, rect5Y+9, rect5X+7, rect5Y+9); line(rect5X+9, rect5Y+7, rect5X+9, rect5Y+7); // draws the night button if(mousePressed && zeichne == true) { stroke(currentColor); line(mouseX, mouseY, pmouseX, pmouseY); line(mouseX-1, mouseY, pmouseX-1, pmouseY); line(mouseX+1, mouseY, pmouseX+1, pmouseY); line(mouseX, mouseY-1, pmouseX, pmouseY-1); line(mouseX, mouseY+1, pmouseX, pmouseY+1); } if(mousePressed && zeichne == false) { floodFillSimple2(mouseX, mouseY, currentColor, width, height); } if(mousePressed && rect5Over == true) { for(int ix=0; ix<1000; ix++) { for(int iy=0; iy<737; iy++) { // Get colour for ix, iy pixel color ct = get(ix, iy); // Calculate averaged grey value int gval = int(red(ct) + green(ct) + blue(ct))/3; // Inverted image set(ix, iy, color(255-gval, 255-gval, 255-gval)); } } } } void update(int x, int y) { if( overCircle(circleX, circleY, circleSize, circleSize) ) { circleOver = true; rectOver = rect2Over = rect5Over = false; } else if ( overRect(rectX, rectY, rectSize, rectSize) ) { rectOver = true; circleOver = rect2Over = rect5Over = false; } else if ( overRect2(rect2X, rect2Y, rect2Size, rect2Size) ) { rectOver = circleOver = rect5Over = false; rect2Over = true; } else if (mousePressed && overRect3(rect3X, rect3Y, rect3Size, rect3Size) ) { zeichne = true; rect5Over = false; } else if (mousePressed && overRect4(rect4X, rect4Y, rect4Size, rect4Size) ) { zeichne = rect5Over = false; } else if ( overRect5(rect5X, rect5Y, rect5Size, rect5Size) ) { rect5Over = true; } else { rectOver = rect2Over = circleOver = rect5Over = false; } } void mousePressed() { stroke(currentColor); if(circleOver) { currentColor = circleColor; } if(rectOver) { currentColor = rectColor; } if(rect2Over) { currentColor = rect2Color; } } boolean overRect(int x, int y, int width, int height) { if (mouseX >= x && mouseX <= x+width && mouseY >= y && mouseY <= y+height) { return true; } else { return false; } } boolean overCircle(int x, int y, int width, int height) { if (mouseX >= x && mouseX <= x+width && mouseY >= y && mouseY <= y+height) { return true; } else { return false; } } boolean overRect2(int x, int y, int width, int height) { if (mouseX >= x && mouseX <= x+width && mouseY >= y && mouseY <= y+height) { return true; } else { return false; } } boolean overRect3(int x, int y, int width, int height) { if (mouseX >= x && mouseX <= x+width && mouseY >= y && mouseY <= y+height) { return true; } else { return false; } } boolean overRect4(int x, int y, int width, int height) { if (mouseX >= x && mouseX <= x+width && mouseY >= y && mouseY <= y+height) { return true; } else { return false; } } boolean overRect5(int x, int y, int width, int height) { if (mouseX >= x && mouseX <= x+width && mouseY >= y && mouseY <= y+height) { return true; } else { return false; } } /* ----------------------------------------------------------------------------- | | line fill using stack. | | the idea is to fill in lines either in x or y direction. | starting from the initial x,y step in each of these directions as long as possible | seeding new scans-lines perpendicular to the step direction. | | A A A A A A A (position, directions) | | | | | | | | <- onto stack, next scans (x, y-1, 0, -1) | <-------X-----| <- current scan-line (x, y, -1, 0) | | | | | | | | <- onto stack, next scans (x, y+1, 0, 1) | V V V V V V V */ void floodFillSimple2(int x, int y, int fill_color, int w, int h) { int old_color = get(x,y); if( fill_color != old_color ) { fillSegment fc = new fillSegment( x-1, y, -1, 0, fill_color ); fillStack2 fs = new fillStack2(fc); fs.push(new fillSegment( x+1, y, 1, 0, fill_color )); fs.push(new fillSegment( x, y-1, 0, -1, fill_color )); fs.push(new fillSegment( x, y+1, 0, 1, fill_color )); set(x, y, fill_color); while (fs.index >= 0) { fc = fs.pop(); if (get(fc.x,fc.y) != fill_color & get(fc.x,fc.y) == old_color) { set(fc.x, fc.y, fc.c); if (fc.dy == 0) { fs.push(new fillSegment( fc.x, fc.y-1, 0, -1, fill_color )); fs.push(new fillSegment( fc.x, fc.y+1, 0, 1, fill_color )); } else if (fc.dx == 0) { fs.push(new fillSegment( fc.x-1, fc.y, -1, 0, fill_color )); fs.push(new fillSegment( fc.x+1, fc.y, 1, 0, fill_color )); } int current_color = get(fc.x+fc.dx, fc.y+fc.dy); while (current_color != fill_color & current_color == old_color) { fc.x += fc.dx; fc.y += fc.dy; if (fc.x < 0) { fc.x = 0; break; } if (fc.x >= w) { fc.x = w-1; break; } if (fc.y < 0) { fc.y = 0; break; } if (fc.y >= h) { fc.y = h-1; break; } set(fc.x, fc.y, fc.c); if (fc.dy == 0) { fs.push(new fillSegment( fc.x, fc.y-1, 0, -1, fill_color )); fs.push(new fillSegment( fc.x, fc.y+1, 0, 1, fill_color )); } else if (fc.dx == 0) { fs.push(new fillSegment( fc.x-1, fc.y, -1, 0, fill_color )); fs.push(new fillSegment( fc.x+1, fc.y, 1, 0, fill_color )); } current_color = get(fc.x+fc.dx, fc.y+fc.dy); } } } } } class fillSegment { int x; int y; int dx; int dy; int c; fillSegment (int _x, int _y, int _dx, int _dy, int _c) { x = _x; y = _y; dx = _dx; dy = _dy; c = _c; } public fillSegment copy () { return new fillSegment(x, y, dx, dy, c); } } class fillStack2 { fillSegment[] fills; int MAX_FILLS = 5000; int index; fillStack2( fillSegment fc ) { fills = new fillSegment[MAX_FILLS]; index = 0; fills[index] = fc.copy(); } void push (fillSegment fc) { index++; if (index < MAX_FILLS) { fills[index] = fc.copy(); } else { print("FillStackOverFlow ----"); MAX_FILLS += MAX_FILLS; fillSegment[] fTemp = new fillSegment[MAX_FILLS]; System.arraycopy(fills, 0, fTemp, 0, fills.length); fills = fTemp; fills[index] = fc.copy(); println("Stack resized to: " + MAX_FILLS); } } fillSegment pop () { fillSegment fr = fills[index].copy(); fills[index] = null; index--; return fr; } } /* ----------------------------------------------------------------------------- | | brute force method using stack. | | current pixel: P | for every pixel P from stack that is valid | set color and put it's 4 neighbors (X) on the stack. | | | X | | X | P | X | | X | */ void floodFillSimple1(int x, int y, int fill_color) { int old_color = get(x,y); if( fill_color != old_color ) { fillCoordinate fc = new fillCoordinate( x, y, fill_color ); fillStack1 fs = new fillStack1(fc); while (fs.index >= 0) { fc = fs.pop(); if (get(fc.x,fc.y) != fill_color & get(fc.x,fc.y) == old_color) { set(fc.x, fc.y, fc.c); fs.push(new fillCoordinate( fc.x, fc.y-1, fill_color )); fs.push(new fillCoordinate( fc.x, fc.y+1, fill_color )); fs.push(new fillCoordinate( fc.x-1, fc.y, fill_color )); fs.push(new fillCoordinate( fc.x+1, fc.y, fill_color )); } } } } class fillCoordinate { int x; int y; int c; fillCoordinate (int _x, int _y, int _c) { x = _x; y = _y; c = _c; } fillCoordinate copy () { return new fillCoordinate(x, y, c); } } class fillStack1 { fillCoordinate[] fills; int MAX_FILLS = 10000; int index; fillStack1( fillCoordinate fc ) { fills = new fillCoordinate[MAX_FILLS]; index = 0; fills[index] = fc.copy(); } void push (fillCoordinate fc) { index++; if (index < MAX_FILLS) { fills[index] = fc.copy(); } else { print("FillStackOverFlow ----"); MAX_FILLS += MAX_FILLS; fillCoordinate[] fTemp = new fillCoordinate[MAX_FILLS]; System.arraycopy(fills, 0, fTemp, 0, fills.length); fills = fTemp; fills[index] = fc.copy(); println("Stack resized to: " + MAX_FILLS); } } fillCoordinate pop () { fillCoordinate fr = fills[index].copy(); fills[index] = null; index--; return fr; } }