Search code examples
c++imagefilesliderjuce

Can I change the image displayed on screen with a slider?


I want to use the value output by this slider to change the image which is loaded into the img variable. The paint function is called whenever the slider is moved.

The slider's value is 0 - 2. The second if statement tests as correct when the slider is > 1, however, the image does not change. Without the if statements I can switch the image manually, otherwise it isn't doing what I expect.

 void ModelGUI::sliderValueChanged(Slider* slider)
{
  rotate.getValue();
  DBG(rotate.getValue());

}
void ModelGUI::paint (Graphics& g)
{
    DBG("PAINT");
    g.setColour(Colours::red);

    if(rotate.getValue() < 1)
    {
        img = ImageFileFormat::loadFrom(f);
    }
    if(rotate.getValue() > 1)
    {
        img = ImageFileFormat::loadFrom(f2);
    }
    g.drawImage(img, model);

    g.drawEllipse(225, 230, 2, 2, 2);
    g.drawEllipse(240, 240, 2, 2, 2);
    g.drawEllipse(245, 275, 2, 2, 2);
}

Why is this happening? Cheers


Solution

  • just a wild guess but shouldn't you force repainting in the sliderValueChanged event somehow? I do not know juce but I would expect one of these:

    g.Repaint();
    g.Refresh();
    g.Update();
    

    as changing slider is probably just repainting the slider itself and not updating the rest of your GUI. The g should be your Graphics object of coarse.

    If the repaint is really time consuming then I usually do something like this:

    bool _redraw=false; // some global variable (or local but accessible to your GUI/Form/Canvas whatever)
    
    void any_event()
     {
     // here event stuff code
     if (need_to_repaint) _redraw=true; // for example you change something hit a special key that changes something etc ...
     }
    
    void timer() // any timer with target max fps interval
     {
     if (_redraw())
      { 
      _redraw=false;
      // here force repaint or call the repaint directly
      }
     }
    

    This way the repaint will not slow down other events too much. For example If I use zoom on wheel and I am rendering a lot of stuff the wheel could feel slagish if repainted on each change of wheel. This will repaint only with timer period which should be fine if set right.

    The usual events that need such handling are zoom, mouse move/edit and window/view resize as they can create a lot of event calls per second ...