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

**URL:** https://discourse.processing.org/t/i-want-to-create-a-window-with-height-smaller-than-the-minimum-allowed-by-processing-is-this-possible/40755
**Category:** Coding Questions
**Created:** [January 30, 2023, 2:43pm UTC](https://discourse.processing.org/t/i-want-to-create-a-window-with-height-smaller-than-the-minimum-allowed-by-processing-is-this-possible/40755 "2023-01-30T14:43:46Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![vinijoncrafts](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/vinijoncrafts/32/14306_2.png) [@vinijoncrafts](https://discourse.processing.org/u/vinijoncrafts)
#### Post date: [January 30, 2023, 2:43pm UTC](https://discourse.processing.org/t/i-want-to-create-a-window-with-height-smaller-than-the-minimum-allowed-by-processing-is-this-possible/40755/1 "2023-01-30T14:43:46Z")

</div>

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

```auto
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;
}

```

---

<div class="post-metadata">

### Author: ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)
#### Post date: [January 30, 2023, 7:22pm UTC](https://discourse.processing.org/t/i-want-to-create-a-window-with-height-smaller-than-the-minimum-allowed-by-processing-is-this-possible/40755/2 "2023-01-30T19:22:29Z")

</div>

Could try a JFrame.

```auto
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);  
}

```

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [January 30, 2023, 8:22pm UTC](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 "2023-01-30T20:22:11Z")

</div>

> [@vinijoncrafts](#):
>
> `int winX = width/2;`

Accessing [_width_](https://processing.org/reference/width.html) before [**size()**](https://processing.org/reference/size_.html) doesn’t work!

Dunno how to force the canvas having its [_height_](https://processing.org/reference/height.html) smaller than [PSurface.MIN\_WINDOW\_HEIGHT](http://processing.github.io/processing-javadocs/core/processing/core/PSurface.html#MIN_WINDOW_HEIGHT), but this is how we can move the sketch canvas within the boundaries of [_displayWidth_](https://processing.org/reference/displayWidth.html) & [_displayHeight_](https://processing.org/reference/displayHeight.html):

```auto
/**
 * 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;
}

```

---

<div class="post-metadata">

### Author: ![vinijoncrafts](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/vinijoncrafts/32/14306_2.png) [@vinijoncrafts](https://discourse.processing.org/u/vinijoncrafts)
#### Post date: [January 31, 2023, 2:25pm UTC](https://discourse.processing.org/t/i-want-to-create-a-window-with-height-smaller-than-the-minimum-allowed-by-processing-is-this-possible/40755/4 "2023-01-31T14:25:50Z")

</div>

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)?

---

<div class="post-metadata">

### Author: ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)
#### Post date: [January 31, 2023, 4:43pm UTC](https://discourse.processing.org/t/i-want-to-create-a-window-with-height-smaller-than-the-minimum-allowed-by-processing-is-this-possible/40755/5 "2023-01-31T16:43:10Z")

</div>

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](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).

```auto
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);
}

```

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [March 8, 2023, 4:48pm UTC](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 "2023-03-08T16:48:21Z")

</div>

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

> **[r/processing - A basic code works with Java option but doesn't with Python...](https://www.reddit.com/r/processing/comments/11ltxhh/a_basic_code_works_with_java_option_but_doesnt/)**
>
> 2 votes and 4 comments so far on Reddit

```auto
"""
 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

```
