Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hey guys, I need to put a timer in my program which is almost similar to a stop

ID: 3620037 • Letter: H

Question

Hey guys, I need to put a timer in my program which is almost similar to a stop watch.

Basically when the user clicks on the square i want the timer to goo off. And then i want it to show on the screen how long it took the user to click on the the box.
I have the code here but i cant seem to get the timer to work correctly.
Can someone please help me.

[code]
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;
long before;
Timer counter;

public void actionPerformed(ActionEvent ae) {
JButton source = (JButton)ae.getSource();

if (source == theMainButton) {

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

int delay = 100;
counter = new Timer(delay, this);
before = System.nanoTime();
counter.start();

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

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

long after = System.nanoTime();
double before1;
before1 = after-before/1000000000000.0;

JOptionPane.showMessageDialog(null, "Took you: " +before1+ " 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);
}
}

[/code]

Explanation / Answer

Dear.. 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(); // get the starting milli-seconds n++; if(n>=8) { JOptionPane.showMessageDialog(null, "You Clicked it " +n); long elapsedTimeSec = System.currentTimeMillis(); elapsedTimesec1 =(double)(elapsedTimeSec - start)/1000.0; 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(newDimension(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); } }