Frame for P3D Renderer

Does anyone know how to get the frame of a Processing window which uses the P3D renderer?

void setup() {
  size(400, 400, P3D);
  // frame = ??
}
// P2D, P3D & OPENGL
import com.jogamp.newt.Window;
import com.jogamp.newt.event.WindowAdapter;
import com.jogamp.newt.event.WindowEvent;
1 Like

P3D is OpenGL by a different name so the frame is in fact an OpenGL window you can get a reference to it like this -

import com.jogamp.newt.opengl.GLWindow;

void setup() {
  size(400, 400, P3D);
  com.jogamp.newt.opengl.GLWindow frame =
    (com.jogamp.newt.opengl.GLWindow) surface.getNative();
    
    println(frame);
}

The GLWindow class is documented here

2 Likes

Thanks for the help. Much appreciated.

It appears you are trying to make the frame invisible but there is some ambiguity.

Are you trying to make

  1. the whole window invisible invisible, or
  2. make the window un-decorated i.e. without the window border?

If (1) then the following code will make the window invisible if the v key is typed

com.jogamp.newt.opengl.GLWindow frame;
boolean frameVisible  = true;

void setup() {
  size(400, 400, P3D);
  frame = (com.jogamp.newt.opengl.GLWindow) surface.getNative();
  println(frame);
}

void keyPressed() {
  if (key == 'v'){
    frameVisible = !frameVisible;
    frame.setVisible(frameVisible);
  }
}

void draw(){
  background(96);
}

Note that when the window is made invisible it halts execution and will no longer respond to events so it can’t make itself visible again. It explains why we can’t make it invisible in setup because Processing creates and initializes the window when executing the size() method, making the window invisible stops Processing part way through setup so it hangs up.

If (2) then AFAIK both JFrame and GLWindow support a method called setUndecorated(true) but this can only be called before the frame/window becomes visible for the first time, after that a fatal exception is thrown.

I can not find a way of accessing the frame/window created by Processing before first visibility but other forum members might.

2 Likes

Thanks for all of your work on this. I was trying to find a solution for another thread:https://discourse.processing.org/t/implementing-a-processing-app-inside-another-one/44920/3
and my initial thought was to toggle the slot machine window on and off. That works ok with the default Processing window, but not with a P3D renderer. I thought my problem was not having the frame but I could never get it to work so I abandoned the idea and instead used another approach with the command line as a possible solution. I wish there was an opengl control that could be placed in a default window for drawing with Processing methods, but I don’t think such a thing exists in java.

I have seen that discussion and I don’t believe there is a realistic way of a running sketch (main-sketch) launching and running another sketch so that the second-sketch-display appears in the display of the main-sketch.

On the other hand I believe that with good design and forward planning this could easily be achieved using p5.js :smile:

1 Like

It could possibly be done with java overlays, but the SlotMachine uses too much opengl code using Processing methods for that to be an option. Furthermore, the SlotMachine window is huge and is never going to fit in the default window without going fullscreen.

1 Like

Next Question:
Do you know how to get just the canvas? I want to drop it down to make room for a few controls at the top as shown below using a GLCanvas with a JFrame. Would like to do something similar with a Processing window using a P3D renderer.

Addendum:
The following source code will get the canvas of a P3D window, but it has to be built offscreen otherwise, the canvas is null. Bounds do not appear to be honored (canvas reported as invalid).

//https://processing.github.io/processing-javadocs/core/index.html?processing/opengl/PSurfaceJOGL.html

import java.awt.*;

processing.opengl.PSurfaceJOGL mySurface;
java.awt.Component canvas;

int _wndW = 400;
int _wndH = 450;

void setup() {
  size(_wndW, _wndH, P3D);
  mySurface = (processing.opengl.PSurfaceJOGL) surface;
  mySurface.setLocation(100,100);
  mySurface.setTitle("P3D Canvas");
  mySurface.initOffscreen(this);  // Need this
  canvas = (java.awt.Component)mySurface.getComponent();
  canvas.setBounds(0,50,_wndW,_wndH - 50);
  canvas.setBackground(new Color(0, 0, 255));
  println("mySurface = ",mySurface);
  println("=============================================");
  println("canvas = ",canvas);
}

Summary:
The P3D renderer uses a GLWindow with a NewtCanvasAWT and does not have the characteristics of a JFrame with Swing components which can be used with the default renderer. So far I have been unable to add any component to a GLWindow. A JFrame with a GLCanvas will allow other components.

1 Like

I wouldn’t recommend it.

When you run your program it is hidden because the size is 1x1 pixel, but being the last application launched it has ‘focus’ so will respond to events.

Now hit v to increase its size and bingo no problem not hit x to shrink it. Now click on any other application or icon and the sketch loses focus and no longer receives events, so the sketch cannot resized remains shrunk until the sketch is halted.

1 Like

That’s a serious problem with the demo. There should be a way to regain focus, but I can’t get .requestFocus() to work. To avoid confusion in the future I’m going to take it down. Thanks for making us aware.