How can i create a "boundary" for my ms paint project?

edited September 2015 in Questions about Code

So, in an attempt to get better at using processing and programming, i decided to make a program as similar to ms paint as possible. So, what i did was i made the layout, three rectangles, one for the menu (color, stroke, and what not), the light blue background color, and the white pad where the drawing is actually made. Anyways, now that i have these three rectangles, how do i make it so when i draw the line (line(mouseX, mouseY, pmouseX, pmouseY) stays inside my white rectangle? I tried some lengthy if statement, but that failed miserably. if (mouse coordinates are outside the white box size/ coordinates) { stroke(0); //0 means no line right? or whatever the code is, my line stops drawing, or the mouseDragged is overridden in thinking that the mouse is not pressed or something) } else { stroke(10); }

I tried a boolean, where if it was outside, allowDraw would be false, and if it's false, then i can't draw, but again, it still draws the line outside of the "boundaries"

Sorry, again, i am a new programmer, and i can't figure out a way to create this boundary. I'll include the code so you can see what the layout looks like (the if statement and boolean are both deleted since it did not work, or maybe i just messed up) PS: i tried using the draw function, and well, even though the border code sort of works, it's just not as smooth and it's very detached vs the draw line code, and the circles that it places can overlap into the background, vs being cutoff by the "border."

Thanks for your help and tips.

void setup() { //setting the layout size(1440, 760);
background(192, 203, 234);

//draw pad noStroke(); fill(255); rect(10, 125, 1000, 500);

//menu box fill(232, 238, 255); rect(0, 20, 1440, 100); }

void draw() { }

void mouseDragged() { stroke(random(255), random(255), random(255)); strokeWeight(20); line(mouseX, mouseY, pmouseX, pmouseY); }

Yeah, there's a trippy rainbow code, i couldn't figure out how to make the borders so i made the rainbow code instead. haha Thanks guys.

Answers

  • _vk_vk
    Answer ✓

    -1 Format code posted in forum please.

    -2 Use variables instead of hard numbers for menu and canvas positioning.

    -3 Usually it's not a good idea to draw inside events callbacks. It's fine so far though...

    -4 I think a PGraphics as a canvas would be a better idea.

    Anyway, to your question:

    final int CANVAS_X = 10, CANVAS_Y = 125, CANVAS_W = 1000, CANVAS_H = 500;
    final int HALF_STROKE = 10;
    
    
    
    void setup() { 
      //setting the layout 
      size(1440, 760);
      background(192, 203, 234);
    
      //draw pad noStroke(); 
      fill(255); 
      rect(CANVAS_X, CANVAS_Y, CANVAS_W, CANVAS_H);
    
      //menu box
      fill(232, 238, 255); 
      rect(0, 20, 1440, 100);
    }
    
    
    void draw() {
    }
    
    void mouseDragged() { 
      stroke(random(255), random(255), random(255)); 
      strokeWeight(HALF_STROKE*2); 
    
    
    
      if (isInside(mouseX, mouseY) && isInside(pmouseX, pmouseY)) {
        line(mouseX, mouseY, pmouseX, pmouseY);
      }
    }
    
    
    boolean isInside(int x, int y) {
      return x > CANVAS_X + HALF_STROKE &&
        x < CANVAS_X + CANVAS_W  - HALF_STROKE && 
        y > CANVAS_Y + HALF_STROKE  && 
        y < CANVAS_Y + CANVAS_H - HALF_STROKE;
    }
    
  • Thanks for the tips. sorry, i tried the "code" button, but nothing happened, it still posted as normal text vs your code. I probably messed it up though. thanks again.

  • _vk_vk
    Answer ✓

    There is a link in my previous post to the page explaining how to format the code. Perhaps you missed skipping a line before and after the code block...

    :)

Sign In or Register to comment.