Help with color changing and display of drawn objects

Hello everybody, I am currently in 4th year of my college and I need some help with my code. (My coding skills aren’t that great…)

We got assignment to create simpler version of MS Paint in Processing.

Currently, my program can do following:

1) You can choose which color you want in left palette (color palette), but it only works for stroke (left mouse button), not for fill (right mouse button).
2) You can choose which shape you want to draw from right palette (shape palette).
3) You can either draw free-hand with pencil or shapes (shapes are drawn interactively by pressing and releasing left mouse button).
4) You can also delete all you have drawn.

I have two problems which I can’t solve.

1) How should fill color change when you press on color in color palette?
I tried with array of colors, with for loop, multiple ifs, but nothing really works for me.
Stroke works fine everytime, but fill doesn’t.

2) When shapes are drawn interactively, they are only shown when drawing them is finished, but you cannot “preview” what it will look like when you start drawing.
I tried by inserting new method “void display{}” but I can’t find out how to use it properly…

Pasted part of the code is relevant for my question, other half of the code, such as palettes is intentionally left out, to not cause overcluster.

EDIT
Problem 1) is solved, but problem 2) still persists.

CODE:

//Color variables and array
color red= color(255, 0, 0);
color green= color(0, 255, 0);
color blue= color(0, 0, 255);
color yellow= color(255, 255, 0);
color magenta= color(255, 0, 255);
color cyan= color(0, 255, 255);
color orange= color(255, 122, 0);
color gray= color(122);
color white= color(255);
color black= color(0);
color[] colors = {
  red,blue,green,yellow,magenta,cyan,orange,gray,white,black
};
//Stroke width variables
float masterStroke= 1;

//Booleans for drawing shapes
boolean dLine = false;
boolean dRect = false;
boolean dPenc = false;
boolean dCirc = false;

//Int variable for rectangle
int a,b,c,d = 0;

//Main
void setup(){
  size(1200,800);
  smooth();
  background(255);
  
}

//Loop
void draw(){
  
  //Click on color buttons changes color of stroke
  if(mousePressed) {
    if(mouseX > 10 && mouseX < 35){
      if(mouseY > 10 && mouseY < 35){
        if(mouseButton == LEFT){
          stroke(colors[0]);
        }else if (mouseButton == RIGHT){
          fill(colors[0]);
        }
      }
      if(mouseY > 35 && mouseY < 60){
        stroke(colors[2]);
      }
      if(mouseY > 60 && mouseY < 85){
        stroke(colors[4]);
      }
      if(mouseY > 85 && mouseY < 110){
        stroke(colors[6]);
      }
      if(mouseY > 110 && mouseY < 135){
        stroke(colors[9]);
      }
    }
    if(mouseX > 35 && mouseX < 60){
      if( mouseY > 10 && mouseY < 35){
        stroke(colors[1]);
      }
      if(mouseY > 35 && mouseY < 50){
        stroke(colors[3]);
      }
      if(mouseY > 60 && mouseY < 85){
        stroke(colors[5]);
      }
      if(mouseY > 85 && mouseY < 110) {
        stroke(colors[7]);
      }
      if(mouseY > 110 && mouseY < 135){
        stroke(colors[8]);
      }
    }
    
  //Draw with pencil   
  if(mousePressed && mouseButton == LEFT && dPenc == true){
    line(mouseX,mouseY,pmouseX,pmouseY);
  }
 }
} 

//Choose which shape to draw
void mouseClicked(){
  if(mouseX > 1165 && mouseX < 1190){
    //Choose pencil
      if(mouseY > 10 && mouseY < 35){
        dPenc = true;     
      }else{
        dPenc = false;
      }
      //Choose bezier line
      if(mouseY > 35 && mouseY < 60){
        dLine = true;
      }else{
        dLine = false;
      }
      //Choose rectangle
      if(mouseY > 60 && mouseY < 85){
        dRect = true;     
      }else{
        dRect = false;
      }
      //Choose circle
      if(mouseY > 85 && mouseY < 110){
        dCirc = true;
      }else{
        dCirc = false;
      }
  }
}

//Setup for rectangle 1
void mousePressed(){
  if(dRect == true || dLine == true || dCirc == true && mouseButton == LEFT){
    a=mouseX;
    b=mouseY;
  }
}

//Setup for rectangle 2
void mouseReleased(){
  if(dRect == true && mouseButton == LEFT){
    c=mouseX-a;
    d=mouseY-b;
    if(keyPressed){
      if(key==CODED){
        if(keyCode==SHIFT){
          square(a,b,(c+d)/2);
        }
      }
    }else{
      rect(a,b,c,d);
    }
  }
  if(dLine == true && mouseButton == LEFT){
    line(a,b,pmouseX,pmouseY);
  }
  if(dCirc == true && mouseButton == LEFT){
    c=mouseX-a;
    d=mouseY-b;
    if(keyPressed){
      if(key==CODED){
        if(keyCode==SHIFT){
          circle(a,b,(c+d)/2);
        }
      }
    }else{
      ellipse(a,b,c,d);
    }
  }
}
1 Like

Hello @Andrej and welcome!

Regarding left/right mouse button:

I recently had that same problem. And read on the apple forum there is a particular generation of older Macbook pro that has an unresolved hardware issue with getting the left / right buttons to work properly.
If you determine your code is correct but button action is still not working you may want to do a google search to see if there are any known issues button-hardware-wise for your particular setup…
Good luck!
:nerd_face:

I am not using Macbook nor anything with Apple, so it shouldn’t really be a hardware issue, but thank you for taking your time and replying to this thread debxyz :smiley: .

My issue is more about give me some instructions or tips how to solve those two problems, I don’t need any code or finished solutions, just some tips :slight_smile: .

Hello @Andrej

In terms of tips on how to solve, I would break it down like this:

First, since your code – as it currently stands – has a lot of if-else statements, mousePressed, and keyPressed events, I would set up a simplified version of the program which contains only one instance of each.

For example, write your program to include only the drawing of a rectangle and the option to choose between 2 colors (instead of many colors and several shapes).

This way it’s easier to focus on how specific lines in the code are interacting (or not) with each other. And it will be easier for others to zero in on where a problem area lies. Once it’s worked out in the simplified version then implementing in the full code version will be much easier.

Also, the way I’m reading your mouseReleased() event below, it looks like you have keyPressed combined with the mouseReleased? I’m not sure about this. And I don’t want to mislead you. It’s something that I would look into to confirm its viability. Again, here I would set up a super simple program that uses that mouseRelease and keyPressed construct to see if that works as you expect.

Hopefully this helps!
:nerd_face:

1 Like