#### Howdy, Stranger!

We are about to switch to a new forum software. Until then we have removed the registration on this forum.

# Need help with using int and mousePressed

edited February 2017

Hey everybody I just started taking a intro to programming class and my teacher has made us create moving characters in processing. Now he wants us to make the character leave a marker behind everythin i click the mouse. He wants it to be able to create a new marker (ellipse) with every click

I am using void mousePressed but idk where to go from there. He says i need to capture the x and y coordinates and asign them a value with the int variable and then write code into the void mousePressed section... can any please help me do this.

• Btw i mean he wants it to add a new marker with every click resulting in multiple.markers(ellipses) in the window

• edited February 2017
``````int capX=250;
int capY=250;

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

void draw(){
//STEWIE=FAMILYGUY CHARACTER
background(255);

//Legs
fill(100,149,237);
rectMode(CENTER);
rect(mouseX-35,mouseY+249,70,80);
rect(mouseX+35,mouseY+249,70,80);

//body
fill(255,255,0);
rectMode(CENTER);
rect(mouseX-0,mouseY+149,140,130,30,30,5,5);

//overall
fill(255,0,0);
rectMode(CENTER);
rect(mouseX-0,mouseY+229,140,100);
rect(mouseX-40,mouseY+138,25,110,2,2,10,10);
rect(mouseX+40,mouseY+138,25,110,2,2,10,10);
line(mouseX-0,mouseY+229,mouseX-0,mouseY+289);
fill(255,255,0);
ellipse(mouseX-40,mouseY+184,15,15);
ellipse(mouseX+40,mouseY+184,15,15);

//arms
fill(255,255,0);

fill(250,235,215);
ellipse(mouseX, mouseY,305,175);

//hair
line(mouseX-70,mouseY-83,mouseX-55,mouseY-75);//1
line(mouseX-35,mouseY-91,mouseX-25,mouseY-77);//2
line(mouseX-0,mouseY-93,mouseX-0,mouseY-78);//3
line(mouseX+70,mouseY-83,mouseX+55,mouseY-75);//4
line(mouseX+35,mouseY-91,mouseX+25,mouseY-76);//5
line(mouseX-100,mouseY-71,mouseX-90,mouseY-68);//6
line(mouseX-120,mouseY-60,mouseX-105,mouseY-54);//7
line(mouseX+100,mouseY-71,mouseX+90,mouseY-68);//8
line(mouseX+120,mouseY-60,mouseX+105,mouseY-54);//9

//lefteye
fill(255,255,255);
ellipse(mouseX-70,mouseY-21,60,60);
//righteye
ellipse(mouseX+70,mouseY-21,60,60);

//leftpupil
fill(0,0,0);
ellipse(mouseX-70,mouseY-21,10,10);
//rightpupil
ellipse(mouseX+70,mouseY-21,10,10);

//lefteyelid
fill(250,235,215);
arc(mouseX-70,mouseY-21,60,60,PI,TWO_PI,CLOSE);
//righteyelid
arc(mouseX+70,mouseY-21,60,60,PI,TWO_PI,CLOSE);

//lefteyebrow
line(mouseX-85,mouseY-59,mouseX-40,mouseY-51);
//righteyebrow
line(mouseX+85,mouseY-59,mouseX+40,mouseY-51);

//nose
beginShape();
vertex(mouseX-0,mouseY-11);
vertex(mouseX+15,mouseY+7);
vertex(mouseX+6,mouseY+14);
endShape();

//upperlip
beginShape();
vertex(mouseX+12,mouseY+29);
vertex(mouseX+20,mouseY+34);
vertex(mouseX-10,mouseY+37);
endShape();

//lowerlip
beginShape();
vertex(mouseX+8,mouseY+35);
vertex(mouseX+6,mouseY+41);
endShape();

//leftear
curve(mouseX-30,mouseY+49,mouseX-148,mouseY-21,mouseX-151,mouseY+14,mouseX-20,mouseY-21);

//rightear
curve(mouseX+30,mouseY-0,mouseX+148,mouseY-21,mouseX+151,mouseY+14,mouseX+20,mouseY-21);

//lefthand
ellipse(mouseX-5,mouseY+175,40,40);
//righthand
ellipse(mouseX+5,mouseY+175,40,40);

//mark
ellipse(capX,capY,50,50);

}

void mousePressed() {
if (capX==250);
capX = mouseX;
if (capY==250);
capY = mouseY;

}
``````

this is the code i have so far. i have to use processing 2.2.1

please edit post, highlight code, press ctrl-o to format the above code

• thanks for that... makes it so much easier!!! but yeah i just need the mouse click to add a new ellipse everytime bcuz right now it just clears the old one and reappears whereever i click

• im also using processing 2.2.1

• edited February 2017 Answer ✓

When you click and want the stewie appear where you click use capX,capY with translate() fot stewie

Make stewie a own function so your draw() gets lean

Anyway. To have stewie plus multiple green dots you are in trouble. Your dots disappear because you use background but you need background to let stewie disappear. Dilemma.

To solve it, stick with background and store all capX, capY in an ArrayList of type PVector - see reference for both.

