Hey guys, I have tried for 3 days now, tried so many forums but help, but still i cannot get the help.
I need help from this forum, because i have heard the members are very smart.

Basicaly i have a code for a game. The point is that the user has to click the square certain amount of time and see how long it takes the user to click it.

I got the square and clicker to work. But I CANNOT GET THE TIMER TO WORK CORRECTLY. I AM HAVING A HARD TIME FIGURING OUT HOW TO MAKE THE TIMER WORK AND DISPLAY SAYING IT TOOK YOU __ SECONDS TO CLICK.

pLEASE HELP
THANKSS

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 =
    		Toolkit.getDefaultToolkit().getScreenSize();

    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) {

		resizeComponent(this);
                setLocation(rand.nextInt(screenSize.width-3*BUTTON_WIDTH),
                            rand.nextInt(screenSize.height-3*BUTTON_HEIGHT));

               
            long start = System.currentTimeMillis();

                n++;
                if(n>=8){

                JOptionPane.showMessageDialog(null, "You Clicked it " +n);

              float elapsedTimeSec = elapsedTimeMillis/1000F;

              elapsedTimesec1 = elapsedTimeSec - start;

              JOptionPane.showMessageDialog(null, "Took you: " +elapsedTimeSec1+ " seconds.");
                }

                validate();
                }

        }




        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;
                        theMainButton.addActionListener(this);

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

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


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

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

        frame.target = new TargetPanel();
        frame.middle.add(frame.target);

        frame.setVisible(true);
	}
}

Thank you guys

I have modified your code between line 41 and 62 as follows:

if (source == theMainButton) {
		resizeComponent(this);
        	         setLocation(rand.nextInt(screenSize.width-3*BUTTON_WIDTH),
        	         rand.nextInt(screenSize.height-3*BUTTON_HEIGHT));               
                  long start = System.currentTimeMillis(); // get the starting milli-seconds
             	n++;
                if(n>=8){
                JOptionPane.showMessageDialog(null, "You Clicked it " +n);
                long elapsedTimeSec = System.currentTimeMillis(); // get the stoping milli-seconds,
                elapsedTimesec1 =(double)(elapsedTimeSec - start)/1000.0; // calculate the lasting time in seconds.
                JOptionPane.showMessageDialog(null, "Took you: " + elapsedTimesec1  + " seconds."); // showing the result.
                }

hope the changes are helpful.

Hey thanks alot, but it doesnt show the seconds still??

Hey thanks alot, but it doesnt show the seconds still??

I have modified your code between line 41 and 62 as follows:

if (source == theMainButton) {
		resizeComponent(this);
        	         setLocation(rand.nextInt(screenSize.width-3*BUTTON_WIDTH),
        	         rand.nextInt(screenSize.height-3*BUTTON_HEIGHT));               
                  long start = System.currentTimeMillis(); // get the starting milli-seconds
             	n++;
                if(n>=8){
                JOptionPane.showMessageDialog(null, "You Clicked it " +n);
                long elapsedTimeSec = System.currentTimeMillis(); // get the stoping milli-seconds,
                elapsedTimesec1 =(double)(elapsedTimeSec - start)/1000.0; // calculate the lasting time in seconds.
                JOptionPane.showMessageDialog(null, "Took you: " + elapsedTimesec1  + " seconds."); // showing the result.
                }

hope the changes are helpful.

Im sorry i got it to display it, but it seems like the counter is off.
It shows like .987 seconds when i have been clicking it for more than like 10 seconds

The clock is way off the time. It takes me 3 secs to click and it shows 13 seconds.

Thank you for the help though.

If this can be fixed you will the best person alive and make me a happy man. :)

Did you check out my 2nd link?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.