background in setup() vs draw()
in
Contributed Library Questions
•
6 months ago
I'm not sure if this is the right section. The question deals more with basic programming but it does include the controlP5 library so I put it in here. Anyways....
Is there anyway to redraw the background when I change the sliders but not when click the mouse to draw ellipses?
I've tried to add background(255); to the control event section for when I move the sliders but that blocks the rectangle from being updated till I release the mouse.
I feel like I'm missing something really obvious but I can't seem to find how to do it. Thanks in advance for the help!
Here's my code:
- import controlP5.*;
- ControlP5 cp5;
- int unitWidth;
- int floorToFloor;
- float actualWidth;
- float actualHeight;
- float rectX1;
- float rectX2;
- float rectY1;
- float rectY2;
- void setup() {
- size(1024,768);
- background(255); //With background here my circles work but when I change the sliders to change the rectangle
- //it doesn't redraw over the old ones.
- cp5 = new ControlP5(this);
- cp5.setColorLabel(color(0));
- cp5.setColorValue(color(255));
- // sliders that animate the rectangle, I want these to show the current rectangle without showing the "after images"
- cp5.addSlider("unitWidth")
- .setPosition(20,20)
- .setSize(150, 20)
- .setValue(25)
- .setRange(15,45)
- .setNumberOfTickMarks(7);
- ;
- cp5.getController("unitWidth").getCaptionLabel().align(ControlP5.LEFT, ControlP5.BOTTOM_OUTSIDE).setPaddingY(5);
- cp5.addSlider("floorToFloor")
- .setPosition(210,20)
- .setSize(150,20)
- .setValue(10)
- .setRange(8,15)
- .setNumberOfTickMarks(8)
- ;
- cp5.getController("floorToFloor").getCaptionLabel().align(ControlP5.LEFT, ControlP5.BOTTOM_OUTSIDE).setPaddingY(5);
- }
- void draw() {
- // If background is here the sliders and rectangle work well but the ellipses won't show up because the background redraws over them.
- actualWidth = unitWidth*(21.866667);
- actualHeight = floorToFloor*(21.866667);
- rectX1 = (width/2) - (actualWidth/2);
- rectX2 = (width/2) + (actualWidth/2);
- rectY1 = 414 - (actualHeight/2);
- rectY2 = 414 + (actualHeight/2);
- fill(255);
- strokeWeight(1);
- stroke(0);
- line(0,60,width,60);
- //rectangle, I know I could use rect() but I need all the corner points for later.
- line(rectX1, rectY1, rectX1, rectY2);
- line(rectX1, rectY1, rectX2, rectY1);
- line(rectX2, rectY1, rectX2, rectY2);
- line(rectX1, rectY2, rectX2, rectY2);
- fill(0);
- text(unitWidth + "'", 550, 195);
- }
- void controlEvent(ControlEvent theEvent) {
- println("got a control event from controller with id "+theEvent.getController().getId());
- // tried using this section, when I did a fill() or background it wouldnt show the rectangle updating with the slider,
- // only on release it would show the new rectangle.
- }
- void mousePressed() {
- fill(125);
- stroke(0);
- ellipse(mouseX, mouseY, 10, 10);
- }
1