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);
}
}
}