Problem with PGraphics
in
Contributed Library Questions
•
2 years ago
Firstly, I've been toying around with PGraphics because of the paint program I'm doing. It works fine in the sense that the image that is saved doesn't show the color palette or controlP5 buttons, but if you use the eraser button that I made it changes the color of the line to a color that's almost the background color, but not the actual background color. I don't know what causes this because the background and the fill of the line are the same numbers in the actual code.
Second, whenever I start to color in and drag the mouse over the color palette the line draws over the colors, instead of changing the fill like its supposed to.
Here's the code
- import controlP5.*;
- PGraphics canvas;
- ControlWindow controlWindow;
- ControlP5 controlP5;
- int theValue;
- int QQ;
- float myNumber;
- int cols=10;
- int rows=2;
- color f1 = color(0);
- color f2 = color(200);
- boolean over = false;
- Cell[][] palette = new Cell[cols][rows];
- color[] rectCol = new color[20];
- void setup() {
- size(800,800);
- canvas = createGraphics(800,800,JAVA2D);
- canvas.beginDraw();
- canvas.smooth();
- canvas.endDraw();
- controlP5 = new ControlP5(this);
- Controller New = controlP5.addButton("New",0,0,0,40,19);
- Controller Save = controlP5.addButton("Save",0,0,181,40,19);
- Controller Quit = controlP5.addButton("Quit",0,280,0,40,19);
- Controller Eraser = controlP5.addButton("Eraser",00,280,181,40,19);
- Controller slider = controlP5.addSlider("size",0,200,0,160,50,10,100);
- controlWindow = controlP5.addControlWindow("Stuff",0,0,320,200);
- controlWindow.hideCoordinates();
- controlWindow.setBackground(200);
- New.setWindow(controlWindow);
- Save.setWindow(controlWindow);
- Quit.setWindow(controlWindow);
- Eraser.setWindow(controlWindow);
- slider.setWindow(controlWindow);
- for (int i=0; i<cols; i++) {
- for (int j=0; j<rows; j++) {
- palette[i][j] = new Cell(i*20,j*20,20,20,2*i+j);
- }
- }
- rectCol[0] = color(0); //black
- rectCol[1] = color(255); //white
- rectCol[2] = color(46, 11, 11); //brown
- rectCol[3] = color(120, 10, 10); // dark red
- rectCol[4] = color(255, 0, 0); //red
- rectCol[5] = color(250, 71, 74); // light red
- rectCol[6] = color(245, 59, 255); //pinkish
- rectCol[7] = color(232, 0, 245); // pink
- rectCol[8] = color(129, 3, 137); //purple
- rectCol[9] = color(55, 55, 250); //light blue
- rectCol[10] = color(0, 0, 255); //blue
- rectCol[11] = color(0, 0, 150); //dark blue
- rectCol[12] = color(95, 247, 101); //light green
- rectCol[13] = color(0, 247, 10); //lime green
- rectCol[14] = color(0, 103, 4); //dark green
- rectCol[15] = color(58, 103, 0); //yellow green
- rectCol[16] = color(255, 255, 0); //yellow
- rectCol[17] = color(255, 100, 0); //orange
- rectCol[18] = color(155); // gray
- rectCol[19] = color(55); //grayer
- }
- void draw() {
- for (int i=0; i<cols; i++) {
- for (int j=0; j<rows; j++) {
- palette[i][j].display();
- checkMouseOver(palette[i][j]);
- }
- }
- image(canvas,0,0);
- }
- void mouseDragged() {
- if (over == false) {
- if (mouseButton == LEFT) {
- canvas.beginDraw();
- canvas.noFill();
- canvas.stroke(f1);
- canvas.strokeWeight(myNumber);
- canvas.strokeJoin(ROUND);
- canvas.line(mouseX,mouseY,pmouseX,pmouseY);
- canvas.endDraw();
- }
- if (mouseButton == RIGHT) {
- canvas.beginDraw();
- canvas.noFill();
- canvas.stroke(f2);
- canvas.strokeWeight(myNumber);
- canvas.strokeJoin(ROUND);
- canvas.line(mouseX,mouseY,pmouseX,pmouseY);
- canvas.endDraw();
- }
- }
- }
- void checkMouseOver(Cell c) {
- if (mousePressed) {
- if (mouseButton == LEFT && mouseX > c.x && mouseX < c.x+c.w && mouseY > c.y && mouseY < c.y+c.h) {
- f1 = (get(mouseX,mouseY));
- over = true;
- }else if (mouseButton == RIGHT && mouseX > c.x && mouseX < c.x+c.w && mouseY > c.y && mouseY < c.y+c.h) {
- f2 = (get(mouseX,mouseY));
- over = true;
- }
- }else{
- over = false;
- }
- }
- class Cell {
- float x,y,w,h;
- int c;
- Cell (float tempX, float tempY, float tempW, float tempH, int tempC) {
- x = tempX;
- y = tempY;
- w = tempW;
- h = tempH;
- c = tempC;
- }
- void display() {
- stroke(0);
- strokeWeight(3);
- fill(rectCol[c]);
- rect(x,y,w,h);
- }
- }
- void size(float theEllipse) {
- myNumber = theEllipse;
- }
- public void New(int theValue) {
- canvas.background(200,200,200);
- }
- public void Save(int theValue){
- canvas.save("picture.jpg");
- }
- public void Quit(int QQ){
- exit();
- }
- public void Eraser(int theValue){
- f1 = color(200);
- f2 = color(200);
- }
1