Paint application

Im trying to make a paint application. when i click on the shape icons the shape of the brush dosent change. when i click on the save icons they dont work either. im not sure what im missing in my code to get them to work.

boolean glide;
int white;
int red;
int green;
int blue;
int yellow;
int purple;
int brown;
int grey;
int black;
int current;
PImage img;
PImage img1;
PImage png;
void setup()
{ 
  background(255);
  size(1500,700);
  white=color(255);
  red=color(255,0,0);
  green=color(0,255,0);
  blue=color(0,0,255);
  yellow=color(255,255,0);
  purple=color(255,0,255);
  brown=color(139,42,42);
  grey=color(139,139,139);
  black=color(0);
  current=color(255);
  glide=true;
  img = loadImage("eraser.png");
  img1 = loadImage("jpg.JPG");
  png = loadImage("png.JPG");
}

void draw()
{
  stroke(10);
  smooth();
  
  rectMode(CORNER);
  fill(109);
rect(0,0,500,700);
strokeWeight(5);
fill(255);
rect(50,50,100,50);
line(60,60,140, 90);
fill(255);
rect(200,50,100,50);
rect(210,60,80,30 );
fill(255);
rect(350,50,100,50);
ellipse(400,75,50,30 );

fill(255);
rect(50,150,100,50);
image(img,60,160,80,30);
fill(255);
rect(200,150,100,50);
image(img1,200,150,100,50);
fill(255);
rect(350,150,100,50);
image(png,350,150,100,50);


fill(white);
rect(50,250,100,50);
fill(red);
rect(200,250,100,50);
fill(green);
rect(350,250,100,50);

fill(blue);
rect(50,350,100,50);
fill(yellow);
rect(200,350,100,50);
fill(purple);
rect(350,350,100,50);

fill(brown);
rect(50,450,100,50);
fill(grey);
rect(200,450,100,50);
fill(black);
rect(350,450,100,50);

if (mousePressed)
{
  noStroke();
 fill(current);
if (glide)
{if((mouseX>50)&&(mouseY>50) && (mouseX<150)&&(mouseY<100))
  { glide=true;
line(mouseX-15,mouseY-15,mouseX,mouseY);}

 else if((mouseX>200)&&(mouseY>50) && (mouseX<300)&&(mouseY<100))
  { glide=true;
  rect(mouseX-25,mouseY-25,50,50);
}

 else if((mouseX>350)&&(mouseY>50) && (mouseX<450)&&(mouseY<100))
  { glide=true;
  ellipse(mouseX,mouseY,50,50);
}
  
else if(mousePressed==true&&(mouseX>200)&&(mouseY>150) && (mouseX<300)&&(mouseY<200))
  { glide=true;
saveFrame("save-###.jpg");}

 else if(mousePressed==true&&(mouseX>350)&&(mouseY>150) && (mouseX<450)&&(mouseY<200))
  { glide=true;
saveFrame("save-###.png");}
}
}
}

void mousePressed()
{
  
  

  
  
  
  if((mouseX>50)&&(mouseY>150) && (mouseX<150)&&(mouseY<200))
  { glide=true;
current=color(white);
rect(mouseX-50,mouseY-50,50,50);}
  
 if((mouseX>50)&&(mouseY>250) && (mouseX<150)&&(mouseY<300))
  { glide=true;
current=color(white);}

  if((mouseX>200)&&(mouseY>250) && (mouseX<300)&&(mouseY<300))
  { glide=true;
current=color(red);}

if((mouseX>350)&&(mouseY>250) && (mouseX<450)&&(mouseY<300))
  { glide=true;
current=color(green);}

if((mouseX>50)&&(mouseY>350) && (mouseX<150)&&(mouseY<400))
  { glide=true;
current=color(blue);}

  if((mouseX>200)&&(mouseY>350) && (mouseX<300)&&(mouseY<400))
  { glide=true;
current=color(yellow);}

if((mouseX>350)&&(mouseY>350) && (mouseX<450)&&(mouseY<400))
  { glide=true;
current=color(purple);}

if((mouseX>50)&&(mouseY>450) && (mouseX<150)&&(mouseY<500))
  { glide=true;
current=color(brown);}

  if((mouseX>200)&&(mouseY>450) && (mouseX<300)&&(mouseY<500))
  { glide=true;
current=color(grey);}

if((mouseX>350)&&(mouseY>450) && (mouseX<450)&&(mouseY<500))
  { glide=true;
current=color(black);}


  
}
1 Like

please format your code posting by pasting it into the

</> code button

of the editor header menu ( context name: Preformatted text )
it looks like
```
type or paste code here
```

also can use the ``` manually above and below your code.

thank you.


thanks for the project description and the code,
but please tell us also about the problem you have?

I cant get my shape and save buttons to work in my program.

1 Like

To start with, you must define your colors as color not as int.

1 Like

my colors are working its just the shape and save buttons

Interesting, normally you just use integers with grey colors. But it really doesn’t give an error.

I can’t test it. saveFrame() does not work on android(only have this). I have to find a workaround for this.

color is in it‘s essence just an int. RGB is from -16milion to -1 while RGBA (with alpha) goes from -2B to 2B. See here for further information.

Hehe, here is where?

Sorry, took a while to find that :sweat_smile:

1 Like

Then why the need of data type color, if you can use integer? .Can you give me an example when datatype “color” is strictly necessary.

No datatype except the primitives (and not even all of them) are strictly necessary, but it‘s veeery convinient. For example, it‘s way easier to write String a = “Hello”; than to use char[] or byte[].

The Same goes for color. It‘s just an int, but it makes it a lot easier to work with if we call it color, simple to show what it represents.

There are also some other reasons, like that it‘s easier to understand for beginners, but in short, they aren‘t necessary, but they are useful (like everything other than direct programming in binary…).

Here‘s also some more Info about the color datatype.

1 Like

Still do not see char String and byte as interchangeable, otherwise you wouldn’t need convertion functions. I have to think about this.
In the mean time I have a workaround for saveFrame()
I have to use a boolean in draw() because noLoop() also doesn’t work in android.
Below the code for the modified saveFrame function (example code in the doc’s).

import android.os.Environment;

int x = 0; 
boolean no_loop = true;
String my_folder = "my_frames"; // Must be an existing folder one level above absolute path
String data_folder_path = new String(Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+my_folder+"/");
PImage pi;

void setup() {
  size(100, 100);
}

void draw() { 
  if (no_loop) {
    background(204); 
    if (x < 100) { 
      line(x, 0, x, 100); 
      x = x + 1;
    } else { 
      no_loop = false;
    } // Saves each frame as line-000001.png, line-000002.png, etc. 
    saveFrame("line-"); // numbers(hashtags) will be included in saveFrame()
  }
}

void saveFrame(String s) {
  int maxHashes=6; 
  int myInt = x; 
  String fs = String.format("%1$" + maxHashes + "s", myInt).replace(' ', '0');
  pi = get(0, 0, width, height);
  pi.save(data_folder_path+s+fs+".png");
}  

Maybe you can explain what you want to do, because you are only drawing in the “icon” boxes.

Although listed under primitives in the reference, the keyword color is “syntactic sugar”, not a real datatype separate from int. color / Reference / Processing.org In fact, color is a keyword that is synonymous with int, and when Processing is transpiled into Java the word is replaced with int. It was added to the language to help learners, but it isn’t an object or a discrete primitive. So:

  • Any place you write color you can write int instead.
  • However, the method color() does something very different from int()!

By contrast, p5.js has a very different implementation of Color –

…but high level API methods interact with it in more or less the same way, even though the ways that colors are stored and manipulated internally are very different.

3 Likes