sketchpad drawing problems
in
Programming Questions
•
1 year ago
I'm quite new to coding and using Processing, but I've currently came up with this. It draws a shape when I press the buttons which I binded it to, but when I click the palette to change the colour, it also draws under the palette. Is there a way to prevent this?
Also when I try to increase or decrease the size by pressing UP or DOWN, the drawing tool just stops and I have to press it again for it to work. How can I get it to keep the drawing tool when I press it?
- int row = 1;
- int col = 10;
- int shapeSize = 150;
- color mouseCol = color(0);
- Box[][] palette = new Box[col][row];
- color[] boxCol = new color[10];
- float targetX, targetY;
- float penX, penY, ppenX, ppenY;
- void setup(){
- size(500,500);
- background(255);
- noStroke();
- smooth();
- for (int i=0; i<col; i++) {
- for (int j=0; j<row; j++) {
- palette[i][j] = new Box(3+i*25,3+j*20,20,20,2*j +i);
- }
- }
- boxCol[0] = color(0); // D
- boxCol[1] = color(255); // White
- boxCol[2] = color(128); // Grey
- boxCol[3] = color(255, 0, 0); // Red
- boxCol[4] = color(255, 106, 0); // Orange
- boxCol[5] = color(255, 216, 0); // Yellow
- boxCol[6] = color(0, 255, 33); // Green
- boxCol[7] = color(0, 216, 255); // Blue
- boxCol[8] = color(111, 0, 255); // Indigo
- boxCol[9] = color(143, 0, 255); // Violet
- }
- void draw(){
- for (int i=0; i<col; i++) {
- for (int j=0; j<row; j++) {
- palette[i][j].display();
- checkMouseOver(palette[i][j]);
- }
- }
- if (key == 'v'){
- if (penX != targetX || penY != targetY) {
- ppenX = penX;
- ppenY = penY;
- penX = penX + 0.05 * (targetX - penX);
- penY = penY + 0.05 * (targetY - penY);
- stroke(mouseCol);
- line(ppenX, ppenY, penX, penY);
- }
- }
- }
- void keyPressed() {
- if (key = CODED){
- if (keyCode == UP) {
- shapeSize = shapeSize + 15;
- }
- }
- if (key = CODED){
- if (keyCode == DOWN){
- shapeSize = shapeSize - 15;
- }
- }
- }
- void mousePressed(){
- if (key == 'z') { // To draw an arc
- noStroke();
- fill(mouseCol);
- arc(mouseX, mouseY, shapeSize, shapeSize, PI, TWO_PI);
- }
- if (key == 'x') { // To draw a circle
- noStroke();
- fill(mouseCol);
- ellipse(mouseX, mouseY, shapeSize, shapeSize);
- }
- if (key == 'c') { // To draw a square
- noStroke();
- fill(mouseCol);
- rectMode(CENTER);
- rect(mouseX, mouseY, shapeSize, shapeSize);
- }
- targetX = mouseX;
- targetY = mouseY;
- penX = targetX;
- penY = targetY;
- ppenX = penX;
- ppenY = penY;
- }
- void mouseDragged() {
- targetX = mouseX;
- targetY = mouseY;
- }
- void checkMouseOver(Box c) {
- if (mousePressed) {
- if (mouseButton == LEFT && mouseX > c.x && mouseX < c.x+c.w && mouseY > c.y && mouseY < c.y+c.h) {
- mouseCol = (get(mouseX,mouseY));
- }
- }
- }
- class Box {
- float x,y,w,h;
- int c;
- Box (float tempX, float tempY, float tempW, float tempH, int tempC) {
- x = tempX;
- y = tempY;
- w = tempW;
- h = tempH;
- c = tempC;
- }
- void display() {
- stroke(0);
- rectMode(CORNER);
- strokeWeight(3);
- fill(boxCol[c]);
- rect(x,y,w,h);
- }
- }
1