I want to create a window with height smaller than the minimum allowed by Processing. Is this possible?

Idk what else to say. Here’s my code, if that’s relevant.

float v = 5;
int winX = width/2;
int winY = 800;
boolean [] keys = new boolean[128];

import java.awt.Robot;

Robot robot;

void setup () {
  size(600, 25);
  try {
    robot = new Robot();
  } 
  catch (Throwable e) {
  }
}

void draw() {
  if (keyPressed) {

    if (keys['a']) {
      winX -= v;
    }

    if (keys['d']) {
      winX += v;
    }  
  }
  surface.setLocation(winX, winY);
}

void keyPressed() {
  println(key, int(key));
  if (key<128)
    keys[key] = true;
}

void keyReleased() {
  println(key, int(key));
  if (key<128)
    keys[key] = false;
}

Could try a JFrame.

import javax.swing.*;

void setup() {
  surface.setVisible(false); // Suppresses default window
  JFrame frame = new JFrame("JFrame Demo");
  frame.setBounds(500,300,300,35);
  frame.setVisible(true);  
}
1 Like

Accessing width before size() doesn’t work!

Dunno how to force the canvas having its height smaller than PSurface.MIN_WINDOW_HEIGHT, but this is how we can move the sketch canvas within the boundaries of displayWidth & displayHeight:

/**
 * Move Canvas Via Keys (v1.0.4)
 * GoToLoop (2023/Jan/30)
 *
 * https://Discourse.Processing.org/t/
 * i-want-to-create-a-window-with-height-smaller-than-the-minimum-allowed-
 * by-processing-is-this-possible/40755/3
 */

static final int KEYS = 'X', VEL = 10;
final byte[] keys = new byte[KEYS];

int winX, winY, wMax, hMax;

void setup() {
  size(100, 50);

  final int
    minW = PSurface.MIN_WINDOW_WIDTH, 
    minH = PSurface.MIN_WINDOW_HEIGHT, 

    winW = max(minW, width), 
    winH = max(minH, height);

  wMax = displayWidth  - winW;
  hMax = displayHeight - winH;

  winX = wMax >> 1;
  winY = hMax >> 1;

  println(wMax, hMax, displayWidth, displayHeight);
  println(minW, minH, winW, winH, width, height, ENTER);
}

void draw() {
  final int
    r = keys['D'] | keys[RIGHT], 
    l = keys['A'] | keys[LEFT], 
    d = keys['S'] | keys[DOWN], 
    u = keys['W'] | keys[UP];

  winX = constrain(winX + r - l, 0, wMax);
  winY = constrain(winY + d - u, 0, hMax);

  surface.setLocation(winX, winY);

  if (frameCount % 60 == 0)  background((color) random(#000000));
}

void keyPressed() {
  print(keyCode, key, TAB);
  setKey(keyCode, VEL);
}

void keyReleased() {
  setKey(keyCode, 0);
}

void setKey(final int idx, final int val) {
  if (idx < KEYS)  keys[abs(idx)] = (byte) val;
}
1 Like

this could work but i have no Idea how to use JFrames. could you explain to me how to use it (in a basic way)?

JFrame is a Swing component; it is what they call a ‘window’. You would then add other Swing components to this window. If you add very many you would need to use a separate thread, which is not difficult to do. You would have to do your own event handling of frame components and Processing’s draw() function would no longer work, because you would not be using the default Processing frame (window). I don’t know how many frame components your app needs; the posted example is basically just a window with a band running across it. Below is another very basic demo adding a button to a JFrame. There is a more extensive example of Swing components in the Gallery: https://discourse.processing.org/t/swing-components-in-default-processingwindow/35483 . In your case you would not be using the default Processing window as shown there, but be using an actual JFrame (adding components is the same).

import java.awt.event.*;
import javax.swing.*;

void setup() {
  surface.setVisible(false);
  JFrame frame = new JFrame("JFrame Demo");
  frame.setBounds(500,300,300,100);
  JButton btn = new JButton("Press me.");
  btn.setBounds(30,30,100,30);
  frame.add(btn);
  frame.setLayout(null); // Need this or btn takes up entire window
  frame.setVisible(true);
  ActionListener actionListener = new ActionListener() {
    public void actionPerformed(ActionEvent actionEvent) {
      println("JFrame btn hit.");
    }
  };
  btn.addActionListener(actionListener);
}
2 Likes

I’m cross-posting Python Mode (Jython) version of “Move Canvas Via Keys” here in addition to Reddit:

"""
 Move Canvas Via Keys (v1.0.0)
 GoToLoop (2023-Mar-08)

 https://Discourse.Processing.org/t/
 i-want-to-create-a-window-with-height-smaller-than-the-minimum-allowed-
 by-processing-is-this-possible/40755/6

 https://www.Reddit.com/r/processing/comments/11ltxhh/
 a_basic_code_works_with_java_option_but_doesnt/
"""

VEL = 10
D, A, S, W, KEYS = map(ord, 'DASWX')

from jarray import zeros
keys = zeros(KEYS, 'b') # Java fixed-size byte-datatype container
# keys = [0] * KEYS # Pythonic style container

def setup():
    size(100, 50)

    minW = PSurface.MIN_WINDOW_WIDTH
    minH = PSurface.MIN_WINDOW_HEIGHT

    winW = max(minW, width)
    winH = max(minH, height)

    global winX, winY, wMax, hMax

    wMax = displayWidth  - winW
    hMax = displayHeight - winH

    winX = wMax >> 1
    winY = hMax >> 1

    print wMax, hMax, displayWidth, displayHeight
    print minW, minH, winW, winH, width, height, ENTER


def draw():
    frameCount % 60 or background(int(random(PImage.ALPHA_MASK)))

    r = keys[D] | keys[RIGHT]
    l = keys[A] | keys[LEFT]
    d = keys[S] | keys[DOWN]
    u = keys[W] | keys[UP]

    global winX, winY

    winX = constrain(winX + r - l, 0, wMax)
    winY = constrain(winY + d - u, 0, hMax)

    this.surface.setLocation(winX, winY)


def keyPressed():
    print keyCode, key, TAB,
    setKey(keyCode, VEL)


def keyReleased(): setKey(keyCode, 0)

def setKey(idx, val):
    if idx < KEYS: keys[abs(idx)] = val
1 Like