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 & HelpSyntax Questions › Using Keypresses with Images
Page Index Toggle Pages: 1
Using Keypresses with Images (Read 425 times)
Using Keypresses with Images
Jun 10th, 2008, 11:44am
 
Hey I'm still pretty new to processing and I've created some coding for one of my uni projects. The idea is that the keypresses place the facial features on top of the blank face. I've currently got 2 problems with it.
1. When i added the blank face as a background image the facial features now only stay on screen for the instance that i press the button instead of being layered over the top. I know it has something to do with loop and redraw but I can't seem to figure it out.
2. When i have the facial features working the nose and mouth layer on top of each other making previous keypressed versions visible underneath. I was trying to get it so each time a key is pressed the previous nose/mouth is erased and replaced by the new one?
Any help would be much appreciated!!!

Code:

PImage [] features = new PImage[21];
PImage bg;
int leye;
int reye;
int mouth;
int nose;

void setup(){
bg = loadImage("mattface2.png");
//background(0);
size(1224,1162);
//frameRate(1);
features[0] = loadImage("le1.png");
features[1] = loadImage("le2.png");
features[2] = loadImage("le3.png");
features[3] = loadImage("le4.png");
features[4] = loadImage("le5.png");
features[5] = loadImage("le6.png");
features[6] = loadImage("re1.png");
features[7] = loadImage("re2.png");
features[8] = loadImage("re3.png");
features[9] = loadImage("re4.png");
features[10] = loadImage("re5.png");
features[11] = loadImage("mouth1.png");
features[12] = loadImage("mouth2.png");
features[13] = loadImage("mouth3.png");
features[14] = loadImage("mouth4.png");
features[15] = loadImage("mouth5.png");
features[16] = loadImage("nose1.png");
features[17] = loadImage("nose2.png");
features[18] = loadImage("nose3.png");
features[19] = loadImage("nose4.png");
features[20] = loadImage("nose5.png");
//noLoop();
}


void draw(){
background(bg);
leye=int(random(5));
reye=int(random(5)+6);
mouth=int(random(5)+11);
nose=int(random(5)+16);

//int rand = 0;

}

void keyPressed(){
if (key == 'l' || key == 'L') {
image(features[leye],0,0, 1224,1162);
}
else if (key == 'r' || key == 'R') {
image(features[reye],0,0,1224,1162);
}
else if (key == 'm' || key == 'M') {
image(features[mouth],0,0,1224,1162);
}
else if (key == 'n' || key == 'N') {
image(features[nose],0,0,1224,1162);
}
else if (key == 's' || key == 'S') {
saveFrame("face-####.tif");
}
}


Re: Using Keypresses with Images
Reply #1 - Jun 10th, 2008, 3:55pm
 
Actually, in the draw loop, you want to :
- draw the blank face
- draw a mouth
- draw a nose
- draw the left eye
- draw the right eye

Code:
void draw() {
background(bg);
image(features[mouth],0,0, 1224,1162);
image(features[nose],0,0, 1224,1162);
image(features[leye],0,0, 1224,1162);
image(features[reye],0,0, 1224,1162);
}


Then, you want to change a feature when the user press a key :

Code:
void keyPressed() {
if (key == 'm' || key == 'M') mouth=int(random(5)+11);
else if (key == 'n' || key == 'N') nose=int(random(5)+16);
else if (key == 'l' || key == 'L') leye=int(random(5));
else if (key == 'r' || key == 'R') reye=int(random(5)+6);
}
Re: Using Keypresses with Images
Reply #2 - Jun 10th, 2008, 4:04pm
 
And by the way, I would use an array per feature type, in order to make the code easier to read :

Quote:
PImage bg;
PImage[] featLEye = new PImage[5];
PImage[] featREye = new PImage[5];
PImage[] featMouth = new PImage[5];
PImage[] featNose = new PImage[5];
int leye, reye, mouth, nose;

void setup(){
 bg = loadImage("mattface2.png");
 size(1224,1162);
 for (int i = 0; i < featLEye.length; i++) featLEye[i] = loadImage("le"+i+".png");
 for (int i = 0; i < featREye.length; i++) featREye[i] = loadImage("re"+i+".png");
 for (int i = 0; i < featMouth.length; i++) featMouth[i] = loadImage("mouth"+i+".png");
 for (int i = 0; i < featNose.length; i++) featNose[i] = loadImage("nose"+i+".png");
}

void draw(){
 background(bg);
 image(featMouth[mouth],0,0, 1224,1162);
 image(featNose[nose],0,0, 1224,1162);
 image(featLEye[leye],0,0, 1224,1162);
 image(featREye[reye],0,0, 1224,1162);
}

void keyPressed() {
 if (key == 'm' || key == 'M') mouth=int(random(5));
 else if (key == 'n' || key == 'N') nose=int(random(5));
 else if (key == 'l' || key == 'L') leye=int(random(5));
 else if (key == 'r' || key == 'R') reye=int(random(5));
 else if (key == 's' || key == 'S') saveFrame("face-####.tif");
}
Re: Using Keypresses with Images
Reply #3 - Jun 10th, 2008, 4:15pm
 
Thank you so much i was actually just about to reply to my own topic because i figured out my mistake was attempting this far too late last night.
The abbreviated version of it though is perfect i actually have 90 odd different versions of each feature cut up and was beginning to stress about having to write that out everytime Thanks so much!!
Page Index Toggle Pages: 1