Results 1 to 6 of 6
  1. #1
    ICEpower is offline Member
    Join Date
    Apr 2012
    Posts
    4
    Rep Power
    0

    Default Timer with System.nanotime()

    I'm just trying to make a rectangle move over the horizontal direction. Below you can find the code I have written, but it doesn't work when I click drop-button.
    I don't understand why it doesn't work. Could somebody push me in the right direction?

    I'm trying to program it scientifically correct, that's why I'm not using Swing Timer with which I can make it work.

    Thank you in advance!


    Java Code:
        public class SimulationREAL extends JPanel implements ActionListener
        {
        	private JButton dropknop;
        	private boolean drop = false;
        	
        	public boolean timer = false;
        	
        	private int Xpos;
        	
        	long time0 = System.nanoTime();
        	double time = 0;
        	double lastTime = 0;
        	double dt = 0;
        	double velocity = 2; // define the initial velocity of the object
        	
        	public SimulationREAL()
        	{
        		dropknop = new JButton("DROP");
        		dropknop.addActionListener(this);
        		this.add(dropknop);
        		
        		while(drop)
        		{
        		    time = (System.nanoTime() - time0)/1E9;  // time in seconds from the beginning
        		    dt = time - lastTime;  //time of last loop
        		    lastTime = time;
        
        		    Xpos += velocity * dt;
        		}
        	}
        
        	public void paintComponent(Graphics g)
        	{
        		super.paintComponent(g);
        		g.drawRect(Xpos, 0, 20, 20);
        	}
        	
        	public void actionPerformed(ActionEvent e) 
        	{
        		if(e.getSource() == dropknop)
        		{
        			this.drop = true;
        		}
        		
        	}
        	
        }

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

    Default Re: Timer with System.nanotime()

    When your object is constructed the variable 'drop' is false so the while-loop in the constructor doesn't run at all.

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

  3. #3
    ICEpower is offline Member
    Join Date
    Apr 2012
    Posts
    4
    Rep Power
    0

    Default Re: Timer with System.nanotime()

    Thank you for the reply.
    But in the actionPerformed I make the variable 'drop' true so why doesn't it work then? I don't really understand.

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

    Default Re: Timer with System.nanotime()

    By then the constructor is already finished and (so) the while-loop as well.

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

  5. #5
    ICEpower is offline Member
    Join Date
    Apr 2012
    Posts
    4
    Rep Power
    0

    Default Re: Timer with System.nanotime()

    I see, thank you. I've also tried putting it in the paintComponent but then it freezes?

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

    Default Re: Timer with System.nanotime()

    Quote Originally Posted by ICEpower View Post
    I see, thank you. I've also tried putting it in the paintComponent but then it freezes?
    If that loop is in the paintComponent( ... ) method and it runs (drop == true); the EDT (Event Dispatch Thread) can't draw or listen to button presses etc. anymore because it's too busy running your loop.

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

Similar Threads

  1. system creats unsense Timer thread-0
    By henry123 in forum Advanced Java
    Replies: 0
    Last Post: 02-20-2011, 11:23 AM
  2. System.nanoTime();
    By rootpi in forum New To Java
    Replies: 6
    Last Post: 01-28-2011, 04:38 AM
  3. Question about System.nanoTime()?
    By chan_nguyen in forum New To Java
    Replies: 5
    Last Post: 09-06-2010, 09:11 AM
  4. problem with System.nanoTime()
    By Bryan500 in forum New To Java
    Replies: 5
    Last Post: 11-01-2008, 04:39 PM
  5. Replies: 0
    Last Post: 04-04-2008, 02:46 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
  •