G4P image toggle button being replaced

motmot
edited September 2016 in Library Questions

o/

Severe beginner question here:

I'm using G4P to create a second window. I have 3 GImageToggleButtons in that second window. During the sketch I scan a folder looking for incoming images and have a preview displayed above the toggles. Sometimes the incoming image preview replaces the off state of the toggle button.

I cant reproduce i consistently, sometimes it replaces the 2nd toggle, sometimes the third. Its not always the same image.

Thanks in advance.

Declared in setup()

  btnToggle0 = new GImageToggleButton(controlWindow, 20, controlPanelSizeY-120, "GUI/btn.png", 2);
  btnToggle0.tag = "large screen button";
  btnToggle1 = new GImageToggleButton(controlWindow, 100, controlPanelSizeY-120, "GUI/btn.png", 2);
  btnToggle1.tag = "small screen";
  btnToggle2 = new GImageToggleButton(controlWindow, 180, controlPanelSizeY-120, "GUI/btn.png", 2);
  btnToggle2.tag = "face";

code that calls the preview image in the control window

    if (getImage.endsWith("JPG")) {
      currentPreview = null;
      currentPreview = loadImage(getImage);
      currentPreview.resize(previewSizeX, previewSizeY);
    }
  }
  app.image(currentPreview, 20, 20);
Tagged:

Answers

  • The images used by the GImageToggleButton are set when the control is created. In this case you have 3 toggle switches all using the same tiled image (2 tiles). It is not possible to change the control's image after it has been created except by recreating the control.

    Based on the code you have provided I believe the error is somewhere else in your code and is not related to G4P.

  • Thank you for your response.

    Undoubtedly its an error on my behalf but I can for the life of me find it.

    I have had another error recently which seems to be connected. After run the sketch for a few minutes text I use on the control window becomes distorted and unreadable. This happens at the same moment that the images of the buttons get replaces.

    On further experimentation it is only the first tile of the button that gets replaced, the "switched state" tile works as intended.

  • I suspect that the problems with text and images on the controls is because you don't use the background(...) statement at the beginning of the draw() method.

  • Thank you for your response. I wish it was that simple. This is what the draw method looks like for the control window. All works as intended for a few minutes and then at some point loading a new image will cause the error.

    public void windowDraw(PApplet app, GWinData data) {
      app.background(0);
    
      if (previewIndex!=currentPreviewIndex) {
        String getImage2 = dirLoc + "/" + list[previewIndex];
        if (getImage2.endsWith("JPG")) {
          currentPreview = null;
          currentPreview = loadImage(getImage2);
          currentPreview.resize(previewSizeX, previewSizeY);
        }
      } 
    
      remaining(app);
    
      currentPreview.mask(mask);
      app.image(currentPreview, 20, 20);
      app.image(finalPrev,350,400);
      currentPreviewIndex = previewIndex;
     }
    

    The remaining() method displays some text on the control window.

    3.2.1 windows10 BTW

    Thanks again for taking the time to look at my blundering.

  • In line 4 you compare the previewIndex with the currentPreviewIndex and if they are different you attempt to load a new image. The attempt may or maynot succeeed but whatever the outcome both the previewIndex and currentPreviewIndex are unchanged so it attempts to load the same image on the next frame.

    Just noticed line 18, but you should ONLY do that if the image was successfully loaded.

    Try this

    public void windowDraw(PApplet app, GWinData data) {
      app.background(0);
    
      if (previewIndex != currentPreviewIndex) {
        String getImage2 = dirLoc + "/" + list[previewIndex];
        if (getImage2.endsWith("JPG")) {
          currentPreview = null;
          currentPreview = loadImage(getImage2);
          currentPreview.resize(previewSizeX, previewSizeY);
          // Image was loaded so change the current index
          currentPreviewIndex = previewIndex;
        }
        else {
          // image failed to load so change the preview index back because it is not valid
          previewIndex = currentPreviewIndex;
       }
      } 
    
      remaining(app);
    
      currentPreview.mask(mask);
      app.image(currentPreview, 20, 20);
      app.image(finalPrev,350,400);
     }
    
Sign In or Register to comment.