In draw display all dots in the Arralist and stewie.

Before setup ()

``````         ArrayList <PVector> list = new ArrayList ();
``````

in mousePressed ()

PVector pv = new PVector(capX,capY);

In draw() :

.........

• Thanks, maybe it seems more complicated the way i made it seem but its just a matter of making a marker(circle) appear everytime when i click, stewie doesnt have to do anythin other than what he is already doing... the problem is the professor wants us to use make it work with just the void mousePressed and capturing x and y then assigning a value to the int..... he didnt really explain much else

• Well remove background and you'll see what I mean

The green circles remain but stewie is all over the place. Bad.

• Why if in 114 and 116?

Delete those lines

• Do you want all circles to remain visible once drawn? I assumed that

mousePressed and capturing x and y then assigning a value to the int.....

You are assining mouseX and mouseY to it

• yea 114 and 116 just my attempt at making it work but it just draws the circle in a different spot and erases the previous one, but yea i see what you mean what happens when u remove the background, and yeah all the circles are soppose to remain visible... so is it technically not possible without having stewie be all over the place

• i guess i am just going to leave it with stewie staying all over the place, its the only way to cirlces kind of stayed. is there a way to clear it all with keyPressed?

• As I said

You need background to have stewie move nicely

And you need a full ArrayList as I've shown above for the circles

You need background to have stewie move nicely

And you need a full ArrayList as I've shown above for the circles

clear it all with keyPressed?

not good enough.

See my approach above.

You can clear of couse with background in keyPressed ()

• thank you for all the help! im trying to do the ArrayList thing you told me but im really new to this so im trying to understand how properly do it by looking at the references.

• ``````ArrayList<PVector> = new ArrayList

int capX=250;
int capY=250;
void setup(){
size(500,500);

}
void draw(){
//STEWIE=FAMILYGUY CHARACTER
background (255);

//Legs
fill(100,149,237);
rectMode(CENTER);
rect(mouseX-35,mouseY+249,70,80);
rect(mouseX+35,mouseY+249,70,80);

//body
fill(255,255,0);
rectMode(CENTER);
rect(mouseX-0,mouseY+149,140,130,30,30,5,5);

//overall
fill(255,0,0);
rectMode(CENTER);
rect(mouseX-0,mouseY+229,140,100);
rect(mouseX-40,mouseY+138,25,110,2,2,10,10);
rect(mouseX+40,mouseY+138,25,110,2,2,10,10);
line(mouseX-0,mouseY+229,mouseX-0,mouseY+289);
fill(255,255,0);
ellipse(mouseX-40,mouseY+184,15,15);
ellipse(mouseX+40,mouseY+184,15,15);

//arms
fill(255,255,0);

fill(250,235,215);
ellipse(mouseX, mouseY,305,175);

//hair
line(mouseX-70,mouseY-83,mouseX-55,mouseY-75);//1
line(mouseX-35,mouseY-91,mouseX-25,mouseY-77);//2
line(mouseX-0,mouseY-93,mouseX-0,mouseY-78);//3
line(mouseX+70,mouseY-83,mouseX+55,mouseY-75);//4
line(mouseX+35,mouseY-91,mouseX+25,mouseY-76);//5
line(mouseX-100,mouseY-71,mouseX-90,mouseY-68);//6
line(mouseX-120,mouseY-60,mouseX-105,mouseY-54);//7
line(mouseX+100,mouseY-71,mouseX+90,mouseY-68);//8
line(mouseX+120,mouseY-60,mouseX+105,mouseY-54);//9

//lefteye
fill(255,255,255);
ellipse(mouseX-70,mouseY-21,60,60);
//righteye
ellipse(mouseX+70,mouseY-21,60,60);

//leftpupil
fill(0,0,0);
ellipse(mouseX-70,mouseY-21,10,10);
//rightpupil
ellipse(mouseX+70,mouseY-21,10,10);

//lefteyelid
fill(250,235,215);
arc(mouseX-70,mouseY-21,60,60,PI,TWO_PI,CLOSE);
//righteyelid
arc(mouseX+70,mouseY-21,60,60,PI,TWO_PI,CLOSE);

//lefteyebrow
line(mouseX-85,mouseY-59,mouseX-40,mouseY-51);
//righteyebrow
line(mouseX+85,mouseY-59,mouseX+40,mouseY-51);

//nose
beginShape();
vertex(mouseX-0,mouseY-11);
vertex(mouseX+15,mouseY+7);
vertex(mouseX+6,mouseY+14);
endShape();

//upperlip
beginShape();
vertex(mouseX+12,mouseY+29);
vertex(mouseX+20,mouseY+34);
vertex(mouseX-10,mouseY+37);
endShape();

//lowerlip
beginShape();
vertex(mouseX+8,mouseY+35);
vertex(mouseX+6,mouseY+41);
endShape();

//leftear
curve(mouseX-30,mouseY+49,mouseX-148,mouseY-21,mouseX-151,mouseY+14,mouseX-20,mouseY-21);

//rightear
curve(mouseX+30,mouseY-0,mouseX+148,mouseY-21,mouseX+151,mouseY+14,mouseX+20,mouseY-21);

//lefthand
ellipse(mouseX-5,mouseY+175,40,40);
//righthand
ellipse(mouseX+5,mouseY+175,40,40);

//mark
fill(0);
ellipse(capX,capY,50,50);

}

