Check visible state

Is it possible to check the visibility state?

For example, I have this code:

void draw() {
  if (!isVisible()) {
    surface.setVisible(true);
  }
}

/**
 * @return true if visible, false otherwise
 */
boolean isVisible() {
  
}

What should the isVisible() method look like?

Answers

  • I am assuming if your sketch is not visible, then it is bc you set it so. In that case, I will suggest my code below.

    Kf

    boolean visState=true;
    
    void draw() {
    
      if (!isVisible()) {
        surface.setVisible(true);
      }
     else{ 
        surface.setVisible(false);
     }
    
     ///// OR     surface.setVisible(visState);
    }     
    
    boolean isVisible() {
       return visState;   //Redundant as you can access visState directly
    }
    
    //Mouse event changes vis state
    void mouseReleased(){
      visState= ! visState;
    }
    
  • Answer ✓

    Forgive me, maybe not quite correctly asked my question.

    But I already found the solution to this problem myself.

    Here it is:

    import processing.awt.PSurfaceAWT;
    
    void draw() {
      if (!isVisible()) {
        surface.setVisible(true);
      }
    }
     
    /**
     * @return true if visible, false otherwise
     */
    boolean isVisible() {
       return ((PSurfaceAWT.SmoothCanvas) surface.getNative()).getFrame().isVisible();
    }
    
    void mousePressed() {
      surface.setVisible(false);
    }
    
Sign In or Register to comment.