/* joan soler-adillon www.joan.cat setembre 2006 */ rectangulus francesc; rectangulus ramon; void setup(){ size(200,200); //l'objecte rectangulus vol quatre paràmetre: x,y, llargada,alçada, //creem el rectangle de 70x90 al punt 20,50: francesc = new rectangulus(20,50,70,90); //creem un altre rectangle de 70x90 al punt 110,50: ramon = new rectangulus(110,50,70,90); //un cop creats, els hi demanem que es dibuixin: francesc.drawYourself(); ramon.drawYourself(); } void draw(){ //nothing here... } void mousePressed(){ //hi ha un click! //ara cal que comprobem si s'ha tocat cap rectangle //enviem als rectangles el missatge, i que facin ells la resta. francesc.theresAClick(); ramon.theresAClick(); } //////////////////////////////////////////////////////// /////////////////////////////////////////////////////// //declarem la classe class rectangulus{ //inicialitzem variables int myX, myY, myWidth, myHeight; //això és el constuctor: rectangulus (int _x, int _y, int _w, int _h){ //associem lo rebut a les variables pròpies myX = _x; myY = _y; myWidth = _w; myHeight = _h; } //funció per dibuixar-se void drawYourself(){ rect(myX,myY,myWidth,myHeight); } //funció perquè faci el que ha de fer quan hi ha un click void theresAClick(){ //l'ha tocat? if((mouseX >= myX && mouseX <= myX+myWidth)&&(mouseY >= myY && mouseY <= myY+myHeight)){ //sí que l'ha tocat! //doncs perquè es vegi, dibuixem un cercle vermell al mig del rectangle fill(255,0,0,32); ellipse(myX+(myWidth/2),myY+(myHeight/2),25,25); } } }