We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Button[] buttons;
int numButton = 10;
int count=0;
boolean flag =false;
int[] sum = { 0, 4, 5, 7, 10, 15, 22, 30, 39, 45 };
float lineX1=0, lineY1=0, lineX2=0, lineY2=0;
float oldlineX1=0, oldlineY1=0, oldlineX2=0, oldlineY2=0;
void setup() {
size(360, 640);
noStroke();
buttons = new Button [numButton];
int C = 150; //color
for (int i=1; i<numButton; i++) {
if (i>0 && i<4) {
buttons[i]=new Button(C, (i)*width/4, 2*height/6, width/11, i, buttons);
} else if (4<=i && i<7) {
buttons[i]=new Button(C, (i-3)*width/4, 3*height/6, width/11, i, buttons);
} else if (7<=i && i<=9) {
buttons[i]=new Button(C, (i-6)*width/4, 4*height/6, width/11, i, buttons);
}
}
}
void textln() {
// counter
for (int i=1; i<numButton; i++) {
if (buttons[i].overButton() && !buttons[i].overButtonFlag() ) {
count = i+count; //count
if (count == sum [i]) {
println("OK", count, sum[i]) ;
} else {
println("error", count, sum[i]);
}
} else {
count =0;
}
}
}
void lines() { //draw lines
if (mousePressed) {
for (int i=1; i<numButton; i++) {
if (buttons[i].overButton() && !buttons[i].overButtonFlag() && ((oldlineX1!=mouseX)||(oldlineY1!=mouseY)) ) {
lineX1=buttons[i].xPos;
lineY1=buttons[i].yPos;
oldlineX1 = lineX1;
oldlineY1 = lineY1;
}
if (buttons[i].overButton() && mousePressed && !buttons[i].overButtonFlag()&& ((lineX2!=lineX1)||(lineY1!=lineY2))) {
lineX2=buttons[i].xPos;
lineY2=buttons[i].yPos;
} else {
lineX2=mouseX;
lineY2=mouseY;
}
stroke(255);
line(lineX1, lineY1, lineX2, lineY2);
}
} else {
noStroke();
lineX1=lineY1=lineX2=lineY2=0; //clear
}
}
void draw() {
background(0);
for (int i=1; i<numButton; i++) {
buttons[i].display();
buttons[i].update();
textln();
}
lines();
}
Class Button
class Button {
color c;
float xPos;
float yPos;
float D; //diametr
boolean overButton = false;
boolean overButtonFlag =false;
int id;
Button[] others;
// constructor
Button(int myColor, float in_xPos, float in_yPos, float in_D, int idin, Button[] oin) {
c = myColor;
xPos = in_xPos;
yPos = in_yPos;
D = in_D;
id = idin;
others = oin;
}
// mouse is over the button
boolean overButton() {
if (mouseX > xPos-D/2 && mouseX < xPos+D/2 &&
mouseY > yPos-D/2 && mouseY < yPos+D/2 && mousePressed && !overButton ) {
overButton = true;
} else if (!mousePressed) {
overButton = false;
}
return overButton;
}
// button is already pressed
boolean overButtonFlag() {
if (overButton && !overButtonFlag) {
overButtonFlag = true;
} else {
overButtonFlag = false;
}
return overButtonFlag;
}
// colouring the button
void update() {
if (overButton()) {
c=255;
} else {
c=150;
}
}
//display the button
void display () {
ellipseMode (CENTER);
fill (c);
ellipse(xPos, yPos, D, D);
}
}
Answers
Correct the format of your code
OK. Now better?
Much better!
You need an ArrayList to store which spheres have been pressed connect elements with lines
For each sphere store its char / String
Both together gives you the word.....?
I understand about the word. But not quite the ArrayList...
Can you give any Example, how to use the ArrayList?
See reference/ show your attempt
there is A LOT of stuff I was too lazy to rename properly or to remove
do some house cleaning....
You're the best. Thank you so much.