The timer is not working perfectly. it gives a very off estimate of the elapsedtime each time.
Please Help

import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;

public class JumpingPanel extends JFrame{

    public static final int BUTTON_WIDTH = 45;
    public static final int BUTTON_HEIGHT = 45;
    static int n=0;

    private static final Dimension screenSize =

    private JPanel middle;
    private TargetPanel target;

    private static void resizeComponent(Component c) {
	    Dimension d = c.getSize(null);
            c.setPreferredSize(new Dimension((int)(0.9*d.width), (int)(0.9*d.height)));

    private static class TargetPanel extends JPanel implements ActionListener{
            private JButton theMainButton;
            private Random rand;

            public void actionPerformed(ActionEvent ae) {
            JButton source = (JButton)ae.getSource();
            float elapsedTimeMillis = 0;
            double elapsedTimesec1;


      if (source == theMainButton) {
      long start = System.currentTimeMillis(); // get the starting milli-seconds
      JOptionPane.showMessageDialog(null, "You Clicked it " +n);
      long elapsedTimeSec = System.currentTimeMillis(); // get the stoping milli-seconds,
      elapsedTimesec1 =(double)(elapsedTimeSec - start)/100.0; // calculate the lasting time in seconds.
      JOptionPane.showMessageDialog(null, "Took you: " + elapsedTimesec1 + " seconds."); // showing the result.



        public TargetPanel() {
            rand = new Random();
            setLayout(new GridLayout(3,3));

            for (int r = 0; r < 3; r++) {
                for (int c = 0; c < 3; c++) {
                    JButton button = new JButton();
                    if (r == 1 && c == 1) {
                        theMainButton = button;

                    button.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));

            setMaximumSize(new Dimension(3*BUTTON_WIDTH,3*BUTTON_HEIGHT));
            Border border = BorderFactory.createLineBorder(Color.BLACK, 4);

    public static void main(String[] args) {
        JumpingPanel frame = new JumpingPanel();
        frame.setTitle("The Clicking Game");

        frame.middle = (JPanel)frame.getContentPane();
        frame.middle.setLayout(new FlowLayout());

        frame.target = new TargetPanel();

6 Years
Discussion Span
Last Post by brandonrunyon

acash229, If your intention is that the time when first time clicking the middle button is stored in long variable start which remains unchanged all the time, you need to declarare it as a global static variable like n.
static int n=0;
static long start =0;
Then should we replace the line 52 with

if (n==0)
start= System.currentTimeMillis(); // get the starting milli-seconds

so that the variable start remains unchanged after the starting milliseconds is stored in it?

In this way, the time gap grows and fast.

Edited by tong1: n/a


Thank you ill try tomorow morning and will let uno how it is.


Hey yah that doesnt work still. I get no value if i do that.
I just need the timer to work correctly.


You've been shown how in over 25 posts on the other forum and tong1 gave you the code to use here.

Where to next?
You need to get a tutor or to schedule some time with your instructor. There are so many programming concepts that you don't have. You need someone full time for a long time so you can learn basic programming before you try to work on this project.


I need the timer fixed.
WHy is it giving me wrong elapsed timings


Try playing computer with your program. Go through the logic step by step to see when the timer is started and when it is ended. Use a piece of paper and a pencil to keep track of the contents of important variables.


From the other thread, I noticed that you said you were getting an output of 0.987 when you were clicking for around 10 seconds.

Are you sure that you did (endtime-starttime)/1000, not 10,000? That would explain why you're getting a result roughly ten times less than the expected value.


you might consider using "T bar" method (you might remember it from chemistry class) to convert from one unit of time to the others so you can keep consistency with your units. But until you think logically/mathematically about this, you can't solve it. I'd suggest the same as the other posts that you spend some time with a tutor/professor on the logic and planning behind how this works. Maybe you can post the algorithm you have written/using to write this program?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.