void mousePressed() {
PVector pv = new PVector(capX,capY);
}
void keyPressed() {
background(255);
}
``````

dont know where to go from here..

• Look at ArrayList reference to see how to put out the list on screen in draw()

After line 120 insert a new line where it says: capX =mouseX; capY = mouseY;

• Trust me ive been reading references since last night but i just dont understand them...so im having a hard time applying it to my code

• In draw()

for (PVector pv : list) {

ellipse (pv.x,pv.y,6,6);

}

• After line 120 insert a new line where it says: capX =mouseX; capY = mouseY;

Kill line 115

• ``````ArrayList<PVector> = new ArrayList();

int capX=250;
int capY=250;
void setup(){
size(500,500);

}
void draw(){
//STEWIE=FAMILYGUY CHARACTER
background (255);

//Legs
fill(100,149,237);
rectMode(CENTER);
rect(mouseX-35,mouseY+249,70,80);
rect(mouseX+35,mouseY+249,70,80);

//body
fill(255,255,0);
rectMode(CENTER);
rect(mouseX-0,mouseY+149,140,130,30,30,5,5);

//overall
fill(255,0,0);
rectMode(CENTER);
rect(mouseX-0,mouseY+229,140,100);
rect(mouseX-40,mouseY+138,25,110,2,2,10,10);
rect(mouseX+40,mouseY+138,25,110,2,2,10,10);
line(mouseX-0,mouseY+229,mouseX-0,mouseY+289);
fill(255,255,0);
ellipse(mouseX-40,mouseY+184,15,15);
ellipse(mouseX+40,mouseY+184,15,15);

//arms
fill(255,255,0);

fill(250,235,215);
ellipse(mouseX, mouseY,305,175);

//hair
line(mouseX-70,mouseY-83,mouseX-55,mouseY-75);//1
line(mouseX-35,mouseY-91,mouseX-25,mouseY-77);//2
line(mouseX-0,mouseY-93,mouseX-0,mouseY-78);//3
line(mouseX+70,mouseY-83,mouseX+55,mouseY-75);//4
line(mouseX+35,mouseY-91,mouseX+25,mouseY-76);//5
line(mouseX-100,mouseY-71,mouseX-90,mouseY-68);//6
line(mouseX-120,mouseY-60,mouseX-105,mouseY-54);//7
line(mouseX+100,mouseY-71,mouseX+90,mouseY-68);//8
line(mouseX+120,mouseY-60,mouseX+105,mouseY-54);//9

//lefteye
fill(255,255,255);
ellipse(mouseX-70,mouseY-21,60,60);
//righteye
ellipse(mouseX+70,mouseY-21,60,60);

//leftpupil
fill(0,0,0);
ellipse(mouseX-70,mouseY-21,10,10);
//rightpupil
ellipse(mouseX+70,mouseY-21,10,10);

//lefteyelid
fill(250,235,215);
arc(mouseX-70,mouseY-21,60,60,PI,TWO_PI,CLOSE);
//righteyelid
arc(mouseX+70,mouseY-21,60,60,PI,TWO_PI,CLOSE);

//lefteyebrow
line(mouseX-85,mouseY-59,mouseX-40,mouseY-51);
//righteyebrow
line(mouseX+85,mouseY-59,mouseX+40,mouseY-51);

//nose
beginShape();
vertex(mouseX-0,mouseY-11);
vertex(mouseX+15,mouseY+7);
vertex(mouseX+6,mouseY+14);
endShape();

//upperlip
beginShape();
vertex(mouseX+12,mouseY+29);
vertex(mouseX+20,mouseY+34);
vertex(mouseX-10,mouseY+37);
endShape();

//lowerlip
beginShape();
vertex(mouseX+8,mouseY+35);
vertex(mouseX+6,mouseY+41);
endShape();

//leftear
curve(mouseX-30,mouseY+49,mouseX-148,mouseY-21,mouseX-151,mouseY+14,mouseX-20,mouseY-21);

//rightear
curve(mouseX+30,mouseY-0,mouseX+148,mouseY-21,mouseX+151,mouseY+14,mouseX+20,mouseY-21);

//lefthand
ellipse(mouseX-5,mouseY+175,40,40);
//righthand
ellipse(mouseX+5,mouseY+175,40,40);

for (PVector pv : list) {
ellipse(pv.x,pv.y,6,6);
}

}

void mousePressed() {
capX=mouseX; capY;mouseY;
PVector pv = new PVector(capX,capY);
}
void keyPressed() {
background(255);
}
``````
• error says unexpected token and highlights ArrayList = new ArrayList(); maybe bcuz im using 2.2.1

• edited February 2017 Answer ✓

Try

``````        ArrayList<PVector> list = new ArrayList();
``````

Word list!!!!

• Sorry, the forum initially killed the PVector in that line