We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › simple paint program
Page Index Toggle Pages: 1
simple paint program (Read 1230 times)
simple paint program
Dec 12th, 2006, 11:47am
 
I'm just getting started with working with processing and one of the first things I'm working on right now is a simple version paint program. Although I can't find good a function that keeps the value after I use the mousePressed() function. Using the void mousePressed() code made it so that the color wouldn't even stay longer then just the click.

current code:

//Wanna
//Paint Prog
//Dec 2006


int Yellow;
int Red;
int Green;
int Blue;
int Grey;
int LightGrey;


void setup()
{
 size(640,480);
 background(102);
 frameRate(60);

 Yellow = color(255,255,0);
 Red = color(255,0,0);
 Green = color(0,255,0);
 Blue = color(0,0,255);
 Grey = color(102);
 LightGrey = color(200);

}

//color buttons
void draw()
{
 stroke(5);

 fill(LightGrey);
 rect(10,10,190,70);

 fill(Yellow);
 rect(20,20,20,20);
 fill(Red);
 rect(50,20,20,20);
 fill(Green);
 rect(80,20,20,20);
 fill(Blue);
 rect(110,20,20,20);
 
 fill(Grey);
 rect(140,20,50,50);    //the eraser
 
 fill(Yellow);
 ellipse(30,60,20,20);
 fill(Red);
 ellipse(60,60,20,20);
 fill(Green);
 ellipse(90,60,20,20);
 fill(Blue);
 ellipse(120,60,20,20);
}

//selecting color
void mousePressed()
{
 if (mousePressed == (mouseX > 20) && (mouseY > 20) && (mouseX < 40) && (mouseY < 40))
 {
 noStroke();
 fill(Red);
 } else
 {
 noStroke();
 fill(102);
 }
 rect(mouseX-10,mouseY-10,20,20);
}



alternative ending to the code above:

//selecting color
 if (mousePressed == (mouseX > 20) && (mouseY > 20) && (mouseX < 40) && (mouseY < 40))
 {
 noStroke();
 fill(Red);
 } else
 {
 noStroke();
 fill(102);
 }
 rect(mouseX-10,mouseY-10,20,20);
}  


Any help appreciated,
wanna
Re: simple paint program
Reply #1 - Dec 13th, 2006, 2:45pm
 
A couple quick things.

The fill in mousePressed() will be overwritten by any fills that are called in draw (to set up the interface.)  Most likely you want to have a global variable to hold onto what the current color is.

Also, the "mousePressed ==" code in your if statement doesn't make any sense.  mousePressed is just a boolean variable which is true or false.


Code:

color currentColor;

void draw() {
// Your interface stuff
// etc.

// Now if the mouse is pressed, paint
if (mousePressed) {
fill(currentColor);
rect(mouseX,mouseY,20,20);
}
}

void mousePressed() {
if ((mouseX>20) && (mouseY>20) && (mouseX<40) && (mouseY<40))
{
currentColor = color(255,0,0);
}
// etc.
}
Re: simple paint program
Reply #2 - Dec 13th, 2006, 9:51pm
 
Thanks shiffman, because of that piece of code i was able to complete it.

For anyone who is interested, here is my (final) version;


//Wanna
//code problem solved by shiffman
//Paint Prog
//Dec 2006


int Yellow;
int Red;
int Green;
int Blue;
int Grey;
int LightGrey;
color currentColor;
boolean typeIsRect;


void setup()
{
 size(640,480);
 background(102);
 frameRate(60);

 Yellow = color(255,255,0);
 Red = color(255,0,0);
 Green = color(0,255,0);
 Blue = color(0,0,255);
 Grey = color(102);
 LightGrey = color(200);
 currentColor = color(102);
 typeIsRect = true;
 

}

//interface
void draw()
{
 stroke(5);
 smooth();

 fill(LightGrey);
 rect(10,10,190,70);

 fill(Yellow);
 rect(20,20,20,20);
 fill(Red);
 rect(50,20,20,20);
 fill(Green);
 rect(80,20,20,20);
 fill(Blue);
 rect(110,20,20,20);
 
 fill(Grey);
 rect(140,20,50,50);    //the eraser
 
 fill(Yellow);
 ellipse(30,60,20,20);
 fill(Red);
 ellipse(60,60,20,20);
 fill(Green);
 ellipse(90,60,20,20);
 fill(Blue);
 ellipse(120,60,20,20);

 // Now if the mouse is pressed, paint
 if (mousePressed)
 {
   noStroke();
   fill(currentColor);
   if (typeIsRect)
   {
     if ((mouseX>140) && (mouseY>20) && (mouseX<190) && (mouseY<70))
     {
       rect(mouseX-25,mouseY-25,50,50);
     }
     else
     {
       rect(mouseX-10,mouseY-10,20,20);
     }
   }
   else
   {
     ellipse(mouseX,mouseY,20,20);
   }
 }
}

void mousePressed()
{
//ractangles
if ((mouseX>20) && (mouseY>20) && (mouseX<40) && (mouseY<40))
 {
   typeIsRect = true;
   currentColor = color(Yellow);
 }
 if ((mouseX>50) && (mouseY>20) && (mouseX<70) && (mouseY<40))
 {
   typeIsRect = true;
   currentColor = color(Red);
 }
 if ((mouseX>80) && (mouseY>20) && (mouseX<100) && (mouseY<40))
 {
   typeIsRect = true;
   currentColor = color(Green);
 }
 if ((mouseX>110) && (mouseY>20) && (mouseX<130) && (mouseY<40))
 {
   typeIsRect = true;
   currentColor = color(Blue);
 }
 if ((mouseX>140) && (mouseY>20) && (mouseX<190) && (mouseY<70))
 {
   typeIsRect = true;
   currentColor = color(Grey);
 }
 
//elipses
 if ((mouseX>20) && (mouseY>50) && (mouseX<40) && (mouseY<70))
 {
   typeIsRect = false;
   currentColor = color(Yellow);
 }
 if ((mouseX>50) && (mouseY>50) && (mouseX<70) && (mouseY<70))
 {
   typeIsRect = false;
   currentColor = color(Red);
 }
 if ((mouseX>80) && (mouseY>50) && (mouseX<100) && (mouseY<70))
 {
   typeIsRect = false;
   currentColor = color(Green);
 }
 if ((mouseX>110) && (mouseY>50) && (mouseX<130) && (mouseY<70))
 {
   typeIsRect = false;
   currentColor = color(Blue);
 }
}
Page Index Toggle Pages: 1