Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Self written timer with System.nanoTime()

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Self written timer with System.nanoTime()

    Hello,
    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?

    Thank you in advance!

    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
    Junior Member
    Join Date
    Nov 2011
    Location
    Aarhus, Denmark
    Posts
    28
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Self written timer with System.nanoTime()

    Try and insert some debug printing before and after your drop while loop. And try to explain your idea behind the flow of the program execution. Also what is the expected behavior of your code when the button is pressed, how is the while loop being reached.

Similar Threads

  1. Code Written In Servelet Is Not Working
    By anandpatil in forum Java Servlet
    Replies: 0
    Last Post: January 10th, 2013, 08:09 AM
  2. My AP CS Book Has Code Written in JRE5
    By RAWBERRY in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 24th, 2011, 07:55 PM
  3. I NEED A WRITTEN CODE
    By samy222 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 22nd, 2010, 05:28 PM
  4. need this program written
    By damajicman3 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 6th, 2010, 03:19 PM