Simple Paint Program

edited October 2013 in Programming Questions

The program must draw the color in the corner, erase and have the ability to change hue, saturation and brightness at this point that is what I am having trouble with. Could someone walk me through and help me figure out what I did wrong?

Code:

color CurrentColor;
int strokeW;
int flag=0;
int hue=0;
int sat=0;
int bright=0;
int alpha=0;

void setup()
{
  size(640, 480);
  colorMode(HSB);
  background(255);
  stroke(hue, sat, bright, alpha);
  CurrentColor = color(0, 0, 0, 0);

}
void draw()
{
  noStroke();
  fill(CurrentColor);
  rect(0, 0, 30, 30);
  stroke(CurrentColor);
  strokeWeight(strokeW);
  if(flag==1) line(mouseX, mouseY, pmouseX, pmouseY);
  if (keyPressed){
    if (key == 's') sat = sat-1;
    if (key == 'S') sat = sat+1;
    if (key == 'b') bright = bright-1;
    if (key == 'B') bright = bright+1;
    if (key == 'h') hue = hue-1;
    if (key == 'H') hue = hue+1;
  }
}

void mouseDragged() {
  flag=1;
}
void mouseReleased(){
  flag=0;
}


void keyPressed() {
  if (key == 'c')  
    background(255);
    if (key == 'p') background(CurrentColor);
    if (key == '1') strokeW = 1;
    if (key == '2') strokeW = 2;
    if (key == '3') strokeW = 3;
    if (key == '4') strokeW = 4;
    if (key == '5') strokeW = 5;
    if (key == '6') strokeW = 6;
    if (key == '7') strokeW = 7;
    if (key == '8') strokeW = 8;
    if (key == '9') strokeW = 9;
    if (key == '0') strokeW = 0;
    if (key == 'w') saveFrame("save-######.png");  
}

Answers

  • edited October 2013

    My experiments w/ your code. See if it's close to whatcha want:

    /** 
     * Simple Paint (v2.11)
     * by  lilmink (2013/Oct)
     * mod GoToLoop
     * 
     * forum.processing.org/two/discussion/71/simple-paint-program
     */
    
    final static color HUE = 360, SAT = 100, BRT = 100;
    color hue, sat = SAT, brt = BRT;
    String path = "save-######.png";
    
    void setup() {
      size(640, 480);
      frameRate(30);
      noLoop();
      smooth();
      colorMode(HSB, HUE, SAT, BRT);
    
      background(-1);
      keyPressed();
    
      path = dataPath(path);
    }
    
    void draw() {
      frame.setTitle("Hue: " + hue + "\tSat: " + 
        sat + "\tBright: " + brt);
    }
    
    void mouseDragged() {
      line(mouseX, mouseY, pmouseX, pmouseY);
      redraw();
    }
    
    void keyPressed() {
      final int k = key, kc = keyCode;
      if (k == CODED)   return;
    
      if (kc == 'C')       background(-1);
      else if (kc == 'P')  background(hue, sat, brt);
      else if (kc == 'W')  saveFrame(path);
    
      else if (k == 'h')   --hue;
      else if (k == 'H')   ++hue;
      else if (k == 's')   --sat;
      else if (k == 'S')   ++sat;
      else if (k == 'b')   --brt;
      else if (k == 'B')   ++brt;
    
      else if (k >= '0' & k <= '9')   strokeWeight(k - '0');
    
      hue = constrain(hue, 0, HUE);
      sat = constrain(sat, 0, SAT);
      brt = constrain(brt, 0, BRT);
    
      stroke(hue, sat, brt);
      redraw();
    }
    
  • do you know why it wasn't working? It looks like you aren't setting the stroke size at first, you should at least give it a default value. You also had keychecking in two locations when it should be in the keypressed event. you don't need to set so many things every time draw is called your settings should be set before draw at least for this program. GotoLoop's code above is pretty nice and concise. here's something i put together http://studio.sketchpad.cc/sp/pad/view/hNPFWzhGal/rev.823 that is similar. good luck.

  •       color c=color(0);
          int x, y, xp, yp;
          int strokeW=1,flag=0;
          void setup()
          {
            size(640, 480);
            background(255);
          }
          void draw()
          {
            noStroke();
            fill(c);
            rect(0, 0, 30, 30);
            stroke(c);
            strokeWeight(strokeW);
            if(flag==1) line(mouseX, mouseY, pmouseX, pmouseY);
          }
    
          void mouseDragged() {
            flag=1;
          }
          void mouseReleased(){
            flag=0;
          }
    
    
          void keyPressed() {
            if (key == 'c')  
              background(0);
            if (key == CODED) {
              if (keyCode == UP) strokeW++; 
              if (keyCode == DOWN) strokeW--;
            }
            if (strokeW<0)strokeW=1;
            if(key== ' ') c = color(random(0,255),random(0,255),random(0,255));
          }
    
  • thanks everyone for the help, that helped me edit my own to allow hsb editable, my next dilemma is making an eraser that goes to the previous hsb after its been used. How can i do this?

  • This is a copy and pasted program from above with red (3), blue (5), green (4), gray(6), white (1), and black (2) color selections and made tab the random color changer.

    void setup() { size(1900, 1000); background(c2); }

    int x,y,xp,yp; int strokeW=1,flag=0;

    color c = color(255); color c2 = color(0);

    void draw() {

    fill(c); noStroke(); rect(0, 0, 50, 50); stroke(c); strokeWeight(strokeW); if(flag==1) line(mouseX, mouseY, pmouseX, pmouseY); } void mouseDragged(){ flag=1; } void mouseReleased(){ flag=0; }

    void keyPressed() { if (key == CODED) { if (keyCode == UP) strokeW++; if (keyCode == DOWN) strokeW--; } if (strokeW<0)strokeW=1; if(key == TAB) c = color(random(0,255),random(0,255),random(0,255)); if(key == '1') c = color(255); else if(key == '2') c = color(0); else if(key == '3') c = color(100,0,0); else if(key == '4') c = color(0,100,0); else if(key == '5') c = color(0,0,100); else if(key == '6') c = color(100,100,100); if (keyCode == BACKSPACE) { background(c2); } }

  • @Green_Titan This is an old post.

    Please edit your post, select your code and hit ctrl+o to format your code. Make sure there is an empty line above and below your code.

    Kf

Sign In or Register to comment.