Results 1 to 14 of 14
  1. #1
    warchieflll is offline Member
    Join Date
    Dec 2010
    Posts
    55
    Rep Power
    0

    Default Load a file doesn't start a timer

    Heey all!

    I've created a program, where you could save and load files. The program uses mouse events: when dragging, a cage is created. when clicking on a cage; a animal will appear, and will move.

    Now, when save everything works great. I save the arraylist of cages. Now, when loading, everything appear correctly, but the animals doesn't start moving. Do I somehow need to start the timer myself?

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,002
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by warchieflll View Post
    Heey all!

    I've created a program, where you could save and load files. The program uses mouse events: when dragging, a cage is created. when clicking on a cage; a animal will appear, and will move.

    Now, when save everything works great. I save the arraylist of cages. Now, when loading, everything appear correctly, but the animals doesn't start moving. Do I somehow need to start the timer myself?
    If nothing else starts the timer you should indeed start it yourself after loading; without seeing any code I see no reason why loading a bunch of objects should start a timer.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    warchieflll is offline Member
    Join Date
    Dec 2010
    Posts
    55
    Rep Power
    0

    Default

    I thought it because in a cage there is a object animal, and that animal is moving by a timer. In the constructor of the Animal class starts the timer:

    Java Code:
    	public Dier (String soort, int grootte, boolean vrouwtje)
    	{
    		setOpaque(false);
    	    addMouseListener(this);
    		random = new Random();	
    	    
    		this.soort = soort;
    		this.grootte = grootte;
    		this.vrouwtje = vrouwtje;
    		
    		maakIcoon();
    		
    	    timer = new Timer(150, this);
    	    timer.start();
    	    timer2 = new Timer(2000, this);
    	    timer2.start();
    	}

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,002
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by warchieflll View Post
    I thought it because in a cage there is a object animal, and that animal is moving by a timer. In the constructor of the Animal class starts the timer:
    Then the question is: does loading that file cause the Dier constructor to be called?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    warchieflll is offline Member
    Join Date
    Dec 2010
    Posts
    55
    Rep Power
    0

    Default

    I guess not... I don't know actually. I've changed the code. Now the timer in the class is started:

    Java Code:
    	public void startTimer()
    	{
    		System.out.println("uitgevoerd");
    
    		timer.start();
    		timer2.start();
    	}
    This works, I see the "uitgevoerd" in my console when loading a file. (when created 4 animals, save the file, close program, open the program, and load file, I see 4 times "uitgevoerd"). The only problem is that the animals doesn't move...? So timer starts, but it looks like the actionPerformed doesn't work or something?

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,002
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by warchieflll View Post
    I guess not... I don't know actually. I've changed the code. Now the timer in the class is started:

    Java Code:
    	public void startTimer()
    	{
    		System.out.println("uitgevoerd");
    
    		timer.start();
    		timer2.start();
    	}
    This works, I see the "uitgevoerd" in my console when loading a file. (when created 4 animals, save the file, close program, open the program, and load file, I see 4 times "uitgevoerd"). The only problem is that the animals doesn't move...? So timer starts, but it looks like the actionPerformed doesn't work or something?
    As you already discovered the System.out.println( ... ) call is a perfect "poor man's debugger". Your actionPerformed( ... ) method can use one ....

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    warchieflll is offline Member
    Join Date
    Dec 2010
    Posts
    55
    Rep Power
    0

    Default

    Done, and just as I expected.... When clicking (making a Animal), I see a lot of "uitgevoerd" in my console. When closing the program, and open and load, nothing displayed in my console... So the actionPerformed isn't called somehow...?

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,002
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by warchieflll View Post
    Done, and just as I expected.... When clicking (making a Animal), I see a lot of "uitgevoerd" in my console. When closing the program, and open and load, nothing displayed in my console... So the actionPerformed isn't called somehow...?
    Are you de/serializing those objects so that the Timers are also de/serialized? I don't think those ActionListeners are de/serialized as well; IOW, register your ActionListener again at those Timers.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    warchieflll is offline Member
    Join Date
    Dec 2010
    Posts
    55
    Rep Power
    0

    Default

    Good point, I forgot to serialize them. Now I did, but no luck.

    Also I tried to set:

    Java Code:
    	    addActionListener(this);
    in the methode timerStart();, but then I noticed: there isn't a addActionListener(this); at all in this class! But the timers do work.. Never hav eseen this before. When I try to add it, it give the error: 'the method ... is undefined for the type...'

    Edit:

    There is however a 'addMouseListener(this);'... the mouselisten does work when set in the timerStart();.
    Last edited by warchieflll; 02-03-2011 at 03:52 PM.

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,002
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by warchieflll View Post
    Good point, I forgot to serialize them. Now I did, but no luck.

    Also I tried to set:

    Java Code:
    	    addActionListener(this);
    in the methode timerStart();, but then I noticed: there isn't a addActionListener(this); at all in this class! But the timers do work.. Never hav eseen this before. When I try to add it, it give the error: 'the method ... is undefined for the type...'

    Edit:

    There is however a 'addMouseListener(this);'... the mouselisten does work when set in the timerStart();.
    This thread is running wild now; you have to show us some (relevant) code because (at least I) can't help you any further now.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    warchieflll is offline Member
    Join Date
    Dec 2010
    Posts
    55
    Rep Power
    0

    Default

    Okay, when opening this thread the timer didn't start. By putting this code I started the timer when loading a file:

    Java Code:
    	public void startTimer()
    	{
    		timer.start();
    		timer2.start();
    	}
    This tested with a System.out.println did work. So the timer has started again. Now, what I saved in the loading file is a 'cage' with 'Animals' in it (imgIcons as labels).:

    Java Code:
    	public void maakIcoon()
    	{
    		dierIcoon = new ImageIcon(Dier.class.getResource("/pics/" + soort + ".png"));
    		imgLabel = new JLabel(dierIcoon);
    		imgLabel.setBounds(0, 0, grootte, grootte);
    		add(imgLabel);
    	}
    When the timer tics, the label moves in a direction, so it looks like the animal is moving. Now, the animal isn't moving at all when loading the file.

    I checked the 'addActionListener(this)', and i noticed that there isn't any in the class. This is strange, because the timer did work before loading the file...

    EDIT:

    I tried the following, but also no luck:

    Java Code:
    	public void startTimer()
    	{
    		timer.start();
    		timer.addActionListener(this);
    		timer2.start();
    		timer.addActionListener(this);
    	}
    Last edited by warchieflll; 02-03-2011 at 04:42 PM.

  12. #12
    warchieflll is offline Member
    Join Date
    Dec 2010
    Posts
    55
    Rep Power
    0

    Default

    Problem finally solved! Although I don't really understand why... I had to create new timers when loading:

    Java Code:
    	public Dier (String soort, int grootte, boolean vrouwtje)
    	{
    		setOpaque(false);
    	    addMouseListener(this);
    		random = new Random();	
    	    
    		this.soort = soort;
    		this.grootte = grootte;
    		this.vrouwtje = vrouwtje;
    		
    		maakIcoon();
    		
    	    timer = new Timer(150, this);
    	    timer.start();
    	    timer2 = new Timer(2000, this);
    	    timer2.start();
    	}
    	
    	public void startTimer()
    	{
    	    timer = new Timer(150, this);
    	    timer.start();
    	    timer2 = new Timer(2000, this);
    	    timer2.start();
    	}
    Maybe that is what you ment, but I mentioned it wrong?

  13. #13
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,002
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by warchieflll View Post
    Problem finally solved! Although I don't really understand why... I had to create new timers when loading:

    Java Code:
    	public Dier (String soort, int grootte, boolean vrouwtje)
    	{
    		setOpaque(false);
    	    addMouseListener(this);
    		random = new Random();	
    	    
    		this.soort = soort;
    		this.grootte = grootte;
    		this.vrouwtje = vrouwtje;
    		
    		maakIcoon();
    		
    	    timer = new Timer(150, this);
    	    timer.start();
    	    timer2 = new Timer(2000, this);
    	    timer2.start();
    	}
    	
    	public void startTimer()
    	{
    	    timer = new Timer(150, this);
    	    timer.start();
    	    timer2 = new Timer(2000, this);
    	    timer2.start();
    	}
    Maybe that is what you ment, but I mentioned it wrong?
    I can't really answer that question because I don't know if your class Dier extends some other class explicitly (I suspect it does) because you've never shown us all your code. The Timer class (in the javax.swing) package is Serializable indeed so it tries to serialize its ActionListeners which is also a Dier object in your case. Better make that Timer object a transient object for now.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  14. #14
    warchieflll is offline Member
    Join Date
    Dec 2010
    Posts
    55
    Rep Power
    0

    Default

    Hmm... nope, Dier is a JPanel...:

    Java Code:
    package opslaanLadenBeweging;
    
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.io.Serializable;
    import java.util.Random;
    
    import javax.swing.ImageIcon;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.Timer;
    
    public class Dier extends JPanel implements MouseListener, ActionListener, Serializable
    {
    	private int grootte, dx, dy;
    	private String soort;
    	private boolean vrouwtje;
    	private ImageIcon dierIcoon;
    	private Timer timer, timer2;
    	private Random random;
    	private Verblijf verblijf;
        JLabel imgLabel;
    	
    	public Dier (String soort, int grootte, boolean vrouwtje)
    	{
    		setOpaque(false);
    	    addMouseListener(this);
    		random = new Random();	
    	    
    		this.soort = soort;
    		this.grootte = grootte;
    		this.vrouwtje = vrouwtje;
    		
    		maakIcoon();
    		
    	    timer = new Timer(150, this);
    	    timer.start();
    	    timer2 = new Timer(2000, this);
    	    timer2.start();
    	}
    	
    	public void startTimer()
    	{
    	    timer = new Timer(150, this);
    	    timer.start();
    	    timer2 = new Timer(2000, this);
    	    timer2.start();
    	}
    	  
    	public void maakIcoon()
    	{
    		dierIcoon = new ImageIcon(Dier.class.getResource("/pics/" + soort + ".png"));
    		imgLabel = new JLabel(dierIcoon);
    		imgLabel.setBounds(0, 0, grootte, grootte);
    		add(imgLabel);
    	}
    	
    	public void maakManVrouw()
    	{
    		dierIcoon = new ImageIcon(Dier.class.getResource("/pics/" + vrouwtje + ".png"));		
    		imgLabel = new JLabel(dierIcoon);
    		imgLabel.setBounds(0, 0, grootte, grootte);
    		add(imgLabel);
    	}
    	
    	public void mouseEntered( MouseEvent event )
    	{
    		remove(imgLabel);
    		maakManVrouw();
    	}
    
    	public void mouseExited( MouseEvent event )
    	{
    		remove(imgLabel);
    		maakIcoon();
    	}
    	
    	public void geefVerblijf(Verblijf verblijf)
    	{
    		 this.verblijf = verblijf;
    	}
    	
    	public void actionPerformed(ActionEvent e)
    	{
    		if(e.getSource() == timer)
    		{
    			if(getX() >= 0 && getY() >= 0)
    			{
    				setLocation(getX() + dx, getY() + dy);
    			}
    			
    			if(getX() <= 0)
    			{
    				dx = 2;
    				dy = 0;
    				setLocation(getX() + dx, getY() + dy);
    			}
    			else if(getY() <= 0)
    			{
    				dx = 0;
    				dy = 2;
    				setLocation(getX() + dx, getY() + dy);
    			}
    			
    			if((verblijf.getX() + getX() + 50 ) >= (verblijf.getX() + verblijf.breedte))
    			{
    				dx = -2;
    				dy = 0;
    				setLocation(getX() + dx, getY() + dy);
    			}
    			else if((verblijf.getY() + getY() + 50 >= (verblijf.getY() + verblijf.hoogte)))
    			{
    				dx = 0;
    				dy = -2;
    				setLocation(getX() + dx, getY() + dy);
    			}
    		}
    
    		if(e.getSource() == timer2)
    		{
    			dx = 0;
    		    dy = 0;
    		    int richting = random.nextInt(8) + 1;
    		    
    		    switch (richting)
    		    {
    		    case 1:
    		      dx = 2; // rechts
    		      break;
    		    case 2:
    		      dx = -2; // links
    		      break;
    		    case 3:
    		      dy = 2; // onder
    		      break;
    		    case 4:
    		      dy = -2; // boven
    		      break;
    		    case 5:
    		      dx = 1; // rechts boven
    		      dy = -1;
    		      break;
    		    case 6:
    		      dx = -1; // links boven
    		      dy = -1;
    		      break;
    		    case 7:
    		      dx = 1; // rechts onder
    		      dy = 1;
    		      break;
    		    case 8:
    		      dx = -1; // links onder
    		      dy = 1;
    		    }
    		}
    	}
    
    	// Niet in gebruik
    	public void mousePressed( MouseEvent event ){}
    	public void mouseReleased( MouseEvent event ){}
    	public void mouseClicked( MouseEvent event ){}
    }

Similar Threads

  1. file load
    By myka in forum Advanced Java
    Replies: 1
    Last Post: 04-03-2010, 08:42 PM
  2. load xml file in servlet help please
    By khdani in forum Java Servlet
    Replies: 2
    Last Post: 10-05-2009, 10:28 PM
  3. Load file in GUI instead of notepad
    By Kruptein in forum AWT / Swing
    Replies: 1
    Last Post: 08-08-2009, 05:57 PM
  4. Quit Button, Load new Frame, Timer Code
    By IHateNetbeans in forum New To Java
    Replies: 1
    Last Post: 03-18-2009, 03:58 PM
  5. Not able to load a properties file
    By Happy9959 in forum New To Java
    Replies: 12
    Last Post: 07-29-2008, 07:15 AM

Posting Permissions

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