We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi guys! I'm new here! I was desperately searching for some help. I'm making something with buttons in processing but the thing is…Something is wrong! the program didn't even run! it's supposed that the button turned into a different color when the mouse passes over them. I've made a class "Boton" (sorry, this thing is in spanglish), here's the code (some things are incomplete):
class Boton
{
int circleX, circleY;
int tamCir;
color botColor;
color botSel; //cambio de saturación cuando el mouse está encima del botón
color current;
boolean sobreBot;
float speed;
int direction;
//PImage
public Boton(int x, int y, color c, color s)
{
circleX= x;
circleY= y;
botColor=c;
botSel=s;
tamCir= 150;
current=botColor;
sobreBot=false;
}
/*
void move()
{
}
void options() //saca las 5 opciones
{
}
*/
void dibujaBoton()
{
ellipseMode(CENTER);
ellipse(circleX,circleY,tamCir,tamCir);
noStroke();
fill(current);
}
void isOver(int mx, int my)
{
int w = (int)tamCir(value);
int h = 20;
if((mx >= x && mx<= x+w) && (my>= y-h && my <= y))
return true;
else
return false;
}
}
and this is the main class:
Boton []buttons;
int activeButtons=-1;
void drawButtons()
{
for(int i=0; i<buttons.length;i++)
{
if(i==activeButtons)
buttons[i].current=buttons[i].botSel;
else
buttons[i].current=buttons[i].botColor;
buttons[i].dibujaBoton();
}
}
boolean sobreBot(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;
}
}
void update(int xx, int yy)
{
if(sobreBot(circleX, circleY, tamCir))
sobreBot = true;
else
sobreBot = false;
}
void setup()
{
size(800,750);
background(255);
buttons = new Boton[3];
buttons[0] = new Boton(width/2,350,color(240,128,7), color(200,32,60));
buttons[1] = new Boton(width/3,500,color(123,221,240), color(13,78,190));
buttons[2] = new Boton(2*width/3,500,color(135,234,36), color(11,200,23));
}
void draw()
{
background(255);
drawButtons();
}
Answers
There is a problem with your code: it doesn't even compile!
What is this
int w = (int)tamCir(value);
line? tamCir isn't a function, it is an int!What are these x, w, etc. variables used in the isOver() function? They don't exist in the class. Beside, that's a method to see if mouse is on a rectangle, but your buttons are circles! Use dist() instead.
The functions sobreBot() and update() outside of the class are useless and never called, why do you include them?
Actually, the code in sobreBot() is closer of what you want in the isOver() method, but, again, dist() is already doing these maths with sqrt() and sq().
Ok, i have to accept that I really suck at this. I changed some things, now the buttons display (but they don't do anything tho). Now, I'm trying to do an Image class, but I have no idea of how to do it, each button should have an assigned Image, for example, for the blue button, it shows a blue flower and so. I was thinking of making a class within a class, but I don't know if that's possible, can you (or someone else) help me? here's the other classes: Main Class(mouseClicked doesn't do anything yet)
Button class:
I suggest to try to make your buttons to work before using images. No need to a class to manage the images, PImage is already doing this, just add a PImage field to your class, and use it in the dibujaBoton() method.
Some remarks on the current code:
Aaaand what if I have to show like, 45 images. Like the idea is to click the button, and press 1 to 5 to choose a "generation"(so that will be like 15 images per button). I'm making like a program which will help you choose a pokemon starter, showing some information and stats. so that will be like, as I said before, 45 images (I'm trying to reduce the quantity). I still don't need an Image class?
I still don't get how 3 buttons and 5 keys got 45 images. :-/
Anyways, I've come up w/ a initial sketch to get you started. Although it's 5 PImage objects divided by 3 Button objects. :bz
Try to modify it or use any parts for your particular needs. And ask for any further doubts! <):)