Processing with mouse click

edited November 2017 in How To...

Hello,

I have to write a processing program which responds to a left mouseclick only. It's quite simple actually, the background hast to be red, if I click with my mouse the background has to turn green and when I click again it has to turn blue. And then again red etc. I am stuck at the point where I have to make a difference between the mouse clicks, like first mouse click second mouse click. I saw some examples on the internet but those weren't just with the left mousebutton only.

And should I use an void mousePressed() or not at all ?

I hope someone can give me some good directions.

Answers

  • @GoToLoop great this is exactly what I was looking for, but could you perhaps explain your code with comments? I find it quite a hard code, I don't understand everything. And since it is a school assignment, I should be able to explain what 'wrote'

  • The code is about 20 lines only. It'd be easier if you could point out exactly what you don't get it. :\">

  • You can check in this link: https://processing.org/reference/ as it will answer most of your questions.

    Kf

  • @GoToLoop yes ofcourse you're right, my bad.

        static final boolean JAVA = 1/2 != 1/2.;  //why 1/2 and static
        static final color[] PALETTE = { #FF0000, #008000, #0000FF }; //this is hex
        int idx;   //what other word would you use here
    
        void setup() {
          size(300, 200);
          noLoop();
          colorMode(RGB);
        }
    
        void draw() {
          final color c = PALETTE[idx]; //so [] means it's an array right?
          background(c);
          if (JAVA)  getSurface().setTitle("Color: " + idx + "  -  #" + hex(c, 6)); //Could you explain what happens here precisely?
        }
    
        void mousePressed() {
          keyPressed();
        }
    
        void keyPressed() {
          idx = (idx + 1) % PALETTE.length; //and here as well.
          redraw();
        }
    

    It's quite a lot as you can see, I'm really in the beginning of processing. I have a lot discover, so if you think I should be able to figure some lines out myself, I understand that. But I hope you can help me understand this, because I think this a very nice and small program.

  • edited November 2017

    static final boolean JAVA = 1/2 != 1/2.; //why 1/2 and static

    1. The expression 1/2 != 1/2. evaluates as true for Java and as false for JS, b/c for the former, the result of a division operation when both operands are integers, the decimal part is removed.
    2. A field declared as static isn't instantiated when it's class is instantiated: https://Processing.org/reference/static.html

    static final color[] PALETTE = { #FF0000, #008000, #0000FF }; //this is hex

    Web-color RGB notation https://Processing.org/reference/color_datatype.html

    int idx; //what other word would you use here

    Field idx represents the current index of the PALETTE array.

    final color c = PALETTE[idx]; //so [] means it's an array right?

    Field PALETTE was clearly declared as color[] datatype at the top of the sketch, right?
    See [] array access: https://Processing.org/reference/arrayaccess.html

  • edited November 2017

    setTitle("Color: " + idx + " - #" + hex(c, 6)); //Could you explain what happens here precisely?

  • edited November 2017

    idx = (idx + 1) % PALETTE.length; //and here as well.

    Operator % is called modulo or remainder of a division operation:
    https://Processing.org/reference/modulo.html

    Given the remainder value never reaches the divisor value, instead the remainder cycles back to 0, it is a nice trick to constrain the current index to always stay within an array's valid range length . :ar!

  • @GoToLoop you are absolutely amazing, thank you very much for explaining everything to me ! I understand your code much better now

Sign In or Register to comment.