Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's some example code that should do what you want. A few things to note:</p> <p>The <code>draw()</code> function should only be used to actually draw your sketch, other code should be located elsewhere. It is called in a continuous loop to redraw the screen, so any extra code can slow down or even prevent the redrawing, which is undesirable.</p> <p>You were on the right track with the <code>A</code> variable. I've renamed it to <code>squareVisible</code>. It is a boolean variable that indicates whether to draw the square or not. The <code>draw()</code> function checks it's state, and changes the fill to only draw the square if <code>squareVisible</code> is true.</p> <p>The <code>mousePressed()</code> function is called by Processing when you click somewhere in the sketch. It is toggling the squareVisible variable.</p> <p>The <code>mouseMoved()</code> function is called by Processing when you move the mouse without clicking, it is a better place to send serial output than the draw() function.</p> <pre><code>boolean squareVisible = true; int x = 50; int y = 50; int w = 100; int h = 100; import processing.serial.*; Serial port; int val; void setup() { size(200, 200); noStroke(); fill(255, 0, 0); rect(x, y, w, h); port = new Serial(this, 9600); } void draw() { background(255); if (squareVisible) { fill(40, 80, 90); } else { fill(255, 0, 0); } rect(x, y, w, h); // Draw a square } void mousePressed() { if (((mouseX &gt; x) &amp;&amp; (mouseX &lt; x + w) &amp;&amp; (mouseY &gt; y) &amp;&amp; (mouseY &lt; y + h))) { // if mouse clicked inside square squareVisible = !squareVisible; // toggle square visibility } } void mouseMoved() { if (((mouseX &gt; x) &amp;&amp; (mouseX &lt; x + w) &amp;&amp; (mouseY &gt; y) &amp;&amp; (mouseY &lt; y + h))) { port.write("H"); // send an H to indicate mouse is over square } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload