Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (408)
games submitted by our members
Games in WIP (293)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  repaint in a jpanel,big problem  (Read 610 times)
0 Members and 1 Guest are viewing this topic.
Offline zulo

Junior Member




Java games rock!


« Posted 2005-06-14 23:31:06 »

Hi

My application creates a JFrame, and it adds my custom class that extends JPanel like this:

sceneEditorJFrame.getContentPane().add(new SceneTegnebrett(editorManager));



This works ok, but the problem is that this custom class, SceneTegneBrett wont repaint itself unless I use the mouse on the JFrame and resize it.

What is supposed to happen is, I pick an image with a FileDialog, and when this is done the "editorManager" object notifies the JPanel with an event. I know this works because I use System.out.println to see if the program gets into that method, and it does. The problem is that when that happens i want the JPanel to repaint itself with the new image. Even though i am calling repaint(); when it happens it doesnt reach the paintComponent(Graphics g) method. But when I resize the JFrame the paintComponent method is called and the JPanel paints itself with the new image.

Here is the code for the custom JPanel:

public class SceneTegnebrett extends JPanel implements SceneChangedEventListener, BackgroundChangedEventListener {
      
   EditorManager editorManager;
   /**
    * @param scene
    */
   public SceneTegnebrett(EditorManager em) {
      editorManager = em;      
      editorManager.addSceneChangedEventListener(this);
      
      editorManager.getCurrentEditingScene().addBackgroundChangedEventListener(this);
   }
      
   protected void paintComponent(Graphics g){
      super.paintComponent(g);
      g.drawRect(0,0,20,20);
      g.drawImage(editorManager.getCurrentEditingScene().getBackground(),0,0,null);
                  System.out.println("paintComponent()");
   }
         
   /* (non-Javadoc)
    * @see com.esedit.manager.event.scenegui.SceneChangedEventListener#sceneChanged(com.esedit.manager.event.scenegui.SceneChangedEvent)
    */
   public void sceneChanged(SceneChangedEvent evt) {
      System.out.println("scenechanged event");
      
      
   }
 
   /* (non-Javadoc)
    * @see com.eventyrskole.spillmotor.event.BackgroundChangedEventListener#backgroundChanged(com.eventyrskole.spillmotor.event.BackgroundChangedEvent)
    */
   public void backgroundChanged(BackgroundChangedEvent evt) {
      System.out.println("backgroundchangedevent");
      repaint();
      
   }
}



The method public void backgroundChanged is called, but when this happen the repaint() call doesnt seem to work. Atleast the paintComponent method isnt called.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Browse for soundtracks for your game!

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (147 views)
2013-05-17 21:29:12

alaslipknot (154 views)
2013-05-16 21:24:48

gouessej (185 views)
2013-05-16 00:53:38

gouessej (179 views)
2013-05-16 00:17:58

theagentd (189 views)
2013-05-15 15:01:13

theagentd (174 views)
2013-05-15 15:00:54

StreetDoggy (217 views)
2013-05-14 15:56:26

kutucuk (242 views)
2013-05-12 17:10:36

kutucuk (240 views)
2013-05-12 15:36:09

UnluckyDevil (246 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!
Page created in 0.076 seconds with 20 queries.