Loading...
Logo
Processing Forum
Ok so I just picked up processing in school a couple days ago, and Im trying to work on this little project where whenever the user clicks on the screen, a circle is drawn, and gets bigger by a certain amount (in my case 10), until it reaches a maximum limit where it can no longer expand (<= width) . I don't know how to use booleans too well yet, nor loops. Heres my code, if someone could help me out it would be much appreciated, Thanks!

int x = 200;
int y = 200;
boolean on = false; 


void setup() {
  size(400,400);

void draw() {
  
  if (on == true) {
  background(0, 252, 210); 
  for (inc = 20 ; inc < width; inc = inc + 10) { 
  ellipse(x, y, inc, inc); 
  }
  }
////////////////////

void mousePressed() {
  on = true; 
}

Replies(4)

I guess what you want is something to happen ("circle getting bigger")
every time a mouse button is clicked inside canvas, right?

An easier approach would be to increase & check circle's size inside mousePressed() itself.

Here's how I've tweaked your code:
Copy code
    final color  bg = color(0, 252, 210);
    final static color c = #FF0000;
    
    final static byte  i = 20; // <-- increment
    int s = 20;  // <-- size
    
    void setup() {
      size(400, 400);
      noLoop(); // deactivates draw()
      fill(c);  // paint colorvoid draw() {
      background(bg);
      ellipse(width/2, height/2, s, s);
    }
    
    void mousePressed() {
      // increases and avoid getting bigger than width too:
      s = (s+i) % width;
      
      redraw();  // makes draw() go once
    }
    
    

Thats a great way to put it! The only thing is that I require using at least one loop ( for or while) within the program. 
Callback function draw() is itself a virtual big loop!
And the action itself (make circle bigger) is mouse-click driven!
Your program is a sitting duck until a click actually happens!

So you need a good excuse to include a loop.
How about drawing more than 1 circle?

Here's my take on it:
Copy code
    /**
     * Click'n'Inflate (v3.0)
     * for ArthurF (2013/Feb)
     *
     * http://forum.processing.org/topic/help-with-loops-and-mousepressed
     */
    
    final static color[] myColors = {
      #FF0000, #00FF00, #0000FF, #FFFF00
    };
    
    final static byte[] quadrants = {
      -1, -1, -1, 1, 1, -1, 1, 1
    };
    
    final static int   numCircles = myColors.length;
    final static color bg = 0100;  // <-- background color
    final static byte  i = 20;     // <-- increment
    int s = 20;                    // <-- size
    
    void setup() {
      size(800, 800);   // canvas dimensions
      noLoop();         // deactivates draw()void draw() {
      background(bg);
    
      // Place origin (0, 0) @ center of canvas
      translate(width>>1, height>>1);  // >>1 same as /2 ;-)
    
      for (byte c=0; c!=numCircles; c++) {
        fill( myColors[c] );  // paint color
    
        ellipse( width/numCircles * quadrants[c*2], 
        height/numCircles * quadrants[c*2 + 1], s, s );
      }
    }
    
    void mousePressed() {
      // increases and avoid getting bigger than width too:
      s = (s+i) % (width>>1);
    
      redraw();  // makes draw() go once to refresh canvas
    }