rectangle error

edited May 2016 in Questions about Code

Hi I am learning to program using Process my button class work , color change when the mouse is over the button; but both button only draw in the very top corner. the error must be very simple but i can not work it out hopping some help

`/** * Button. * * Click on one of the colored squares in the * center of the image to change the color of * the background. */

button button; button button2; void setup() { size(1200, 900); button = new button(20, 20, 40, 40); button2 = new button(80, 80, 100, 100); rectMode(RADIUS); }

void draw() {

//background(0,0,255);

button2.display(); button.display(); }

//---------------------------------

class button {

int ix, iy,lx,hy; // int boxx, boxy; int size; boolean over; boolean press;

button(int x, int y, int il, int ih) { x = ix; y = iy; lx=il; hy = ih; }

void display() { overEvent(); rect(ix, iy, lx, hy);
} void overEvent() { if (over2Rect(ix, iy, lx, hy)) { fill(255,0,0); } else { fill(255,255,0); } }

boolean over2Rect(int x, int y, int xx, int yy) { if (mouseX >= x && mouseX <= x+xx && mouseY >= y && mouseY <= y+yy) { return true; } else { return false; } }}

`

Tagged:

Answers

  • Button button; 
    Button button2; 
    
    void setup() { 
      size(1200, 900); 
      button = new Button(20, 20, 40, 40); 
      button2 = new Button(80, 80, 100, 100); 
      //rectMode(RADIUS);
    }
    
    void draw() {
    
      background(0, 0, 255);
    
      button2.display(); 
      button.display();
    }
    
    //---------------------------------
    
    class Button {
    
      int ix, iy, 
        lx, hy; // int boxx, boxy; int size; boolean over; boolean press;
    
      Button(int x, int y, 
        int il, int ih) { 
        ix = x; 
        iy = y; 
        lx = il; 
        hy = ih;
      }
    
      void display() { 
        overEvent(); 
        rect(ix, iy, lx, hy);
      } 
      void overEvent() { 
        if (over2Rect(ix, iy, lx, hy)) { 
          fill(255, 0, 0);
        } else { 
          fill(255, 255, 0);
        }
      }
    
      boolean over2Rect(int x, int y, int xx, int yy) { 
        if (mouseX >= x && mouseX <= x+xx &&
          mouseY >= y && mouseY <= y+yy) { 
          return true;
        } else { 
          return false;
        }
      }
    }
    
  • Answer ✓

    first, write class name with a capital: Button

    it is very confusing when var and class are the same, they were both button

    then, in the constructor you had mixed up: x = ix; y = iy; is wrong

    you need to pass the parameter (in the ( ) brackets of the constr) into the class vars (that are before the constr)

    ix = x; 
    iy = y; 
    

    it is common to name the params with temp or with _ so it would be easily recognizeable: xTemp or x_

    also, rectMode(RADIUS); confused things

  • Thank Chrisir i see it now :)

  • i added a text to button and gave the button a value e.g button1=1 but now can not get mouse over to work. i want it to set int Valb to the value of the button.

    Button button1; Button button2; Button button3; void setup() { size(1200, 400); button1 = new Button(20, 10, 120, 30,"Button 1",1); button2 = new Button(20, 60, 120, 30,"Button 2",2); button3 = new Button(20, 110, 120, 30,"Button 3",3); } void draw() { // background(0, 0, 255); if (button1.display()== 0 )print("not found button 1"); else print("button 1"); if (button2.display()== 0 )print( "not found button 2"); else print("button 2"); if (button3.display()== 0 ) println("not found button 3"); else print("button 3"); // delay(200); //------------------------------- class Button { int ix, iy, lx, hy; String stt; int vtx; Button(int x, int y, int il, int ih, String st,int Vt) { ix = x; // x coord iy = y; // y coord lx = il; // lenght of button hy = ih; // hight of button stt= st; //text on button vtx= Vt; // number of the button } int display() { Valb=0; overEvent(); rect(ix, iy, lx, hy,10); // draw the button textSize(24);
    fill(255, 0, 0); // text fill color text(stt, ix+10, iy+25); //draw the text on the button return Valb; }

  • forgot to this to above post private int Valb; void mousePressed() { if (over2Rect(ix, iy, lx, hy)) { if (Valb==0 ) Valb = vtx; else Valb = 0; }

  • Answer ✓

    void mousePressed doesn't work inside class - instead use the variable mousePressed

    if(mousePressed) ...

  • Answer ✓

    Or use Void mousePressed outside the class and call over2rect from there

  • Thank Chrisir I got it working Now I understand Class a bit better My long term aim is control my model Railroad from Process via an Arduino.

    class Button { boolean B_flage; int ix, iy, lx, hy; String stt; int vtx; Button(int x, int y, int il, int ih, String st,int Vt) { ix = x; // x coord iy = y; // y coord lx = il; // lenght of button hy = ih; // hight of button stt= st; //text on button vtx= Vt; // number of the button } void display() { B_flage=false; overEvent(); rect(ix, iy, lx, hy,10); // draw the button textSize(24);
    fill(255, 0, 0); // text fill color text(stt, ix+10, iy+25); //draw the text on the button if (mousePressed) { if (over2Rect(ix, iy, lx, hy)) { Valb= vtx; B_flage=true; //20, 10, 120, 30 } else { Valb= 0; } } // return Valb; delay(50);// stops keybounce; }

    then in void Draw

    void draw() { // background(0, 0, 255); button1.display(); if(button1.getVal()) { Mybutton1(); } button2.display(); if(button2.getVal()) { Mybutton2();} button3.display(); if(button3.getVal()) { Mybutton3();}

    button4.display(); 
                   if(button4.getVal())
                             { Mybutton4();}                         
    

    //delay(100); }

Sign In or Register to comment.