Yet another paint question
in
Contributed Library Questions
•
11 months ago
Once again, I'm modifying my paint program. This time, i'll be adding a fill bucket.
<sarcasm> Oh, joy! </sarcasm>.
I found this paint bucket function on the internet, and I modified slightly to suit my needs. The only problem is that when I implement it into my paint program, it gives me a NullPointer exception.
- import controlP5.*;
- ControlP5 slider;
- PGraphics canvas;
- int r = 255;
- int g = 255;
- int b = 255;
- float s = 50;
- float br, bg, bb;
- void setup() {
- size(screen.width, screen.height);
- smooth();
- canvas = createGraphics(width-350, height, JAVA2D);
- canvas.beginDraw();
- canvas.smooth();
- canvas.background(br, bg, bb);
- canvas.endDraw();
- cursor(CROSS);
- slider = new ControlP5(this);
- slider.addSlider("r", 0, 255, 10, 10, 255, 30);
- slider.addSlider("g", 0, 255, 10, 50, 255, 30);
- slider.addSlider("b", 0, 255, 10, 90, 255, 30);
- slider.addSlider("s", 0, 100, 10, 130, 255, 30);
- slider.addSlider("br", 0, 255, 10, 510, 255, 30);
- slider.addSlider("bg", 0, 255, 10, 550, 255, 30);
- slider.addSlider("bb", 0, 255, 10, 590, 255, 30);
- }
- void draw() {
- background(0);
- drawMenu();
- image(canvas, 350, 0);
- fill(r,g,b);
- ellipse(mouseX, mouseY, s, s);
- stroke(r, g, b);
- strokeWeight(s);
- if (mousePressed && mouseButton == LEFT && mouseX > 350) {
- canvas.beginDraw();
- canvas.stroke(r, g, b);
- canvas.strokeWeight(s);
- canvas.line(mouseX-350, mouseY, pmouseX-350, pmouseY);
- canvas.endDraw();
- }
- if (mousePressed && mouseButton == RIGHT && mouseX > 350) {
- canvas.beginDraw();
- canvas.stroke(br, bg, bb);
- canvas.strokeWeight(s);
- canvas.line(mouseX-350, mouseY, pmouseX-350, pmouseY);
- canvas.endDraw();
- }
- }
- void drawMenu() {
- noStroke();
- fill(0);
- rect(0, 0, 350, height);
- fill(r, g, b);
- ellipse(100, 300, s, s);
- fill(br, bg, bb);
- ellipse(100, 450, 50, 50);
- fill(200, 0, 0);
- textSize(48);
- text("SAVE", 90, height-90);
- }
- void keyPressed() {
- if (key == ' ') {
- canvas.beginDraw();
- canvas.background(br, bg, bb);
- canvas.endDraw();
- }
- if (key == 'z' || key == 'Z') {
- color c = canvas.get(mouseX-350, mouseY);
- slider.controller("r").setValue(red(c));
- slider.controller("g").setValue(green(c));
- slider.controller("b").setValue(blue(c));
- }
- if (key == 'x' || key == 'X') {
- color c = canvas.get(mouseX-350, mouseY);
- slider.controller("br").setValue(red(c));
- slider.controller("bg").setValue(green(c));
- slider.controller("bb").setValue(blue(c));
- }
- }
- void mouseReleased() {
- if (mouseX <= 350 && mouseY >= 650) {
- String savePath = selectOutput("Save Image");
- canvas.save(savePath);
- }
- }
- void floodFill(int x, int y, int currcol, int newcol) {
- int pixelQueue[] = new int[width * height];
- int pixelQueueSize = 0;
- pixelQueue[0] = (y << 16) + x;
- pixelQueueSize = 1;
- canvas.set(x, y, newcol);
- while (pixelQueueSize > 0) {
- x = pixelQueue[0] & 0xffff;
- y = (pixelQueue[0] >> 16) & 0xffff;
- pixelQueueSize--;
- pixelQueue[0] = pixelQueue[pixelQueueSize];
- if (x > 0) {
- if ((canvas.get(x-1, y) == currcol) && (canvas.get(x-1, y) != newcol)) {
- canvas.set(x-1, y, newcol);
- pixelQueue[pixelQueueSize] = (y << 16) + x-1;
- pixelQueueSize++;
- }
- }
- if (y > 0) {
- if ((canvas.get(x, y-1) == currcol) && (canvas.get(x, y-1) != newcol)) {
- canvas.set(x, y-1, newcol);
- pixelQueue[pixelQueueSize] = ((y-1) << 16) + x;
- pixelQueueSize++;
- }
- }
- if (x < width-1) {
- if ((canvas.get(x+1, y) == currcol) && (canvas.get(x+1, y) != newcol)) {
- canvas.set(x+1, y, newcol);
- pixelQueue[pixelQueueSize] = (y << 16) + x+1;
- pixelQueueSize++;
- }
- }
- if (y < height-1) {
- if ((canvas.get(x, y+1) == currcol) && (canvas.get(x, y+1) != newcol)) {
- canvas.set(x, y+1, newcol);
- pixelQueue[pixelQueueSize] = ((y+1) << 16) + x;
- pixelQueueSize++;
- }
- }
- }
- updatePixels();
- }
1