Center window on screen

edited November 2013 in How To...

Hy everybody, thanks for readning this question!

Is it possible to center the (presentation)-window on my screen, and if yes, how can I manage this?????

thanks mast

Answers

  • You can use the frame object in processing to edit stuff concerning the window. My code will move the frame to the center of the screen. the only problem is that the processing PDE sets the initial position of the frame every time you run it so if you call this in draw it will work but it will always move the window back to the center even if you move it away. There is a workaround however, you must set up a system to only move it to the center if it is only the first time this happens. My code simply has a Boolean to store if you have already centered the screen and f not it centers it in the draw loop. Again you cannot do this in setup because the PDE will override wherever you try to move it to.

    boolean centered;
    void setup()
    {
      size(400,400);
      centered = false;
    
    }
    void draw()
    {
      centerWindow();
    }
    
    void centerWindow()
    {
      if(frame != null && centered == false)
      {
        frame.setLocation(displayWidth/2-width/2,displayHeight/2-height/2);
        centered = true;
      }
    }
    
  • Weird setLocation() doesn't work inside setup()! X_X
    Anyways, I don't think frame is gonna be null ever! :P

  • Well yeah it probably wont ever be null but that's just the way the processing wiki showed to use stuff regarding the frame object so i thought there must be a reason. I think it doesn't work in setup because the PDE opens the window AFTER setup has been called but i might be wrong.

  • Thanks for your answers!!! I think this this one is a very good solution for my problem

    cheers mast

  • No problem were always glad to help!

Sign In or Register to comment.