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

Java (Goal): Use your own thread to give them a life, that means make the clock

ID: 3837267 • Letter: J

Question

Java (Goal): Use your own thread to give them a life, that means make the clock to work in realtime, and Fan to start. Use own GUI to start and stop as you wish.

The code here does not work. If someone would be able to correct it that would be great. Also, this does not reflect the real time, so please make it as it does.

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

public class ClockAnimation extends JFrame
{
//create an instance for StillClock
private StillClock clock = new StillClock();

//create buttons
JButton startButton;
JButton stopButton;

//create a timer (1000 milliseconds delay)
Timer timer = new Timer(1000, new TimerListener());

public ClockAnimation()
{
//create a panel to hold the clock
JPanel clockPanel = new JPanel();

//add the clock to panel
clockPanel.add(clock);

//create a panel to hold start and stop buttons
JPanel ButtonPanel = new JPanel();

//create a start button
startButton = new JButton("Start");
//add action listener to startButton
startButton.addActionListener(new startListener());
//create a stop button
stopButton = new JButton("Stop");
//add action listener to stopButton
stopButton.addActionListener(new stopListener());
//add start button to ButtonPanel
ButtonPanel.add(startButton);
//add stop button to ButtonPanel
ButtonPanel.add(stopButton);

//create elapsedPanel to hold elaped time
JPanel ElspsedPanel = new JPanel();

//add start time to panel
add.(timer.start());
//add stop time to panel
add.(timer.stop());
//add the elapsed time to panel
ElapsedPanel.add((timer.start())-(timer.stop()));


//add the clockPanel and ButtonPanel to the frame
add(clockPanel, BorderLayout.NORTH);
add(ButtonPanel, BorderLayout.CENTER);
add(ElapsedPanel, BorderLayout.SOUTH);
}

private class startListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
//start clock time
timer.start();
}
}

private class stopListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
//stop clock time
timer.stop();
}
}

private class TimerListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
//set current time and paint clock to display current time
clock.setCurrentTime();
clock.repaint();
}
}

//main method
public static void main(String[] args)
{
//create a JFrame instance
JFrame frame = new ClockAnimation();

//set title of the frame
frame.setTitle("My Clock");
//set location of the frame to center
frame.setLocationRelativeTo(null);
//set frame to close on exit
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//set frame to visable
frame.setVisible(true);
//set components to fit in frame
frame.pack();
}
}


public class ElapsedPanel {
   int startTime;
   double currentElapsedTime;
   double endElapsedTime;

   void start()
   {
   ElapsedPanel timer = null;
   timer.start();
   startTime = (int) System.currentTimeMillis();
   }

   void stop()
   {
   ElapsedPanel timer;
   timer.stop();
   Object elapsedTime = getElapsedTime();
   }

   void getElapsedTime() //Could be called apart from the stop() function to
   //get the elapsed time without stopping the timer
   {
   currentElapsedTime = (System.currentTimeMillis() - startTime)/1000;
   }
  
   String toStandardTime(double elapsedTime)
   {
   int hours = 0;
   int minutes = 0;
   int seconds = 0;
   String formattedTime;

   minutes = (int) t/60;
   seconds = (int) t%60;
   hours = (int) minutes/60;
   minutes = (int) minutes%60;

   formattedTime = hours + ":" + minutes + ":" + seconds;
   return formattedTime;
   }
}

Explanation / Answer

import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
public class ClockAnimation extends JFrame
{
//create an instance for StillClock
private StillClock clock = new StillClock();
//create buttons
JButton startButton;
JButton stopButton;
//create a timer (1000 milliseconds delay)
Timer timer = new Timer(1000, new TimerListener());
public ClockAnimation ()
{
//create frame
Frame mainFrame = new Frame("Clock");
mainFrame.setSize(400,400);
mainFrame.setLayout(new GridLayout(3, 1));
//create a panel to hold the clock
JPanel clockPanel = new JPanel();
//add the clock to panel
clockPanel.add(clock);
//create a panel to hold start and stop buttons
JPanel ButtonPanel = new JPanel();
//create a start button
startButton = new JButton("Start");
//add action listener to startButton
startButton.addActionListener(new startListener());
//create a stop button
stopButton = new JButton("Stop");
//add action listener to stopButton
stopButton.addActionListener(new stopListener());
//add start button to ButtonPanel
ButtonPanel.add(startButton);
//add stop button to ButtonPanel
ButtonPanel.add(stopButton);
//create elapsedPanel to hold elaped time
JPanel ElspsedPanel = new JPanel();
JLabel myclockLabel = new JLabel("");
ElspsedPanel.add(myclockLabel);
//add start time to panel
ElapsedPanel obj = new ElapsedPanel();
obj.start();
//add stop time to panel
obj.stop();
//add the elapsed time to panel
myclockLabel.setText(""+obj.getElapsedTime());

//add the clockPanel and ButtonPanel to the frame
mainFrame.add(clockPanel, BorderLayout.NORTH);
mainFrame.add(ButtonPanel, BorderLayout.CENTER);
mainFrame.add(obj, BorderLayout.SOUTH);
}
private class startListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
//start clock time
timer.start();
}
}
private class stopListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
//stop clock time
timer.stop();
}
}
private class TimerListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
//set current time and paint clock to display current time
clock.setCurrentTime();
clock.repaint();
}
}
//main method
public static void main(String[] args)
{
//create a JFrame instance
JFrame frame = new ClockAnimation();
//set title of the frame
frame.setTitle("My Clock");
//set location of the frame to center
frame.setLocationRelativeTo(null);
//set frame to close on exit
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//set frame to visable
frame.setVisible(true);
//set components to fit in frame
frame.pack();
}
}

public class ElapsedPanel {
int startTime;
double currentElapsedTime;
double endElapsedTime;
void start()
{
ElapsedPanel timer = null;
timer.start();
startTime = (int) System.currentTimeMillis();
}
void stop()
{
ElapsedPanel timer;
timer.stop();
Object elapsedTime = getElapsedTime();
}
double getElapsedTime() //Could be called apart from the stop() function to
//get the elapsed time without stopping the timer
{
currentElapsedTime = (System.currentTimeMillis() - startTime)/1000;
return currentElapsedTime;
}
  
String toStandardTime(double elapsedTime)
{
int hours = 0;
int minutes = 0;
int seconds = 0;
String formattedTime;
minutes = (int) elapsedTime/60;
seconds = (int) elapsedTime%60;
hours = (int) minutes/60;
minutes = (int) minutes%60;
formattedTime = hours + ":" + minutes + ":" + seconds;
return formattedTime;
}
}

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