PDA

View Full Version : Another Processing 0118? (MouseRoutine)


wardd13
2006.10.13, 03:33 AM
Guys,

Just to show you I've used your advice I put the code I've developed so far below.

Now I'm looking for a mouse routine I can't quite get a handle on.

In METAL I had a procedure named GetAClick that I called when I wanted the program to wait for a mouse click and then return with the x and y coords.

Can you help me out with this?

Thanks for your time,
d ward

PImage MandMs[];
int Box[];
int Bag[];
int Cx[];
int Cy[];
int Guess[];
int Actual[];
int Diffs[];
boolean NewSet;
int SetsPicked;

void setup()
{
size (700,500);
background(0);
MandMs = new PImage[7];
MandMs[1] = loadImage("Red.jpg");
MandMs[2] = loadImage("Green.jpg");
MandMs[3] = loadImage("Blue.jpg");
MandMs[4] = loadImage("Brown.jpg");
MandMs[5] = loadImage("Yellow.jpg");
MandMs[6] = loadImage("Check.jpg");
Box = new int[101];
Actual = new int[6];
Bag = new int[21];
Cx = new int[21];
Cy = new int[21];
MainProgram();
}

void MainProgram()
{
SetButtons();
MakeBox();
MakeBag();
PickCoords();
ShowBag();
}

void draw(){
}

void ShowBag()
{
for (int i=1; i<=20; i++)
{
image(MandMs[Bag[i]], Cx[i]*50, Cy[i]*50);
}
}

void PickCoords()
{
int SetsPicked;
int Newx;
int Newy;
boolean NewSet;
int CxTemp, CyTemp;
for (int i=1; i<=20; i++)
{
Cx[i]=0;
Cy[i]=0;
}
SetsPicked = 0;
while (SetsPicked <= 19)
{
CxTemp = round(random(11))+1;
CyTemp = round(random(5))+1;
NewSet = true;
for (int i=1; i<=SetsPicked; i++)
{
if (CxTemp == Cx[i] && CyTemp == Cy[i])
{
NewSet=false;
}
}
if(NewSet==true)
{
SetsPicked = SetsPicked + 1;
Cx[SetsPicked] = CxTemp;
Cy[SetsPicked] = CyTemp;
}

}
for (int i=1; i<=20; i++)
{
print (Cx[i]+" "+Cy[i]+"---");
}
}

void MakeBag()
{
for (int i=1; i<=20; i++)
{
Bag[i]=0;
}
for (int i=1; i<=20; i++)
{
int n = round(random(100));
Bag[i]=Box[n];
}
}

void MakeBox()
{
for (int i=0; i<100; i++)
{
Box[i]=0;
}
int a,b,c,d,e;
a=0;
b=0;
c=0;
d=0;
e=0;
while (a+b+c+d+e!=100)
{
a = round(random(1,50));
b = round(random(1,50));
c = round(random(1,50));
d = round(random(1,50));
e = round(random(1,50));
}
Actual[1]=a;
Actual[2]=b;
Actual[3]=c;
Actual[4]=d;
Actual[5]=e;
for (int i=1; i<=a; i++)
{
Box[i] = 1;
}
for (int i=a+1; i<=a+b; i++)
{
Box[i] = 2;
}
for (int i=a+b+1; i<=a+b+c; i++)
{
Box[i] = 3;
}
for (int i=a+b+c+1; i<=a+b+c+d; i++)
{
Box[i] = 4;
}
for (int i=a+b+c+d+1; i<=a+b+c+d+e; i++)
{
Box[i] = 5;
}

}

void SetButtons()
{
image(MandMs[1],200,450);
image(MandMs[2],250,450);
image(MandMs[3],300,450);
image(MandMs[4],350,450);
image(MandMs[5],400,450);
image(MandMs[6],450,450);
}


/*void DrawMs()
{
for(int i=0; i<800; i=i+50) {
for(int j=0; j<600; j=j+50){
int m = round(random(4))+1;
image(MandMs[m], i, j);
}
}
}
*/

ThemsAllTook
2006.10.13, 10:23 AM
From a C point of view (I don't know the API you're using, but most of them work similarly), the way to do this is to set a state variable somewhere in your program that indicates you're waiting for a click to occur. You would check the value of this variable in your main loop, and if it's currently set, don't do further processing for that cycle. In your mouseDown handler, if that variable is set, you'd unset it, and either do the necessary processing right there, or save the click coordinates for later processing.

Jones
2006.10.13, 10:51 AM
That's not QuickCG is it?

diordna
2006.10.13, 10:52 AM
It's very simple.

Like draw(), Processing has functions which will be called when certain things happen. draw() is called when the program is ready to draw. There are two functions in this vein that deal with the mouse: mousePressed() and mouseReleased(); They are called by Processing when the mouse is pressed and released, respectively. The following program should wait until the mouse is pressed and released and then quit. I've been having some trouble with Processing on this particulary crappy machine, but it shouldn't be my code.

boolean mouseIsDown = false;

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

void draw(){
background(255);
waitForClick();
exit();
}

void waitForClick(){
while (!mouseIsDown){}
}

void mousePressed(){
mouseIsDown = true;
}

void mouseReleased(){
mouseIsDown = false;
}

wardd13
2006.10.14, 01:41 PM
diordna,

I played with your code quite a bit as well as some other similar methods and keep having the same problem.

If there is a while loop outside draw() the program locks up.

So, I decided to quite fighting draw() and work with it instead.

The code below seems to work and allows me to call procedures based on the coords of a mouse click.

Note: Is Calling background() the proper way to clear the screen?

Thanks for all your help,
d ward

int x,y;
boolean Mode1,Mode2,ModeQ;

void setup(){
size(700,500);
background(250);
ModeQ=false;
Proc1();
GetMode();
}

void GetMode(){
Mode1=false;
Mode2=false;
ModeQ=false;
if(mousePressed){
if(mouseX<350){
Mode1=true;
}else{
Mode2=true;
}
if(mouseY>450){
ModeQ=true;
}
}
}

void draw(){
if(Mode1==true){
background(250);
Proc1();
}
if(Mode2==true){
background(250);
Proc2();
}
if(ModeQ==true){
exit();
}
GetMode();
}

void Proc1(){
for(int i=0; i<=10; i++){
line((round(random(700))),(round(random(500))),(ro und(random(700))),(round(random(500))));
}
}
void Proc2(){
for(int i=0; i<=10; i++){
ellipse((round(random(700))),(round(random(500))), (round(random(50))),(round(random(50))));
}
}

diordna
2006.10.14, 04:21 PM
Yes.

That seems like a very odd problem, I'll post on the Processing forums about it.

Also, please put your code in '[CODE]' tags.

wardd13
2006.10.15, 03:57 PM
diordna,

Well I finally had some success.

Here is a link to an Applet for use in a lesson on using sampling to find the percentages of different colors of M&Ms in a batch.

http://www.tri-county.k12.ia.us/MandMsApplet/MandMs.html

I'm not 100% sure I've figured out all the glitches in getting it set up properly on my web server, but it seems to work for now.

I can't thank you enough for turning me on to Processing0118 and for all of your help.
Being able to create Mac, Windows and Applet programs will be of great use to me and hopefully, a great value to my students.

By the way, now that I think of it, in my last post I should have said that any while loop that used mousePressed etc. caused the program to lock up, not just any while loop.

Thanks again,
Dennis Ward
Tri-County School
Thornburg, IA