Hello, I need assistance in making my scroll wheel change the size of my ellipse drawing tool.

I just need a sample code to help me with making the scroll wheel change the size of my ellipse as I scroll up it enlarges and as I scroll down it goes smaller. Anything helps. Thanks!

import controlP5.*;
ControlP5 cp5;

int c = color(100);

void setup() {
size(800, 800);
smooth();
cp5 = new ControlP5( this );
cp5.addColorWheel("c", 596, 3, 200 ).setRGB(color(128, 0, 255));
noStroke();
background(0, 255, 255);

//Below is the drawing area black border.
stroke(0);
strokeWeight(8);
rect(10, 220, 780, 570);
fill(255);

PFont font;
font = loadFont("UrbanClass-10.vlw");

//Instructions are below in this code.
String s = "Your objective is to complete the following drawing in 2 minutes. If you don't finish then restart and try again! This is only a beta.";
fill(50);
text(s, 10, 10, 400, 100); // Text wraps within text box

//Below is "Draw Here!" inside of rect.
textFont(font, 12);
fill(0);
text("Draw Here!", 360, 240);
}

void draw() {
frameRate(800);
println(mouseX + " : " + mouseY);

if ( mouseX < 790 && mouseY > 10) {

//Draw normal
if (mousePressed && (mouseButton == LEFT)) {
ellipse(mouseX, mouseY, 5, 5);
fill(c);
noStroke();

//Eraser
} else if (mousePressed && (mouseButton == RIGHT)) {
ellipse(mouseX, mouseY, 10, 10);
noStroke();
fill(255);
}
}
}

void mouseWheel(MouseEvent event) {
float e = event.getCount();
println(e);
ellipse(mouseX, mouseY, 10, 10);
fill(c);
noStroke();
}

Answers

  • edited April 2016 Answer ✓

    It's not possible to draw things outside of a void draw(){ } function.
    I made a new int brushSize that the ellipse() uses to make each new circle.

    import controlP5.*;
    ControlP5 cp5;
    int brushSize = 10;
    boolean brushType  = false;
    boolean eraserType = false;
    color c = color(128, 0, 255); // Moved color() here to read cp5 easier.
    
    /*******************************/ // Use dividers to reduce eye strain.
    void setup() {
      size(800, 800); // P2D is fastest.
      frameRate(60); 
      // frameRate() goes in setup(). Don't call this every drawing loop.
      // 60[f/s] / 1000[ms/s] == 16 [ms/f] milliseconds per frame.
      // Human reaction time is about 200 milliseconds.
      // Try timing yourself with millis(). It's kinda fun. :)
      // Look into beizer curve or draw line Examples for making smooth lines.
      // Examples > Topics > Drawing > ContinuousLines.pde
    
      cp5 = new ControlP5( this );
      cp5.addColorWheel("c", 596, 3, 200 ).setRGB(c);
      noStroke();
      background(0, 255, 255);
    
      //Below is the drawing area black border.
      stroke(0);
      strokeWeight(8);
      rect(10, 220, 780, 570);
      fill(255);
    
      PFont font;
      font = loadFont("ArialMT-10.vlw");
    
      //Instructions are below in this code.
      String s = "Your objective is to complete the following drawing in 2 minutes. If you don't finish then restart and try again! This is only a beta.";
      fill(50);
      text(s, 10, 10, 400, 100); // Text wraps within text box
    
      //Below is "Draw Here!" inside of rect.
      textFont(font, 12);
      fill(0);
      text("Draw Here!", 360, 240);
    
      // Just call noStroke() once before draw() if stroke(x) is NOT in draw().
      noStroke();
    
      // We don't need to draw a frame until they push the buttons.
      noLoop();
    }
    
    
    /*******************************/
    void draw() {
      println(mouseX + " " + mouseY);
      if ( mouseX < 790 && mouseX > 10  )
      if ( mouseY > 225 && mouseY < 790 ) { // Ranged region of interest.
    
        if(brushType) {
          fill(c); // Call fill() before drawing so the previous frame's fill() doesn't overlap.
          ellipse(mouseX, mouseY, brushSize, brushSize); 
    
        } else if(eraserType) {
          fill(255);
          ellipse(mouseX, mouseY, brushSize, brushSize); 
        } // end if tree
    
      } // end if mouse location
    } // end draw
    
    
    /*******************************/
    void mousePressed() { // When we click, it changes.
             if(mouseButton == RIGHT) {
        eraserType = true;
      } else if(mouseButton == LEFT) {
        brushType = true;
      }
      loop(); // Start drawing after we change the type.
    }
    
    
    /*******************************/
    void mouseReleased() {
      eraserType = brushType = false;
      noLoop(); // Stop the drawing when we stop drawing so the type properly changes.
    }
    
    
    /*******************************/
    void mouseWheel(MouseEvent event) {
      int e = int(event.getCount());
      if(e < 0) {
        if(brushSize >= 1)
          brushSize += e; // e == -x, so...brushSize + (-1) == brushSize-1;
        println(e + " -- " + brushSize);
      } else {
        brushSize += e;
        println(e + " ++ " + brushSize);
      }
    }
    
  • Thanks for the help. Do you mind if I use some of this code to aid me in my code?

  • Well it is your program, I just knocked the dust off. Sure, you can use your own idea! :)

  • I am a beginner so this really helps. Thanks, man! Really appreciate the help.

  • Answer ✓

    No problem. Keep workin' at it! :-?

Sign In or Register to comment.