javaprocessingcontrol-p5

How to prevent resizing of a panel in Java


I have already applied the surface.setResizable (false) method; How to do the same with the panel? I've been like this:

stage.setMinWidth (w);
stage.setMinHeight (h);
stage.setWidth (w);
stage.setHeight (h);
stage.setMaxWidth (w);
stage.setMaxHeight (h);

pane.setMaxSize (700, 420);
pane.setPrefSize (700, 420);
pane.setMinSize (700, 420);

And another little question here! How to make the title centered?

surface.setTitle ("HELLO WORLD");

enter image description here

enter image description here


Solution

  • There might be an issue making the GLWindow the P3D renderer in Processing resizable. From the little information provided it appears there's no need to use P3D at all. Use the default (JAVA2D) renderer and window will not be resizable: it's the default.

    Regarding the little question I have little question too: why ? :)

    In terms of usability ideally you want your app to be consistent.

    One way to centre the text, if you really really really want to is to prepend some spaces before "HELLO WORLD". You will need to workout how many pixels to move the string based on the system font (which on each OS and user preferences may differ) and it would be easier if it's a monospaced font (which in most cases it isn't). A lot of things to consider, a lot that can go wrong if you want to nail it pixel perfect and what would be that gain from all that effort ?

    Here's a test sketch regardless:

    void setup(){
      size(300,300, JAVA2D);
      background(0,192,0);
      // don't need this with JAVA 2D: it's the default
      //surface.setResizable(false);
      
      String title = "Hello World";
      // estimated char width: assumes font is monospaced (Arial/Verdana it isn't)
      int charWidth = 6;
      // calculate left margin
      int leftMargin = (width - (title.length() * charWidth)) / 2;
      // calculate the number of chars to prepent
      int chars = leftMargin / charWidth;
      // prepend spaces to move text to the right
      for(int i = 0 ; i < chars; i++){
        title = " " + title;
      }
      
      surface.setTitle(title); 
    }
    

    You can even animate it you want:

    void draw(){
      String title = "->";
      for(int i = 0 ; i < (int)map(sin(frameCount * 0.1), -1, 1, 0, 21); i++){
        title = "-" + title;
      }
      surface.setTitle(title);
    }
    

    Is it really worth the effort and wasting CPU cycles ? Probably not :)