loading images in G4P windows

edited December 2013 in Library Questions

Hello. I am new to processing and want to start with a simple program. This program should display images in seperate windows with seperate window sizes. I also need a notification, if a window is closed/opened. I want to use the G4P library, but if I use the background image I always get the message, that the image size must be the same size as the application. How could I check if a window is closed/opened? I want to display a warning image if a value has reached, but it only have to display again, if the window is closed. I think its easy, but I dont know how. I hope someone could help me. Thanks for helping.

Answers

  • edited December 2013

    So what's the question? How to check if a window is opened/closed?

    In that case, you would use frame.getExtendedState(). To show on your example it would work like this:

    void draw() {
      if (frame.getExtendedState() == 1) { //This checks if it is minimized
        println("This window is minimized.");
      } else if(frame.getExtendedState() == 0) { //This checks if it is not.
        println("This window is normal.");
      }
    }
    

    Just note that this must be done in draw(), or some other looping function.

    -- MenteCode.

  • I want to open a second window to the sketch window. And the second window should display an image. I started with G4P library and tryed the constructor with a background image, but the image have a different size of the sketch window and doesn't work. How can I display an image in the second window and I need to know, if this second window with the image is closed?

  • If you use the statement

    `background(image);'

    then the image MUST be the same size as the sketch/window display area. This is a Processing warning and nothing to do with G4P.

    If you want to use the background statement with a GWindow then you have to use the constructor that takes a PImage object so it can create the window with a display size that is the same as the image.

    If you simply want to display an image in an existing GWindow then you should not use the background statement.

    If we have created a global GWindow reference called window with this statement

    GWindow window;
    

    and in setup created the window with these statements

    window = new GWindow(this, "Window title", 0, 0, 240, 120, false,  JAVA2D);
    window.addDrawHandler(this, "win_draw");
    

    then in window's draw method we can display an image like this

    synchronized public void win_draw(GWinApplet appc, GWinData data) {
      appc.background(0); // black background
      appc.image(img, 0, 0);
    }
    

    where img is a preloaded PImage.

    To check whether this GWindow object is open or closed then use

    boolean stillOpen = G4P.getOpenWindowsAsList().contains(window);
    
  • Thanks for the helping. Is it possible not to draw the window in the setup but at a point in the code (in draw() function)? I want to open a seperate window, if a variable reached a certain value. If I creat the window in setup, the window shows at the start of the sketch. An if I try the method G4P.getOpenWindowsAsList().contains(window); I get the following error message: The method getOpenWindowsAsList(ArrayList) in the type G4P is not applicable for the arguments () What is my mistake? Thanks for your helping.

  • You can attempt to create the GWindow inside draw() but it may cause problems - I have not tried it myself.

    If you create the window in setup and make it invisible, then when your variable reaches the appropriate value then make it visible.

    Sorry I made a slight mistake in the code to test whether a window is open it should have been

    boolean stillOpen = G4P.getOpenWindowsAsList(null).contains(window);
    

    Anyway the following example makes the GWindow in setup and makes it invisible. When the sketch runs the window will remain invisible for 10 seconds. Once visible click on the windows close icon and it will be reported that the window is closed.

    import g4p_controls.*;
    
    GWindow window;
    int startTime;
    
    public void setup() {
      size(480, 320, JAVA2D);
      createGUI();
      window.setVisible(false);
      fill(0);
      startTime = millis();
    }
    
    public void draw() {
      background(200, 200, 255);
      if (G4P.getOpenWindowsAsList(null).contains(window)) {
        // Make the window visible after 10 seconds
        int showTime = 10000 - millis() + startTime;
        if (showTime <= 0)
          window.setVisible(true);
        text("Window is OPEN", 20, 20);
        if (!window.isVisible()) {
          text("Will be come visible in " + showTime + " milliseconds", 20, 40);
        }
      }
      else {
        text("Window is CLOSED", 20, 20);
      }
    }
    
    synchronized public void win_draw(GWinApplet appc, GWinData data) {
      appc.background(255, 220, 220);
    }
    
    // Create all the GUI controls. 
     public void createGUI() {
      G4P.messagesEnabled(false);
      G4P.setGlobalColorScheme(GCScheme.BLUE_SCHEME);
      G4P.setCursor(ARROW);
      if (frame != null)
        frame.setTitle("Sketch Window");
      window = new GWindow(this, "Window title", 0, 0, 240, 120, false, JAVA2D);
      window.setActionOnClose(G4P.CLOSE_WINDOW);
      window.addDrawHandler(this, "win_draw");
    }
    
  • Thank for you help. It works perfectly, but now I want to add a button to a GWindow. If the button is pressed, the window should become invisible. I can create a button, but how can I add it to the separate window and not to the mainsketch. window.add(button) does not work.

  • Thanks alot I figured that out. I have to give the constructor of the button the corresponding window in which the button should placed.

Sign In or Register to comment.