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

Java problem!! Instructions: 1. Initial configuration The application shall crea

ID: 3727577 • Letter: J

Question

Java problem!!

Instructions:

1. Initial configuration

The application shall create a window with two buttons and a label.

The first button shall be labeled "Start".

The second buttons shall be labeled "Reset".

The label shall display the elapsed time, in 0.01 second increments.

The text in the label should be centered vertically and horizontally.

The label shall be easy to read. This example uses 36-point, bold Arial as the font.
(Hint: see java.awt.Font)

The initial window looks like this:

2. Clicking the Start button:

Clicking the Start button, starts the timer.

The button caption changes to "Pause".

The label updates each 0.01 of a second to reflect elapsed time.

Here is an example of what the window looks like after the Start button is clicked:

3. Clicking the Pause button:

Clicking the Pause button, stops the timer.

The button caption changes to "Continue".

The output label stops counting elapsed time.

Here is an example of what the window looks like after the Stop button is clicked:

4. Clicking the Continue Button

Clicking the Continue button, restarts the timer.

The button caption changes to "Pause".

The output label resumes counting elapsed time, adding to the elapsed time.
That is, the next update will indicate 0.01 seconds after the time displayed during pause

5. Clicking the Reset Button

Clicking the Reset button, returns the timer to the initial configuration.

If the timer is running, it is stopped.

The caption of the first button changes to "Start".

The output label reverts to "0.00 seconds".

Note:

Use java.lang.Thread rather than java.util.Timer

You may assume that the Thread.sleep is accurate enough for this implementation. That is, you can assume that every call the Thread.sleep(10) is 10 milliseconds long. So, after calling Thread.sleep(10), the display can be updated by 0.01 seconds.

Here is my code so far:

public class MyTimer extends javax.swing.JFrame {
public MyTimer(){
super("My Timer");
setLocation(25, 25);
setSize(400, 300);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
  
  
// create a toolbar
javax.swing.JPanel toolbar = new javax.swing.JPanel();
add(toolbar, java.awt.BorderLayout.NORTH);
  
// create a button
javax.swing.JButton button1;
javax.swing.JButton button2;
button1 = new javax.swing.JButton("Start");
button2 = new javax.swing.JButton("Reset");
toolbar.add(button1);
toolbar.add(button2);
  
  
// finally, set the window to be visible
setVisible(true);
}

/**
* The application method
* @param args The command-line arguments
*/
public static void main(String[] args) {
new MyTimer();

}
  
}

My Timer Start Reset 0.00 Seconds

Explanation / Answer

Programming code :

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

public class CountTimerGUI implements ActionListener {

private JFrame frame;
private JPanel panel;

private JLabel timeLabel = new JLabel();

private JButton startBtn = new JButton("Start");
private JButton pauseBtn = new JButton("Pause");
private JButton resumeBtn = new JButton("Resume");
private JButton stopBtn = new JButton("Stop");
private JButton resetBtn = new JButton("Reset");

private JButton greenBtn = new JButton("Green");
private JButton redBtn = new JButton("Red");

private CountTimer cntd;


public CountTimerGUI() {
setTimerText(" ");
GUI();
}

private void GUI() {
frame = new JFrame();
panel = new JPanel();

panel.setLayout(new BorderLayout());
timeLabel.setBorder(BorderFactory.createRaisedBevelBorder());
panel.add(timeLabel, BorderLayout.NORTH);

startBtn.addActionListener(this);
pauseBtn.addActionListener(this);
resumeBtn.addActionListener(this);
stopBtn.addActionListener(this);
resetBtn.addActionListener(this);
greenBtn.addActionListener(this);
redBtn.addActionListener(this);

JPanel cmdPanel = new JPanel();
cmdPanel.setLayout(new GridLayout());

cmdPanel.add(startBtn);
cmdPanel.add(pauseBtn);
cmdPanel.add(resumeBtn);
cmdPanel.add(stopBtn);
cmdPanel.add(resetBtn);

panel.add(cmdPanel, BorderLayout.SOUTH);

JPanel clrPanel = new JPanel();
clrPanel.setLayout(new GridLayout(0,1));

clrPanel.add(greenBtn);
clrPanel.add(redBtn);

panel.add(clrPanel, BorderLayout.EAST);

frame.setContentPane(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.pack();

cntd = new CountTimer();

}

private void setTimerText(String sTime) {
timeLabel.setText(sTime);
}


private void setTimerColor(Color sColor) {
timeLabel.setForeground(sColor);
}

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

JButton btn = (JButton) e.getSource();

if (btn.equals(greenBtn)) { setTimerColor(Color.GREEN.darker()); }
else if (btn.equals(redBtn)) { setTimerColor(Color.RED); }
else if (btn.equals(startBtn)) { cntd.start(); }
else if (btn.equals(pauseBtn)) { cntd.pause(); }
else if (btn.equals(resumeBtn)) { cntd.resume(); }
else if (btn.equals(stopBtn)) { cntd.stop(); }
else if (btn.equals(resetBtn)) { cntd.reset(); }
}


public static void main(String[] args) {

java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new CountTimerGUI();
}
});

}

private class CountTimer implements ActionListener {

private static final int> private int count = 0;
private boolean isTimerActive = false;
private Timer tmr = new Timer(ONE_SECOND, this);

public CountTimer() {
count=0;
setTimerText(TimeFormat(count));
}

@Override
public void actionPerformed(ActionEvent e) {
if (isTimerActive) {
count++;
setTimerText(TimeFormat(count));
}
}

public void start() {
count = 0;
isTimerActive = true;
tmr.start();
}

public void resume() {
isTimerActive = true;
tmr.restart();
}

public void stop() {
tmr.stop();
}

public void pause() {
isTimerActive = false;
}

public void reset() {
count = 0;
isTimerActive = true;
tmr.restart();

}

}

private String TimeFormat(int count) {

int hours = count / 3600;
int minutes = (count-hours*3600)/60;
int seconds = count-minutes*60;

return String.format("%02d", hours) + " : " + String.format("%02d", minutes) + " : " + String.format("%02d", seconds);
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote