Results 1 to 4 of 4
  1. #1
    MBaricevic is offline Member
    Join Date
    Apr 2010
    Posts
    2
    Rep Power
    0

    Smile Animation inside JInternalFrame - Solved

    I'm not entirely new to programming or Java, but I tend to avoid GUI programming like the plague when possible because it just seems that I always run into trouble hah.

    I've been developing a mutating learning algorithm, which has been going totally fine, what I'm having trouble with now is graphically representing the critters that are employing the algorithm.

    I'm getting a Null Pointer Exception in this patch of code which is the graphical update loop inside of my DrawPanel class (which extends JPanel).

    More specifically the bug shows up in my drawScreen() method.

    Java Code:
    	public void drawScreen() {
    		Graphics2D g = (Graphics2D) this.getGraphics();
    
    		g.drawImage(buffer, 0, 0, this);
    
    		Toolkit.getDefaultToolkit().sync();
    		g.dispose();
    	}
    I've established that I'm getting the null pointer from the fact that getGraphics() returns a null. This only happens, however, when the DrawPanel is inside of a JInternalFrame, which is inside of a JDesktop. When I add the DrawPanel to a simple JFrame, it all works fine.

    I wanted to have a few JInternalFrames with data, settings, etc that would co-exist along side the JIInternalFrame which displayed the DrawPanel animation.

    Perhaps there is a better way to accomplish this, any suggestions?

    If all else fails, I'm simply planning on having multiple JFrames instead of multiple JInternalFrames.
    Last edited by MBaricevic; 04-13-2010 at 05:43 PM. Reason: Problem solved.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    My guess is that you're trying to get the Graphics object on a component that has not yet been rendered and so will be null. What are you calling getGraphics on? the JInternalFrame? If so, then you probably should change things a bit. I recommend doing your drawing passively within the paintComponent method of a JPanel that is held in the JInternalFrame's contentPane.

    Edit: then when you update your data, you call repaint on the JPanel. The JPanel's paintComponent method will then iterate through the data and draw the image. Or if you are updating a BufferedImage, then paintComponent will simply draw the image as you appear to be doing now.
    Last edited by Fubarable; 04-11-2010 at 02:18 AM.

  3. #3
    MBaricevic is offline Member
    Join Date
    Apr 2010
    Posts
    2
    Rep Power
    0

    Smile

    Oh wow, I didn't quite expect such an expedient reply. I left for the weekend and come back to discover you responded within 20 minutes of my original post! Thank you very much Fubarable!

    Your postulate was correct. I believe I was attempting to draw something which had not yet been rendered. My quick solution to the problem was to have the draw panel a thread with the loop checking for null before attempting to draw. This seems to have fixed my problem. I will however, try over-writing paintComponent and then calling repaint after updating because it sounds like this is proper GUI etiquette.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by MBaricevic View Post
    ...Thank you very much Fubarable!
    You're quite welcome!

    Your postulate was correct. I believe I was attempting to draw something which had not yet been rendered. My quick solution to the problem was to have the draw panel a thread with the loop checking for null before attempting to draw. This seems to have fixed my problem. I will however, try over-writing paintComponent and then calling repaint after updating because it sounds like this is proper GUI etiquette.
    Yep, your second plan is definitely the way to go.

Similar Threads

  1. How to reload JInternalFrame
    By chyrl in forum AWT / Swing
    Replies: 42
    Last Post: 04-16-2010, 02:29 PM
  2. How to refresh my JInternalFrame upon CUD
    By chyrl in forum AWT / Swing
    Replies: 2
    Last Post: 04-01-2010, 05:22 PM
  3. JInternalFrame Help
    By collin389 in forum New To Java
    Replies: 2
    Last Post: 03-16-2010, 01:23 AM
  4. JInternalFrame Help
    By collin389 in forum AWT / Swing
    Replies: 1
    Last Post: 03-15-2010, 10:24 PM
  5. Problem with JInternalFrame
    By hameem in forum AWT / Swing
    Replies: 1
    Last Post: 12-11-2008, 05:23 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •