Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In order to answer <strong>How to call the Frame methods from the PApplet?</strong>, I have modified your code snippet to bare minimum. In this modified version when the user click mouse button a <code>System.out</code> is fired.</p> <p>Now there are two ways in which you can access your <code>Frame</code> object. But before that let me state these two points:</p> <ul> <li>When you create a PApplet like <code>new ExampleFrame(new Menu());</code> and add it in your <code>JFrame</code> like this <code>add(app, BorderLayout.CENTER);</code> then a complex hierarchy of windows/panels are created. </li> </ul> <p><strong>Like this:</strong></p> <pre><code>javax.swing.JPanel javax.swing.JLayeredPane javax.swing.JRootPane test.ExampleFrame </code></pre> <ul> <li><code>PApplet</code> provides a public field for setting and accessing your frame object. And amazingly it is called <code>frame</code> :). You can set it before calling <code>app.init();</code></li> </ul> <p><strong>>>Code</strong></p> <p>** Checkout the comments in the code**</p> <p><strong>Modified ExampleFrame.java</strong></p> <pre><code>import java.awt.BorderLayout; import javax.swing.JFrame; import processing.core.PApplet; public class ExampleFrame extends JFrame { private static final long serialVersionUID = 4792534036194728580L; PApplet app; public ExampleFrame(PApplet emApp) { super("Ball Maze Game"); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setLocation(200, 200); app = emApp; setSize(615,438); setVisible(true); setLayout(new BorderLayout()); add(app, BorderLayout.CENTER); // Setting my frame object app.frame = this; app.init(); } // Sample Method public void sampleMethod(String msg) { System.out.println("I think '"+ msg +"' called me !!"); } } </code></pre> <p><strong>Modified Menu.java</strong></p> <pre><code>import java.awt.Container; import processing.core.PApplet; import processing.core.PImage; public class Menu extends PApplet { private static final long serialVersionUID = -6557167654705489372L; PImage background; static String tab = ""; //simple constructor public Menu() { } public void setup() { size(600, 400); smooth(); background = loadImage("C:/temp/background.jpg"); } public void draw() { image(background, 0, 0); } public void mousePressed() { Container p = getParent(); tab = ""; // FIRST WAY OF ACCESSING PARENT FRAME while(p != null) { //printParentTree(p); if(p instanceof ExampleFrame) { ExampleFrame myframe = (ExampleFrame)p; myframe.sampleMethod("First Way"); break; } p = p.getParent(); } // SECOND WAY OF ACCESSING PARENT FRAME if(frame != null &amp;&amp; (frame instanceof ExampleFrame)) { ExampleFrame myframe = (ExampleFrame)p; myframe.sampleMethod("Second Way"); } } void printParentTree(Container p) { System.out.println(tab+p.getClass().getName()); tab +='\t'; } } </code></pre> <p>Checkout the <code>public void mousePressed()</code> method.</p> <p>For completeness, I am also including Main.java.</p> <pre><code>public class Main { public static void main(String[] args){ new ExampleFrame(new Menu()); } } </code></pre> <p>Now to answer <strong>Remove all PApplets contents and load another PApplet in</strong></p> <p>Well I have not tested it. But you can add a <code>JPanel</code> to your <code>JApplet</code> and do all your drawing on that i.e creating child controls etc. When feel like redrawing then call <code>JPanel.removeAll()</code>. Which as per javadoc: </p> <blockquote> <p>Removes all the components from this container. This method also notifies the layout manager to remove the components from this container's layout via the removeLayoutComponent method.</p> </blockquote> <p>After this call <code>repaint</code> on the <code>JPanel</code>. Try it out, it might work :).</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload