//JAVA COSDE TO COPY//
// importing required packages
import java.applet.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Timer extends Applet implements Runnable,
ActionListener {
// Panel to keep all the buttons and labels
Panel p;
Label display;
// Button
Button start, stop, reset;
// Time
int hour, minute, second, millisecond;
// string to be displayed on the label
String disp;
// State of stopwatch on/off
boolean on;
// initialization
public void init()
{
// initially off
on = false;
p = new Panel();
// Setting layout of the panel
p.setLayout(new GridLayout(4, 1, 6, 10));
// initial time 00 : 00 : 00 : 000
hour = minute = second = millisecond = 0;
// Label
display = new Label();
disp = "00 : 00 : 00 : 000";
display.setText(disp);
p.add(display);
// Start button
start = new Button("Start");
start.addActionListener((ActionListener) this);
p.add(start);
// Reset button
reset = new Button("Reset");
reset.addActionListener((ActionListener) this);
p.add(reset);
// Stop button
stop = new Button("Stop");
stop.addActionListener((ActionListener) this);
p.add(stop);
add(p);
// starting thread
new Thread(this, "StopWatch").start();
}
// Reset Function
// reset to default value
public void reset()
{
try {
Thread.sleep(1);
}
catch (Exception e) {
System.out.println(e);
}
hour = minute = second = millisecond = 0;
}
// update function
// update the timer
public void update()
{
millisecond++;
if (millisecond == 1000) {
millisecond = 0;
second++;
if (second == 60) {
second = 0;
minute++;
if (minute == 60) {
minute = 0;
hour++;
}
}
}
}
// changing label
public void changeLabel()
{
// Properly formatting the display of the timer
if (hour < 10)
disp = "0" + hour + " : ";
else
disp = hour + " : ";
if (minute < 10)
disp += "0" + minute + " : ";
else
disp += minute + " : ";
if (second < 10)
disp += "0" + second + " : ";
else
disp += second + " : ";
if (millisecond < 10)
disp += "00" + millisecond;
else if (millisecond < 100)
disp += "0" + millisecond;
else
disp += millisecond;
display.setText(disp);
}
// thread.run function
public void run()
{
// while the stopwatch is on
while (on) {
try {
// pause 1 millisecond
Thread.sleep(1);
// update the time
update();
// changeLabel
changeLabel();
}
catch (InterruptedException e) {
System.out.println(e);
}
}
}
// actionPerformed
// To listen to the actions on the buttons
public void actionPerformed(ActionEvent e)
{
// start a thread when start button is clicked
if (e.getSource() == start) {
// stopwatch is on
on = true;
new Thread(this, "StopWatch").start();
}
// reset
if (e.getSource() == reset) {
// stopwatch off
on = false;
reset();
changeLabel();
}
if (e.getSource() == stop) {
// stopwatch off
on = false;
}
}
}
//PROGRAM OUTPUT//
//EDITOR OUTPUT//
//COMMENT DOWN FOR ANY QUERIES...
//HIT A THUMBS UP IF YOU DO LIKE IT!!!
Nork individually or in pairs to write a simple "timer" dialog in Java. The dialog shall...
JavaFX! Just need to fill several lanes of code please!!! CSE205 OOP and Data Structure Quiz #15 Last Name (print) First Name (print) Write a JavaFX GUI application program that simulates a timer. The timer should show "count: the beginning and increase the number by 1 in every one second, and so on. o" at .3 A Timer - × A Timer Count: 0 Count: 1 (B) After 1 second, the GUI Window (A) Initial GUI Window According to the...
I want the content of the combo box updates with the corresponding radiobutton, how can I write the two radioButton actionListeners? and mycomboBox addItemListener? There are 4 GUI components at the top (NORTH) of the ContentPane of the JFrame, label, two radio buttons, and a combo box. The combo box is empty. When a user has clicked a radio button, the content of the combo box will be update You will need to use the setModel method and set the...
1. Analyze the following code: public class Test implements Runnable { public static void main(String[] args) { Thread t = new Thread(this); t.start(); } public void run() { System.out.println("test"); } } 1. The code compiles but will not print anything since t does not invoke the run method. 2. The code will not compile since you cannot invoke "this" in a static method. 3. The program compiles, runs, and prints tests on the console. 2. What will the following example...
JAVA Hello I am trying to add a menu to my Java code if someone can help me I would really appreacite it thank you. I found a java menu code but I dont know how to incorporate it to my code this is the java menu code that i found. import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.ButtonGroup; import javax.swing.JCheckBoxMenuItem; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JRadioButtonMenuItem; public class MenuExp extends JFrame { public MenuExp() { setTitle("Menu Example");...
/** * File: GradeCalculator . java * Description: Instances of this class are used to calculate * a course average and a letter grade. In order to calculate * the average and the letter grade, a GradeCalculator must store * two essential pieces of data: the number of grades and the sum * of the grades. Therefore these are declared as object properties * (instance variables). * Each time calcAverage (grade) is called, a new grade is added to *...
Need help writing Java code for this question. CircleFrame class is provided below. what happen after u add the code and follow the requirement? It would be nice to be able to directly tell the model to go into wait mode in the same way that the model's notify method is called. (i.e. notify is called directly on the model, whereas wait is called indirectly through the suspended attribute.) Try altering the the actionPerformed method of the SliderListener innner class...
I have this problem for a final thats in my book thats i'm stuck on . Any help would be really appreciated. Design and code a Swing GUI to translate text that is input in English into Pig Latin. You can assume that the sentence contains no punctuation. The rules for Pig Latin are as follows: ⦁ For words that begin with consonants, move the leading consonant to the end of the word and add “ay.” For example, “ball” becomes...
Please try to write the code with Project 1,2 and 3 in mind. And use java language, thank you very much. Create an Edit Menu in your GUI Add a second menu to the GUI called Edit which will have one menu item called Search. Clicking on search should prompt the user using a JOptionPane input dialog to enter a car make. The GUI should then display only cars of that make. You will need to write a second menu...
In java language here is my code so far! i only need help with the extra credit part For this project, you will create a Rock, Paper, Scissors game. Write a GUI program that allows a user to play Rock, Paper, Scissors against the computer. If you’re not familiar with Rock, Paper, Scissors, check out the Wikipedia page: http://en.wikipedia.org/wiki/Rock-paper-scissors This is how the program works: The user clicks a button to make their move (rock, paper, or scissors). The program...
could you please help me with this problem, also I need a little text so I can understand how you solved the problem? import java.io.File; import java.util.Scanner; /** * This program lists the files in a directory specified by * the user. The user is asked to type in a directory name. * If the name entered by the user is not a directory, a * message is printed and the program ends. */ public class DirectoryList { public static...