-1

I am working on this program that calculates the Beats per Minute (BPM) when you click the button. When you click two times, it is supposed to display the current BPM, and display the new one with every click after that. What the problem is, though, is that the display isn't changing. What do I need to do?

Here is my code:

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

public class BPM extends JPanel implements ActionListener {
    JLabel label;
    public String display;
    public int bpm;
    public int buttonPressed;
    public int time1;
    public int time2;
    public int time3;
    public int counter[];

    public void addComponents(Container pane) {
        JPanel buttons = new JPanel();

        JButton bpmButton = new JButton("Click");
        bpmButton.setSize(new Dimension(100, 50));
        bpmButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                buttonPressed++;
                counter = new int[2];
                if (buttonPressed == 1) {
                    counter[0] = (int)(System.currentTimeMillis());
                }   else if (buttonPressed == 2) {
                    counter[1] = (int)(System.currentTimeMillis());
                    calculateTimeBetweenClicks();
                    setTime();
                }   else {
                    counter[0] = counter[1];
                    counter[1] = (int)(System.currentTimeMillis());
                    calculateTimeBetweenClicks();
                    setTime();
                }
            }
        });

        display = "0";
        label = new JLabel(display, SwingConstants.CENTER);
        label.setFont(label.getFont().deriveFont(100.0f)); // original 45

        pane.add(label, BorderLayout.PAGE_START);
        pane.add(bpmButton, BorderLayout.CENTER);
    }

    // Calculates the difference between the two saved clicks
    public void calculateTimeBetweenClicks() {
        if (buttonPressed == 1) {
            time1 = counter[0];
        } else {
            time1 = counter[0];
            time2 = counter[1];
        }
        time3 = time2 - time1;
    }

    // Calculates the BPM and changes the display accordingly
    public void setTime() {
        bpm = 60000 / time3;
        display = "" + bpm + "";
        label.setText(display);
    }

    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub

    }

    public static void createAndShowGUI() {
        // Creates the window
        JFrame frame = new JFrame("BPM Calculator");
        frame.setPreferredSize(new Dimension(300, 200)); // original (250, 130)
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // Adds the components to the content pane
        BPM window = new BPM();
        window.addComponents(frame.getContentPane());

        //Displays the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        // Turns off bold text
        UIManager.put("swing.boldMetal", Boolean.FALSE);

        // Allows the components to be used and interacted with
        java.awt.EventQueue.invokeLater(new Runnable() {
             public void run() {
                createAndShowGUI();
             }
        });
    }

}
Snurgler
  • 9
  • 5
  • [What does your step debugger tell you?](http://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems). Your question can be answered very quickly and easily with your step-debugger. You should always try and solve your problems with a step debugger before coming to StackOverflow. –  May 16 '18 at 23:42

1 Answers1

1

The problem is in your addComponents method, you are creating a new array on each and every button click (so you end up with a new and empty array). This is throwing off your calculation. Simply move the instantiation of your array to somewhere outside of the ActionListener like this...

public void addComponents(Container pane) {
    JPanel buttons = new JPanel();
    counter = new int[2]; //Move this line to here...
    JButton bpmButton = new JButton("Click");
    bpmButton.setSize(new Dimension(100, 50));
    bpmButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            buttonPressed++;                
            if (buttonPressed == 1) {
                counter[0] = (int)(System.currentTimeMillis());
            }   else if (buttonPressed == 2) {
                counter[1] = (int)(System.currentTimeMillis());
                calculateTimeBetweenClicks();
                setTime();
            } //Removed the else - see edit below :-)
        }
    });

Additional

Your code as-is seems to get a litle confused after the 2nd click (the first BPM calculation) as it seems to take that 2nd click as the first click of the next set of 2 clicks if you get what I mean. I'm not sure if this is intended behaviour, but if not, I would reset everything in the calculateTimeBetweenClicks method after you've calculated the correct bpm ready for a new set of 2 clicks...

 // Calculates the difference between the two saved clicks
public void calculateTimeBetweenClicks() {
    if (buttonPressed == 1) {
        time1 = counter[0];
    } else {
        time1 = counter[0];
        time2 = counter[1];
        //Reset here ready for next 2 clicks...
        counter[0]=0;
        counter[1]=0;
        buttonPressed = 0;
    }
    time3 = time2 - time1;
}
Zippy
  • 3,826
  • 5
  • 43
  • 96