I’ve been trying to stick a PApplet in a JFrame and have it resize when the user changes the size of the JFrame, but the documentation, when it exists, is unclear. Here I am told to use
void setup() {
frame.setResizable(true);
}
void resize(int w, int h) {
super.resize(w,h);
frame.setSize(w,h);
}
but when I try that it seems frame is null, and I’m not clear on how to make sure that resize get’s called, anyway.
Has anyone gotten this to work?
EDIT: simplified code.
Here is some of my code, based on http://wiki.processing.org/w/Swing_JSliders:
public class MyPanel extends JPanel
{
public MyPanel()
{
//have tried both BoxLayout and BorderLayout
setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
//if, instead of a PApplet I use a JPanel, this resizes fine
// it's only when using a PApplet that it won't resize
//add our processing window
PApplet pa = new PApplet();
pa.init();
add(pa);
}
// create external JFrame
private static void createGui()
{
// create new JFrame
JFrame jf = new JFrame("test");
// this allows program to exit
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// You add things to the contentPane in a JFrame
jf.getContentPane().add(new MyPane());
// keep window from being resized
//jf.setResizable(false);
// size frame
jf.pack();
// make frame visible
jf.setVisible(true);
}
public static void main(String[] args)
{
// threadsafe way to create a Swing GUI
javax.swing.SwingUtilities.invokeLater(new Runnable() {
@Override
public void run()
{
createGui();
}
}
);
}
}
thanks, any help is greatly appreciated.
The answer is quite simple, actually. It seems that unless draw is being used the PApplet won’t get resized. It’s as simple as turning
into
or replacing it with a properly extended PApplet.
I just didn’t make my personal proof-of-concept detailed enough.