I have modified your code snippet to bare minimum. In this modified version when the user click mouse button a System. Out is fired.
I have modified your code snippet to bare minimum. In this modified version when the user click mouse button a System. Out is fired.
Now there are two ways in which you can access your Frame object. But before that let me state these two points: When you create a PApplet like new ExampleFrame(new Menu()); and add it in your JFrame like this add(app, BorderLayout. CENTER); then a complex hierarchy of windows/panels are created.
Like this: javax.swing. JPanel javax.swing. JLayeredPane javax.swing.
JRootPane test. ExampleFrame PApplet provides a public field for setting and accessing your frame object. And amazingly it is called frame :).
You can set it before calling app.init(); >>Code ** Checkout the comments in the code** Modified ExampleFrame. Java 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!"); } } Modified Menu. Java 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 && (frame instanceof ExampleFrame)) { ExampleFrame myframe = (ExampleFrame)p; myframe. SampleMethod("Second Way"); } } void printParentTree(Container p) { System.out. Println(tab+p.getClass().getName()); tab +='\t'; } } Checkout the public void mousePressed() method.
For completeness, I am also including Main.java. Public class Main { public static void main(String args){ new ExampleFrame(new Menu()); } } Now to answer Remove all PApplets contents and load another PApplet in Well I have not tested it. But you can add a JPanel to your JApplet and do all your drawing on that i.
E creating child controls etc.When feel like redrawing then call JPanel.removeAll(). Which as per javadoc: 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.
After this call repaint on the JPanel. Try it out, it might work :).
1 In fairness, note that this question relates to Processing. Good luck on the bounty; I'm sitting this one out. – trashgod Apr 27 at 3:03 2 @trashgod: Yup I knew that.
Even requested for the original processing code :O. In absence of that I just stripped down the presented snippet and used it for answer. Anyway thanks :) – Favonius Apr 27 at 4:23.
What needs to happen when mousePressed == ebtn is all the stuff in the Frame will be removed and a ghscores Screen will be loaded The demo. Below of a nested CardLayout adds an ActionListener instead of a MouseListener. It reacts to both mouse and keyboard input.
There are a multitude of other ways to include more than one GUI element in the same screen space. Off the top of my head, JTabbedPane, JSplitPane, JDesktopPane/JInternalFrame, popping the high scores in a JDialog or JOptionPane.. Screenshots CardLayoutDemo. Java import java.awt.
*; import java.awt.event. *; import javax.swing. *; class CardLayoutDemo { public static void main(String args) { Runnable r = new Runnable () { public void run() { final JRadioButton game = new JRadioButton("Game", true); JRadioButton highScores = new JRadioButton(" ButtonGroup bg = new ButtonGroup(); bg.
Add( game ); bg. Add( highScores ); JPanel buttons = new JPanel(new FlowLayout(FlowLayout. CENTER, 5, 5)); buttons.
Add( game ); buttons. Add( highScores ); JPanel gui = new JPanel(new BorderLayout(5,5)); gui. Add(buttons, BorderLayout.
SOUTH); final CardLayout cl = new CardLayout(); final JPanel cards = new JPanel(cl); gui. Add(cards); cards. Add(new JLabel("Level 1"), "game"); cards.
Add(new JLabel(" "scores"); ActionListener al = new ActionListener(){ public void actionPerformed(ActionEvent ae) { if (game.isSelected()) { cl. Show(cards, "game"); } else { cl. Show(cards, "scores"); } } }; game.
AddActionListener(al); highScores. AddActionListener(al); JOptionPane. ShowMessageDialog(null, gui); } }; SwingUtilities.
InvokeLater(r); } }.
1 +1 good example. See also these CardLayout examples. – trashgod Apr 27 at 2:34 I keep telling myself I should check Rob's site (& a limited group of other sites, including yours) before drafting code.
But it's such fun. ;) – Andrew Thompson Apr 27 at 2:46 1 Yes, I am completely shameless in citing yours and Rob's sites; feel free to link to mine. :-) Note Processing, and good luck on the bounty; I'm sitting this one out.
– trashgod Apr 27 at 3:02 1 Thanks for the link. That clears up a few things. I had thought 'Processing' was meant to be (the generic word) 'processing'!
– Andrew Thompson Apr 27 at 3:14 Apolgies. I probably should have explained it was an external library. – OVERTONE Apr 277 at 12:10.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.