Limiting window resize to a certain minimum

I'm trying to provide a minimum window size for a window that can be resized. Currently, I'm achieving this with the following:

void setup() {
  size(400, 300);
  pixelDensity(displayDensity());
  frameRate(20);
  surface.setResizable(true);
}

void draw() {
  // Deal with window size:
  if (width < 400) {
    surface.setSize(400, height);
  }
  if (height < 300) {
    surface.setSize(width, 300);
  }
}

While this does work, it's gittery as you'd expect. If you drag the corner or edge of the window to make it smaller than the lower limit, it looks very unpleasant.

I've done plenty of googling and there are lots of examples of how to do this in Processing 2 and oder, perhaps the cleanest uses frame.setMinimumSize(); but of course in P3 this is no longer available - there is no similar PSurface method.

Does anyone know how this 'should' be done now?

Answers

  • edited March 2016
    // forum.Processing.org/two/discussion/15398/limiting-window-resize-to-a-certain-minimum
    // GitHub.com/processing/processing/blob/master/core/src/processing/awt/PSurfaceAWT.java
    // docs.Oracle.com/javase/8/docs/api/java/awt/Window.html#setMinimumSize-java.awt.Dimension-
    
    // GoToLoop (2016-Mar-10)
    
    import processing.awt.PSurfaceAWT.SmoothCanvas;
    import javax.swing.JFrame;
    import java.awt.Dimension;
    
    final SmoothCanvas sc = (SmoothCanvas) getSurface().getNative();
    final JFrame jf = (JFrame) sc.getFrame();
    final Dimension d = new Dimension(400, 300);
    
    jf.setMinimumSize(d);
    println(jf.getMinimumSize());
    
    size(200, 150);
    noLoop();
    background((color) random(#000000));
    
    getSurface().setResizable(true);
    
  • edited March 2016

    Thanks for the code, but I'm getting: No library found for processing.awt.PSurfaceAWT

  • Answer ✓

    No library found for processing.awt.PSurfaceAWT

    It is a warning rather than an error so can ignore this message.

    The following code is GoTOLoop's code but done with setup and draw methods

    import processing.awt.PSurfaceAWT.SmoothCanvas;
    import javax.swing.JFrame;
    import java.awt.Dimension;
    
    int col;
    
    void setup() {
      size(200, 150);
      SmoothCanvas sc = (SmoothCanvas) getSurface().getNative();
      JFrame jf = (JFrame) sc.getFrame();
      Dimension d = new Dimension(400, 300);
      jf.setMinimumSize(d);
      println(jf.getMinimumSize());
      getSurface().setResizable(true);
    }
    
    void draw() {
      if (frameCount % 300 == 1)
        col = (int)random(0xff000000);
      background(col);
    }
    
  • Thanks to you both. I don't know what I did wrong when I threw GoTOLoop's code in, but I must have missed something.

    Cheers

Sign In or Register to comment.