# Necesito ayuda con processing

**URL:** https://discourse.processing.org/t/necesito-ayuda-con-processing/39476
**Category:** Español
**Tags:** homework
**Created:** [October 28, 2022, 4:10pm UTC](https://discourse.processing.org/t/necesito-ayuda-con-processing/39476 "2022-10-28T16:10:20Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Pr1M3](https://avatars.discourse-cdn.com/v4/letter/p/41988e/32.png) [@Pr1M3](https://discourse.processing.org/u/Pr1M3)
#### Post date: [October 28, 2022, 4:10pm UTC](https://discourse.processing.org/t/necesito-ayuda-con-processing/39476/1 "2022-10-28T16:10:20Z")

</div>

En la pantalla se dibujan 5 botones, los 2 primeros cuando le doy click tienen que generar un cuadrado(primer boton) y un circulo(segundo boton) en pantalla pero que no se borren, pero no puedo hacer que se mantengan en pantalla porq siempre desaparecen, los demas 3 botones sirven para achicar el cuadrado, aumentar el circulo y el ultimo para borrar todo en pantalla… Alguien me puede orientar en como puedo hacer que funcione?

int cir=0;  
boolean cuad=false;  
int posy=(int)random(0,500);  
int posx=(int)random(0,500);  
int tam=20;  
int tamc=50;

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

void draw(){  
background(255);  
if(cuad){  
for(int i=0;i\<10;i++){  
cuadrados();  
cuad=false;  
}  
}

if(cir==1){  
circulos();  
}  
botones();  
}

void botones(){  
superRect(#F5F5F5,100,200,50,50);  
superRect(#F5F5F5,160,200,50,50);  
superRect(#F5F5F5,220,200,50,50);  
superRect(#F5F5F5,280,200,50,50);  
superRect(#F5F5F5,340,200,50,50);  
}

void circulos(){  
fill(255);  
ellipse(posx,posy,tamc,tamc);  
}

void cuadrados(){  
superRect(#F5F5F5,(int)random(0,500),(int)random(0,500),tam,tam);  
}

void mousePressed(){  
//boton cuadrados  
if( clickboton(100,200,50,50,mouseX,mouseY) ){  
cuad=true;  
}  
//circulos  
if( clickboton(160,200,50,50,mouseX,mouseY) ){  
cir=1;  
}  
//+1  
if( clickboton(220,200,50,50,mouseX,mouseY) ){  
tam++;  
}  
//-1  
if( clickboton(280,200,50,50,mouseX,mouseY) ){  
tamc–;  
}  
//reset  
if( clickboton(340,200,50,50,mouseX,mouseY) ){  
cuad=false;  
cir=0;  
tam=20;  
tamc=50;  
}  
}

void superRect(color c, int x, int y, int w, int h){  
fill(c);  
rect(x, y, w, h);  
}

boolean clickboton(int x, int y, int w, int h, int mx, int my){

if( (mx \> x && mx \< (x+w)) && (my \> y && my \< (y+h)) ){  
return true;  
}else{  
return false;  
}

}

---

<div class="post-metadata">

### Author: ![debxyz](https://avatars.discourse-cdn.com/v4/letter/d/58956e/32.png) [@debxyz](https://discourse.processing.org/u/debxyz)
#### Post date: [October 28, 2022, 5:24pm UTC](https://discourse.processing.org/t/necesito-ayuda-con-processing/39476/2 "2022-10-28T17:24:35Z")

</div>

Hello!  
I used google translate so I hope it gave a good translation to your question. 😀

> [@Pr1M3](#):
>
> void draw(){  
> background(255);

I think you want to move your **background (255);** function into setup then your buttons will not disappear.  
🤓

---

<div class="post-metadata">

### Author: ![Pr1M3](https://avatars.discourse-cdn.com/v4/letter/p/41988e/32.png) [@Pr1M3](https://discourse.processing.org/u/Pr1M3)
#### Post date: [October 28, 2022, 7:31pm UTC](https://discourse.processing.org/t/necesito-ayuda-con-processing/39476/3 "2022-10-28T19:31:25Z")

</div>

thanks!! but If I move the background for the setup my circles and squares will not scale well, they will leave a trace of their previous size on the image.

---

<div class="post-metadata">

### Author: ![debxyz](https://avatars.discourse-cdn.com/v4/letter/d/58956e/32.png) [@debxyz](https://discourse.processing.org/u/debxyz)
#### Post date: [October 28, 2022, 11:28pm UTC](https://discourse.processing.org/t/necesito-ayuda-con-processing/39476/4 "2022-10-28T23:28:52Z")

</div>

Ok, I see the problem now.

Please see the revised placement for where to put your random number generator.

- Declare your position variable for your rectangles before setup().

- Then, initialize your random number for position in setup().

When you generate a random number in draw() you get a different random number every time the program loops. So the position is different each loop.  
Setup runs only one time, so the position remains the same.

```auto
void cuadrados() {
  superRect(#F5F5F5, pos, pos, tam, tam); //replace your x, y position here
}

```

> [@Pr1M3](#):
>
> ```auto
> if(cuad){
> for(int i=0;i<10;i++){ // Also, I'm not sure about your reason for using the for loop. In your question you say you want one square, not 10 squares.
> cuadrados();
> cuad=false;
> 
> ```

See the revised code here:

```auto
int cir = 0;
boolean cuad = false;
int posy = (int)random(0, 500);
int posx = (int)random(0, 500);
int tam = 20;
int tamc = 50;
int pos; //declare your rect position as global variable here

void setup() {
  size (500, 500);
  //background(255);
  pos = int(random(500)); //initialize random position in setup
}

void draw() {
  background(255);

  if (cuad) {
    cuadrados();
  }

  if (cir == 1) {
    circulos();
  }
  botones();
}

void botones() {
  superRect(#F5F5F5, 100, 200, 50, 50);
  superRect(#F5F5F5, 160, 200, 50, 50);
  superRect(#F5F5F5, 220, 200, 50, 50);
  superRect(#F5F5F5, 280, 200, 50, 50);
  superRect(#F5F5F5, 340, 200, 50, 50);
}

//draw circle
void circulos() {
  fill(255);
  ellipse(posx, posy, tamc, tamc);
}

//draw rect in random x,y position
void cuadrados() {
  superRect(#F5F5F5, pos, pos, tam, tam); //replace your x, y position here
}

void mousePressed() {
  //boton squares
  if ( clickboton(100, 200, 50, 50, mouseX, mouseY) ) {
    cuad = true;
  }
  //circles
  if ( clickboton(160, 200, 50, 50, mouseX, mouseY) ) {
    cir = 1;
  }
  //+1
  if ( clickboton(220, 200, 50, 50, mouseX, mouseY) ) {
    tam++;
  }
  //-1
  if ( clickboton(280, 200, 50, 50, mouseX, mouseY) ) {
    tamc--;
  }
  //reset
  if ( clickboton(340, 200, 50, 50, mouseX, mouseY) ) {
    cuad = false;
    cir = 0;
    tam = 20;
    tamc = 50;
  }
}

void superRect(color c, int x, int y, int w, int h) {
  fill(c);
  rect(x, y, w, h);
}

boolean clickboton(int x, int y, int w, int h, int mx, int my) {

  if ( (mx > x && mx < (x+w)) && (my > y && my < (y+h)) ) {
    return true;
  } else {
    return false;
  }
}

```

🤓

---

<div class="post-metadata">

### Author: ![vkbr](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/vkbr/32/16844_2.png) [@vkbr](https://discourse.processing.org/u/vkbr)
#### Post date: [October 29, 2022, 4:30pm UTC](https://discourse.processing.org/t/necesito-ayuda-con-processing/39476/5 "2022-10-29T16:30:05Z")

</div>

Ola, no hablo español de verdad, entonces perdona pelos errores. 🙂

Una cosa és que

```auto
for(int i=0;i<10;i++){
cuadrados();
cuad=false;
}

```

cambia `cuad` para falso así que acaba de dibujar los quadros. O sea, por isso no los dibuja más. Tienes que encontrar otro sitio para cambiar esta booleana.

Saca esta linea `cuad=false;` que verás.

Saudaciones :))

---

<div class="post-metadata">

### Author: ![Pr1M3](https://avatars.discourse-cdn.com/v4/letter/p/41988e/32.png) [@Pr1M3](https://discourse.processing.org/u/Pr1M3)
#### Post date: [October 30, 2022, 6:50pm UTC](https://discourse.processing.org/t/necesito-ayuda-con-processing/39476/6 "2022-10-30T18:50:15Z")

</div>

Sorry, it still doesn’t work. Ignore the “for”. I tried this but it didn’t work as I wanted, what I want to do is, every time I click on the first button I want to create a new rect at a random position and this is the same for ellipse but I can’t do this!! maybe this is so easy and i can’t see, sorry for my english! and thaks for help!!!

---

<div class="post-metadata">

### Author: ![debxyz](https://avatars.discourse-cdn.com/v4/letter/d/58956e/32.png) [@debxyz](https://discourse.processing.org/u/debxyz)
#### Post date: [October 30, 2022, 9:43pm UTC](https://discourse.processing.org/t/necesito-ayuda-con-processing/39476/7 "2022-10-30T21:43:48Z")

</div>

Ok. 🙂  
Compare your original code in your first post with the code I posted in my second post. Go through each line. Your error is in this line:

> [@Pr1M3](#):
>
> ```auto
> void cuadrados(){
> superRect(#F5F5F5, (int)random(0,500), (int)random(0,500), tam, tam);
> }
> 
> ```

For you goal, you cannot use the random() function in `void draw()`.  
Draw runs continuously, so you get a different random number each loop of draw. That is why the rect position is always changing.

But you still need a random number for x, and another random number for y.

`void setup()` runs only one time! So you will need to generate your random number(s) in setup. One for the x position and one for the y position.

In the area **above void setup()** declare your x and y variables:

```auto
int posX; // x position
int posY; // y position

```

**Inside void setup()** you initialize these variables:

```auto
posX = int(random(0, 500); // here in setup() your random numbers are chosen **once**
posY = int(random(0, 500);

```

Now you can use your new x, y variables.

In draw() in:

```auto
void cuadrados(){
superRect(#F5F5F5, posX, posY, tam, tam);

```

Your random rectangle will draw at a new position each time you click inside your left most square.

🤓

---

<div class="post-metadata">

### Author: ![Pr1M3](https://avatars.discourse-cdn.com/v4/letter/p/41988e/32.png) [@Pr1M3](https://discourse.processing.org/u/Pr1M3)
#### Post date: [October 31, 2022, 12:22am UTC](https://discourse.processing.org/t/necesito-ayuda-con-processing/39476/8 "2022-10-31T00:22:24Z")

</div>

Thanks for help!!! really i apreciate your pattient, i understand now whats was wrong!!!
