Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6047603
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:22:09+00:00 2026-05-23T07:22:09+00:00

I can’t seem to get the follow code right. This is a basic program

  • 0

I can’t seem to get the follow code right.

This is a basic program I’m using with Processing. I made the square change color when I click it, but I can’t seem to get it to change back again when clicked for a second time.

It’s basically a toggle button when I click the square and NOT when I release the mouse button. I’m trying to integrate it with Arduino, that’s why there is the port write.

boolean A = 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 ((A) && (mousePressed) && ((mouseX > x) && (mouseX < x + w) &&
        (mouseY > y) && (mouseY < y + h))) { // If mouse is pressed,

        fill(40, 80, 90);
        A = !A;// change color and
        port.write("H"); // Send an H to indicate mouse is over square.
    }
    rect(50, 50, 100, 100); // Draw a square.
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T07:22:09+00:00Added an answer on May 23, 2026 at 7:22 am

    Here’s some example code that should do what you want. A few things to note:

    The draw() 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.

    You were on the right track with the A variable. I’ve renamed it to squareVisible. It is a boolean variable that indicates whether to draw the square or not. The draw() function checks it’s state, and changes the fill to only draw the square if squareVisible is true.

    The mousePressed() function is called by Processing when you click somewhere in the sketch. It is toggling the squareVisible variable.

    The mouseMoved() 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.

    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 > x) && (mouseX < x + w) &&
                (mouseY > y) && (mouseY < y + h))) {
            // if mouse clicked inside square
            squareVisible = !squareVisible;  // toggle square visibility
        }
    }
    
    void mouseMoved() {
        if (((mouseX > x) && (mouseX < x + w) &&
                (mouseY > y) && (mouseY < y + h))) {
            port.write("H");                 // send an H to indicate mouse is over square
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can i get the source code for a WAMP stack installer somewhere? Any help
Can we close all known/unknown connections to database with the code? I'm using Access
Can't seem to get the Back Button to appear in a UINavigationController flow. I
can I change background color for disabled <h:inputText> ? I've tried to do this
Can anyone tell me whats wrong in this code? when i try this i
Can I get a 'when to use' for these and others? <% %> <%#
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Can I use the following across all browsers? <a href=# onclick=doSomething()>Click here.</a> Is this
Can anyone tell me what's wrong with this robots.txt? http://bizup.cloudapp.net/robots.txt The following is the
Can someone maybe tell me why this is not working? I have used echo

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.