We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpIntegration › Embed a Panel onto a PApplet
Page Index Toggle Pages: 1
Embed a Panel onto a PApplet (Read 2935 times)
Embed a Panel onto a PApplet
Apr 9th, 2009, 3:32pm
 
Edit: In case anyone stumbles across this; key input becomes troublesome because the GUI components steal focus from the PApplet. See post below for a global key handler.


I know about embedding a PApplet into a frame but I am wondering if there are any caveats to adding a Panel to a PApplet.

Aside from part of the display being covered.

This does work. Just curious if anyone knows of any troubles with it.

Code:

import javax.swing.JButton;
import javax.swing.UIManager;

int dx = 300, dy = 200;

void setup() {
size(640,480);

makeUI();

frameRate(30);
}

void mouseDragged() {
dx += mouseX - pmouseX;
dy += mouseY - pmouseY;
}

void draw() {
background(0);

rect(dx,dy,20,20);

}

void makeUI() {

try {
//Attempt to use the system look and feel
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

} catch (Exception er) {
//we're stuck with the default look and feel
println("Unable to load SystemLookAndFeel");
}


setLayout(null);//lets us specify where to put objects


Panel p = new Panel();
p.setBounds(0,0,width,50);
p.setBackground(new Color(32,32,32));


JButton jb = new JButton("Green");
jb.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {//button code
fill(0,255,0);
}
});


JButton jbr = new JButton("Red");
jbr.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {//button code
fill(255,0,0);
}
});


p.add(jb);
p.add(jbr);

add(p);
}
Re: Embed a Panel onto a PApplet
Reply #1 - Apr 20th, 2009, 2:23pm
 
If anyone is interested how to do global key capture:

-inside setup()-

Code:

KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventPostProcessor(new KeyEventPostProcessor() {

public boolean postProcessKeyEvent(KeyEvent e) {
if (e.getID() == KeyEvent.KEY_PRESSED) {

if (e.getKeyCode() == SHIFT) {
shft = true;
} else if (e.getKeyCode() == CONTROL) {
cntrl = true;
}// else if (some other key)...

} else if (e.getID() == KeyEvent.KEY_RELEASED) {

if (e.getKeyCode() == SHIFT) {
shft = false;
} else if (e.getKeyCode() == CONTROL) {
cntrl = false;
}// else if (some other key)...

}
return true;
}
}
);


Re: Embed a Panel onto a PApplet
Reply #2 - Apr 20th, 2009, 3:37pm
 
When I tried it with P3D and OPENGL the Panel failed to show.
Re: Embed a Panel onto a PApplet
Reply #3 - Apr 20th, 2009, 9:47pm
 
I see what you mean. P2D did not work either.

JAVA2D works. I'm guessing that is the default...
Re: Embed a Panel onto a PApplet
Reply #4 - Nov 19th, 2009, 5:32pm
 
Anyone find a solution for getting this to work with OpenGL?  Cry
Re: Embed a Panel onto a PApplet
Reply #5 - Nov 28th, 2009, 12:21pm
 
If there is a solution, it will be very complex. The OpenGL renderer is a native component, just like AWT. If you are using Swing, the OpenGL renderer (which is heayweight) will appear on top of the Swing component(s) (which are lightweight). I'm not sure about making lightweight components appear above heavyweight components... Sad
Re: Embed a Panel onto a PApplet
Reply #6 - Nov 28th, 2009, 12:27pm
 
I'd be fine with a heavyweight AWT panel.  I'm not stuck on using Swing components, but I am stuck on getting some Panel or Container to break through to the foreground in OpenGL.
Page Index Toggle Pages: 1