Results 1 to 2 of 2
Thread: super.addNotify()
- 03-15-2012, 06:11 PM #1
Member
- Join Date
- Oct 2011
- Posts
- 2
- Rep Power
- 0
super.addNotify()
I'm reading Killer Game Programming in Java, however I'm confused about something, and that is how addNotify works exactly.
In an effort to understand, I did this:
The JFrame:Java Code:public class GamePanel extends JPanel implements Runnable { // some variables ....... public GamePanel() { System.out.println("Constructor started"); setBackground(Color.white); setPreferredSize(new Dimension(PWIDTH, PHEIGHT)); // create game components // .. System.out.println("Constructor ended"); } // END OF GAMEPANEL @Override public void addNotify() { System.out.println("addNotify started"); super.addNotify(); startGame(); System.out.println("addNotify ended"); } // some more stuff, not relevant }
The result was this:Java Code:public class Main extends JFrame { public Main() { System.out.println("Constructor main started"); setTitle("game"); setSize(500, 400); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); add(new GamePanel()); try { Thread.sleep(3000); System.out.println("setVisible getting called"); } catch (InterruptedException e) { e.printStackTrace(); } setVisible(true); try { System.out.println("setVisible ended"); Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("Constructor main ended"); } public static void main(String[] args) { new Main(); } }
Constructor main started
Constructor started
Constructor ended
(*Imagine a 3 second delay here*)
setVisible getting called
addNotify started
addNotify ended
setVisible ended
(*Imagine another 3 second delay*)
Constructor main ended
So it is clear addNotify gets called only when setVisible() of the JFrame is called, and that is desired I suppose. But why? GamePanel isn't an subclass of Main, is it? Maybe I'm just tired, but I dont get how this works exactlyLast edited by icedust; 03-15-2012 at 06:13 PM.
- 03-15-2012, 06:14 PM #2
Re: super.addNotify()
GamePanel is a subclass of JPanel. The JPanel is added to the JFrame, so the JPanel becomes visible when the JFrame becomes visible.
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
Similar Threads
-
super fun dog
By uc200511 in forum EntertainmentReplies: 1Last Post: 07-28-2012, 11:56 PM -
super
By diamonddragon in forum New To JavaReplies: 3Last Post: 01-29-2012, 10:49 PM -
calling variable using super super..
By Stephen Douglas in forum New To JavaReplies: 7Last Post: 08-16-2010, 06:12 AM -
super
By dj kourampies in forum New To JavaReplies: 5Last Post: 01-23-2009, 03:07 PM -
this() vs super()
By Hevonen in forum New To JavaReplies: 9Last Post: 12-04-2008, 01:43 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks