Necesito ayuda con processing

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;
}

}

1 Like

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

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

1 Like

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.

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.

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

See the revised code here:

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;
  }
}

:nerd_face:

3 Likes

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

Una cosa és que

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 :))

1 Like

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!!!

Ok. :slightly_smiling_face:
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:

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:

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

Inside void setup() you initialize these variables:

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:

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.

:nerd_face:

3 Likes

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

3 Likes