I have tried this method and solved some problems, but I’m still struggling with some issues. The keyboard events are handled by the current window, which is what I expect, and the windows seem to be stopping and hiding correctly, but now I have the problem of switching between windows.
In my code, I switch between 2 windows by pressing ‘1’ or ‘2’ in the main sketch, but since I want all windows except the current one to be hidden and stopped, I need a way to switch between them if im currently in window 2, for example. Bare in mind that the final implementation will have at least 6 windows, so I’m thinking I need some kind of “counter” that knows which window I’m in right now and which one’s next.
Here’s my code:
Main:
public void settings() {
fullScreen();
}
void setup() {
frameRate(20);
nextWindow=0;
logo = loadImage("adamas.jpg");
imageMode(CENTER);
image(logo,displayWidth/2,displayHeight/2,logo.width/2,logo.height/2);
// this.hide();
win1 = new PWindow("RELOJ");
//win1.show();
cameras = new PCamera("CAMARAS");
//cameras.hide();
}
void draw(){
background(0);
if (millis()<2000) image(logo,displayWidth/2,displayHeight/2,logo.width/2,logo.height/2);
}
void keyPressed(){
if (key == 'h'){
switchHDMI();
}
if (key == 'u'){
switchUSB();
}
if (key =='1'){
//nextWindow = (nextWindow+1)%2;
cameras.hide();
win1.show();
}
if (key == '2'){
win1.hide();
cameras.show();
}
}
void switchHDMI(){
// ... //
}
void switchUSB(){
// ... //
}
public void hide(){
surface.setVisible(false);
}
@Override void exit(){
win1.dispose();
win1 = null;
cameras.dispose();
cameras = null;
super.exit();
}
PCamera class:
public class PCamera extends PApplet {
PCamera(String name) {
super();
PApplet.runSketch(new String[] {this.getClass().getSimpleName()}, this);
surface.setTitle(name);
surface.setLocation(0,0);
}
public boolean work = false;
IPCapture cam1, cam2, cam3, cam4, cam5, cam6;
int cont;
void settings() {
//size(640,480);
fullScreen();
frameRate=20;
}
void setup() {
setDefaultClosePolicy(this, false);
iniciarCamaras();
}
void draw() {
background(0);
if(work){
switch(this.cont){
case 0:
if (cam1.isAvailable()) {
cam1.read();
}
if (cam2.isAvailable()) {
cam2.read();
}
if (cam3.isAvailable()) {
cam3.read();
}
if (cam4.isAvailable()) {
cam4.read();
}
if (cam5.isAvailable()) {
cam5.read();
}
if (cam6.isAvailable()) {
cam6.read();
}
image(cam1,0,0, 640, 480);
image(cam2,displayWidth/3,0, 640, 480);
image(cam3,2*displayWidth/3,0, 640, 480);
image(cam4,0,480, 640, 480);
image(cam5,displayWidth/3,480, 640, 480);
image(cam6,2*displayWidth/3,480, 640, 480);
break;
case 1:
if (cam1.isAvailable()) {
cam1.read();
image(cam1,0,0, displayWidth, displayHeight);
} break;
case 2:
if (cam2.isAvailable()) {
cam2.read();
image(cam2,0,0, displayWidth, displayHeight);
} break;
case 3:
if (cam3.isAvailable()) {
cam3.read();
image(cam3,0,0, displayWidth, displayHeight);
} break;
case 4:
if (cam4.isAvailable()) {
cam4.read();
image(cam4,0,0, displayWidth, displayHeight);
} break;
case 5:
if (cam5.isAvailable()) {
cam5.read();
image(cam5,0,0, displayWidth, displayHeight);
} break;
case 6:
if (cam6.isAvailable()) {
cam6.read();
image(cam6,0,0, displayWidth, displayHeight);
} break;
}
}
}
void keyPressed() {
if (key == 's') {
// Changes camera
}
}
if (key == 'n'){
work = false;
println(" stop work and hide ");
this.hide();
}
}
void mouseClicked() {
// ... //
}
void iniciarCamaras(){
// ... //
}
void pararCamaras(){
// ... //
}
void exit(){
dispose();
}
public void show(){
work = true;
surface.setVisible(true);
}
public void hide(){
//pararCamaras();
work = false;
surface.setVisible(false);
}
final void setDefaultClosePolicy(PApplet pa, boolean keepOpen){
final Object surf = pa.getSurface().getNative();
final PGraphics canvas = pa.getGraphics();
if (canvas.isGL()) {
final com.jogamp.newt.Window w = (com.jogamp.newt.Window) surf;
for (com.jogamp.newt.event.WindowListener wl : w.getWindowListeners())
if (wl.toString().startsWith("processing.opengl.PSurfaceJOGL"))
w.removeWindowListener(wl);
w.setDefaultCloseOperation(keepOpen?
com.jogamp.nativewindow.WindowClosingProtocol.WindowClosingMode
.DO_NOTHING_ON_CLOSE :
com.jogamp.nativewindow.WindowClosingProtocol.WindowClosingMode
.DISPOSE_ON_CLOSE);
} else if (canvas instanceof processing.awt.PGraphicsJava2D) {
final javax.swing.JFrame f = (javax.swing.JFrame)
((processing.awt.PSurfaceAWT.SmoothCanvas) surf).getFrame();
for (java.awt.event.WindowListener wl : f.getWindowListeners())
if (wl.toString().startsWith("processing.awt.PSurfaceAWT"))
f.removeWindowListener(wl);
f.setDefaultCloseOperation(keepOpen?
f.DO_NOTHING_ON_CLOSE : f.DISPOSE_ON_CLOSE);
}
}
PWindow class:
class PWindow extends PApplet {
PWindow(String name) {
super();
PApplet.runSketch(new String[] {this.getClass().getSimpleName()}, this);
surface.setTitle(name);
surface.setLocation(0,0);
}
void settings() {
//size(displayWidth,displayHeight);
fullScreen();
}
void setup() {
frameRate(20);
setDefaultClosePolicy(this, false);
inicializa_pantalla();
delay(1000);
}
void draw() {
if(work){
animacion_variables();
dibuja_pantalla();
dibuja_menu(menu);
}
}
void inicializa_pantalla() {
// ... //
}
void visualiza_textos_pantalla(){
// ... //
}
void visualiza_imagenes_pantalla(){
// ... //
}
void repintaFondo(int x, int y, int w, int h){
// ... //
}
void dibuja_pantalla() {
visualiza_textos_pantalla();
visualiza_imagenes_pantalla();
}
void dibuja_menu(boolean menu){
// ... //
}
}
void mousePressed(){
tiempo = millis();
println(tiempo);
}
long tiempoActivado;
void mouseReleased(){
if(millis()-tiempo>1500){
tiempoActivado = millis();
menu = true;
}
}
void keyPressed(){
if (key == 'n'){
work = false;
println(" stop work and hide ");
this.hide();
}
}
boolean hanPasadoXSegundos(){
if(millis()-tiempoActivado>3000){
println("devuelvo falso");
return false;
}else
return true;
}
void handleToggleButtonEvents(GImageToggleButton button, GEvent event){
}
public void show(){
work = true;
surface.setVisible(true);
}
public void hide(){
work = false;
surface.setVisible(false);
}
void exit(){
dispose();
}
final void setDefaultClosePolicy(PApplet pa, boolean keepOpen){
final Object surf = pa.getSurface().getNative();
final PGraphics canvas = pa.getGraphics();
if (canvas.isGL()) {
final com.jogamp.newt.Window w = (com.jogamp.newt.Window) surf;
for (com.jogamp.newt.event.WindowListener wl : w.getWindowListeners())
if (wl.toString().startsWith("processing.opengl.PSurfaceJOGL"))
w.removeWindowListener(wl);
w.setDefaultCloseOperation(keepOpen?
com.jogamp.nativewindow.WindowClosingProtocol.WindowClosingMode
.DO_NOTHING_ON_CLOSE :
com.jogamp.nativewindow.WindowClosingProtocol.WindowClosingMode
.DISPOSE_ON_CLOSE);
} else if (canvas instanceof processing.awt.PGraphicsJava2D) {
final javax.swing.JFrame f = (javax.swing.JFrame)
((processing.awt.PSurfaceAWT.SmoothCanvas) surf).getFrame();
for (java.awt.event.WindowListener wl : f.getWindowListeners())
if (wl.toString().startsWith("processing.awt.PSurfaceAWT"))
f.removeWindowListener(wl);
f.setDefaultCloseOperation(keepOpen?
f.DO_NOTHING_ON_CLOSE : f.DISPOSE_ON_CLOSE);
}
}
}