We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
interface (Read 805 times)
interface
May 29th, 2009, 5:52pm
 
Hi guys, i'm new in programing and doing a school project, so i want to create an interface where the user clicks the button with an image then the image displays in the background. the code is as follows

color currentcolor;

RectButton rect1, rect2, rect3;
RectButton rect4, rect5, rect6;
RectButton rect7, rect8, rect9;

PImage a;
PImage b;
PImage c;
PImage d;
PImage e;

boolean locked = false;

void setup()
{
 size(400, 400);
 smooth();


 color baseColor = color(102);
 currentcolor = baseColor;

a = loadImage("carling_black_label.jpeg");
b = loadImage("johnny_Walker.jpeg");
c = loadImage("Dark_and_lovelly_kids.jpeg");
d = loadImage("Chivas_Regal_18yrs.gif");
e = loadImage("J_&_B.jpeg");

 // Define and create rectangle button
 color buttoncolor = color(224);
 color highlight = color(153);
 rectMode(CENTER);
 rect1 = new RectButton(10, 20, 10, buttoncolor, a);

 // Define and create rectangle button
 buttoncolor = color(204);
 highlight = color(153);
 rect2 = new RectButton(50, 20, 10, buttoncolor, highlight);

 // Define and create circle button
 buttoncolor = color(173);
 highlight = color(102);
 rect3 = new RectButton(90, 20, 10, buttoncolor, highlight);

 // Define and create rectangle button
 buttoncolor = color(152);
 highlight = color(51);
 rect4 = new RectButton(130, 20, 10, buttoncolor, highlight);

 // Define and create rectangle button
 buttoncolor = color(110);
 highlight = color(0);
 rect5 = new RectButton(170, 20, 10, buttoncolor, highlight);
 
 // Define and create rectangle button
 buttoncolor = color(100);
 highlight = color(0);
 rect6 = new RectButton(210, 20, 10, buttoncolor, highlight);
 
 // Define and create rectangle button
 buttoncolor = color(95);
 highlight = color(0);
 rect7 = new RectButton(250, 20, 10, buttoncolor, highlight);
 
 // Define and create rectangle button
 buttoncolor = color(70);
 highlight = color(0);
 rect8 = new RectButton(290, 20, 10, buttoncolor, highlight);
 
 // Define and create rectangle button
 buttoncolor = color(50);
 highlight = color(0);
 rect9 = new RectButton(340, 20, 10, buttoncolor, highlight);
}

void draw()
{
 background(currentcolor);
 stroke(255);
 update(mouseX, mouseY);
 rect1.display();
 rect2.display();
 rect3.display();
 rect4.display();
 rect5.display();
 rect6.display();
 rect7.display();
 rect8.display();
 rect9.display();
}

void update(int x, int y)
{
 if(locked == false) {
   rect1.update();
   rect2.update();
   rect3.update();
   rect4.update();
   rect5.update();
   rect6.update();
   rect7.update();
   rect8.update();
   rect9.update();
   
 }
 else {
   locked = false;
 }

 if(mousePressed) {
   if(rect1.pressed()) {
     currentcolor = rect1.basecolor;
   }
   else if(rect2.pressed()) {
     currentcolor = rect2.basecolor;
   }
   else if(rect3.pressed()) {
     currentcolor = rect3.basecolor;
   }
   else if(rect4.pressed()) {
     currentcolor = rect4.basecolor;
   }
   else if(rect5.pressed()) {
     currentcolor = rect5.basecolor;
   }
   else if(rect6.pressed()) {
     currentcolor = rect6.basecolor;
   }
   else if(rect7.pressed()) {
     currentcolor = rect7.basecolor;
   }
   else if(rect8.pressed()) {
     currentcolor = rect8.basecolor;
   }
   else if(rect9.pressed()) {
     currentcolor = rect9.basecolor;
   }
 }
}


class Button
{
 int x, y;
 int size;
 color basecolor, highlightcolor;
 color currentcolor;
 boolean over = false;
 boolean pressed = false;  

 void update()
 {
   if(over()) {
     currentcolor = highlightcolor;
   }
   else {
     currentcolor = basecolor;
   }
 }

 boolean pressed()
 {
   if(over) {
     locked = true;
     return true;
   }
   else {
     locked = false;
     return false;
   }    
 }

 boolean over()
 {
   return true;
 }

 boolean overRect(int x, int y, int width, int height)
 {
   if (mouseX >= x && mouseX <= x+width &&
     mouseY >= y && mouseY <= y+height) {
     return true;
   }
   else {
     return false;
   }
 }

 boolean overRect(int x, int y, int diameter)
 {
   float disX = x - mouseX;
   float disY = y - mouseY;
   if(sqrt(sq(disX) + sq(disY)) < diameter/2 ) {
     return true;
   }
   else {
     return false;
   }
 }

}


class RectButton1 extends Button
{
 RectButton1(int ix, int iy, int isize, color icolor, color ihighlight)
 {
   x = ix;
   y = iy;
   size = isize;
   basecolor = icolor;
   highlightcolor = ihighlight;
   currentcolor = basecolor;
 }

 boolean over()
 {
   if( overRect(x, y, size) ) {
     over = true;
     return true;
   }
   else {
     over = false;
     return false;
   }
 }

 void display()
 {
   stroke(255);
   fill(currentcolor);
   rect(x, y, size, size);
 }
}


class RectButton extends Button
{
 RectButton(int ix, int iy, int isize, color icolor, color ihighlight)
 {
   x = ix;
   y = iy;
   size = isize;
   basecolor = icolor;
   highlightcolor = ihighlight;
   currentcolor = basecolor;
 }

 boolean over()
 {
   if( overRect(x, y, size) ) {
     over = true;
     return true;
   }
   else {
     over = false;
     return false;
   }
 }

 void display()
 {
   stroke(255);
   fill(currentcolor);
   rect(x, y, size, size);
 }
}



Someone please help
Re: interface
Reply #1 - May 30th, 2009, 1:49am
 
loyd wrote on May 29th, 2009, 5:52pm:
Someone please help

Well, perhaps you should explain where you are stuck...
Generic advice: learn to use arrays and for loop.
Instead of looping in writing your code, let the computer do it for you! Smiley
Re: interface
Reply #2 - Jun 18th, 2009, 7:36am
 
Hey Philho... Winkvery much apologize for appearing rude (not replying), but the project is coming very well though. I'm on the final stages.

But thanx for replying...very much appreciated.http:)
Page Index Toggle Pages: 1