How to connect the dots with the lines and check the result? Like graphic key on the Android )

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);
  }
}
Tagged:

Answers

  • Correct the format of your code

  • OK. Now better?

  • Much better!

  • Answer ✓

    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?

  • Answer ✓

    See reference/ show your attempt

  • Answer ✓
    ArrayList<Ball> balls;
    
    //int xPos = 10;
    //int yPos = 0;
    int rectWidth = 30;
    int rectHeight = 30;
    int beginPosition = 0;
    int stopPosition = 650;
    //int mouseVar;
    //float rCol, gCol, bCol;
    
    void setup() {
      size(400, 800);
      // frameRate(10);
      //  smooth();
      noStroke();
      balls = new ArrayList();
      for (int i=0; i<random (12, 23); i++) 
        balls.add(new Ball( random(0, width), random(0, height), 
          rectWidth, rectHeight, 
          beginPosition, stopPosition, 
          random(0, 255), random(0, 255), random(0, 255)));
      //
    } // func 
    
    void draw() {
      background(255);
      // for-loop
      for (int i = balls.size()-1; i >= 0; i--) { 
        Ball ball = balls.get(i);
        ball.moveThis();
        ball.display();
      } // for 
      println(balls.size());
    } // func 
    
    void mousePressed() {
      balls.add(
        new Ball( mouseX, mouseY, 
        rectWidth, rectHeight, 
        beginPosition, stopPosition, 
        random(0, 255), random(0, 255), random(0, 255))
        );
      //
    }
    
    // ==================================================
    
    class Ball {  
      // pos
      float x;
      float y;
      // size
      float w;
      float h;
      // movement 
      float beginPos;
      float stopPos;
      float speed = 5;
      // color // or use color() 
      float r, g, b;
    
      Ball(float tempX, float tempY, 
        float tempW, float tempH, 
        float tempBegin, float tempStop, 
        float tempR, float tempG, float tempB) {
        x = tempX;
        y = tempY;
        w = tempW;
        h = tempH;
        beginPos = tempBegin;
        stopPos = tempStop;
        r = tempR;
        g = tempG;
        b = tempB;
      }
    
      void moveThis() {
        y = y + speed;
        if (y > stopPos) {
          y = stopPos;
        }
      }
    
      void display() {
        fill(r, g, b);
        ellipse (x, y, w, w);
      }
      //
    } // class
    //
    
  • Answer ✓
    Button[] buttons;
    
    ArrayList<Ball> balls=new ArrayList();
    
    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;
    
    String result="";
    
    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
    
    
      // noStroke();
      //  lineX1=lineY1=lineX2=lineY2=0; //clear
    
      stroke(255);
      for (int i=0; i<balls.size()-1; i++ )
    
        line ( buttons[balls.get(i).ID].xPos, 
          buttons[balls.get(i).ID].yPos, 
          buttons[balls.get(i+1).ID].xPos, 
          buttons[balls.get(i+1).ID].yPos);
    }
    
    
    void mousePressed() {
      for (int i=1; i<numButton; i++) {
    
        if (buttons[i].overButton() ) {
    
          result+=buttons[i].id; 
    
          balls.add(
            new Ball( mouseX, mouseY, 
            5, 5, 
            1, 2, 
            random(0, 255), random(0, 255), random(0, 255), 
            i         )        );
        }
      }
    }//func
    
    void draw() {
      background(0);
      for (int i=1; i<numButton; i++) {
        buttons[i].display();
        buttons[i].update();
        textln();
      }
      lines();
    
      fill(255);
      text(result, 22, 22);
    }
    
    //==============================================
    
    
    class Button { 
      color c;
      float xPos;
      float yPos;
      float D; //diametr
    
      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() {
        boolean overButton = false;
        if (mouseX > xPos-D && mouseX < xPos+D && 
          mouseY > yPos-D && mouseY < yPos+D  ) {
          overButton = true;
        } else {
          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);
      }
    }
    //
    
    // ==================================================
    
    class Ball {  
      // pos
      float x;
      float y;
      // size
      float w;
      float h;
      // movement 
      float beginPos;
      float stopPos;
      float speed = 5;
      // color // or use color() 
      float r, g, b;
    
      int ID; 
    
      Ball(float tempX, float tempY, 
        float tempW, float tempH, 
        float tempBegin, float tempStop, 
        float tempR, float tempG, float tempB, 
        int tempID) {
        x = tempX;
        y = tempY;
        w = tempW;
        h = tempH;
        beginPos = tempBegin;
        stopPos = tempStop;
        r = tempR;
        g = tempG;
        b = tempB;
    
        ID=tempID ;
      }
    
      void moveThis() {
        y = y + speed;
        if (y > stopPos) {
          y = stopPos;
        }
      }
    
      void display() {
        fill(r, g, b);
        ellipse (x, y, w, w);
      }
      //
    } // class
    //
    
  • 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.

Sign In or Register to comment.