#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    8
    Rep Power
    0

    A primitive stopwatch


    I have been working on a new exercise in which I am required to create a primitive stop watch. Here's the code so far:

    Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    
    public class StopWatch extends JFrame implements ActionListener{
    	
    	// instanciates the three buttons needed for the primitive stop watch
    	Button starter;
    	Button stopper;
    	Button resetter;
    	
    	//creates a Text Field object 30 Characters in length 
    	TextField testTextField = new TextField(30);
    	
    	//Crates a Timer object
    	protected Timer timer = new Timer(10, this);
    
    	//the int values for the stop watch counter
    	private int hunSec	= 0;
    	private int tenSec	= 0;
    	private int sec		= 0;
    
    
    	//Constructor
    	public StopWatch() {
    		
    		//Sets the Layout of the panel to a FlowLayout
    		setLayout(new FlowLayout());
    		
    		//Creates the Buttons in the GUI
    		starter = new Button("START");
    		stopper = new Button("STOP");
    		resetter = new Button("RESET");
    		
    		//Creates ActionListeners for the buttons
    		starter.addActionListener(this);
    		stopper.addActionListener(this);
    		resetter.addActionListener(this);
    		
    		//Adds the Buttons to the GUI
    		add(starter);
    		add(stopper);
    		add(resetter);
    		
    		//Adds the text field to the GUI
    		add(testTextField);
    		
    		
    		//Sets the Size of the Frame to 300 by 150 Pixels
    		setSize(300,150);
    		
    		//Adds a WindowListener to end the Program when the Frame is closed
    		addWindowListener(new WindowAdapter() {
    			public void windowClosing(WindowEvent e) {
    				System.exit(0);
    			}
    		});
    	}
    	
    	//ActionListener code for the buttons
    	@Override
    	public void actionPerformed(ActionEvent e) {
    		
    		//Starts the primitive stop Watch. Every 1/100th of a second the time displayed is incremented
            if(e.getSource() == this.starter){
            	timer.start();
                
                while(timer.isRunning()){
                	
                	try {
    					Thread.sleep(10);
    				} catch (InterruptedException e1) {
    					e1.printStackTrace();
    				}
                	
                	testTextField.setText("");
                	
                	hunSec++;
                	
                	if(hunSec == 9){
                		hunSec = 0;
                		tenSec++;
                	}
                	
                	if(tenSec == 9){
                		tenSec = 0;
                		sec++;
                	}
                	
                	testTextField.setText(sec + (":") + tenSec + hunSec);
                	
                	
    
                }
                
                
    
            }
            
            
            //This button is *SUPPOSED* to terminate the incrementation, effectively terminating the stop watch.
            if(e.getSource() == this.stopper){
                timer.stop();
    
            }
            
            //resets the timer and all stop watch components to 0 if the timer is *not* running. Otherwise it does nothing.
            if(e.getSource() == this.resetter){
                if(timer.isRunning() == false){
            		timer.restart();
            		hunSec = 0;
            		tenSec = 0;
            		sec = 0;
            		}
                else{}
    
            }
    		
    
    
    	}
    	
    	//The Window listener which terminates the program when the Frame is closed
    	static class AWindowListener extends WindowAdapter {
    		public void windowClosing(WindowEvent e) {
    		System.exit(0);
    		}
    		}
    
    
    	
    
    }

    Code:
    //The graphic information for the primitive stop watch, included for completion
    
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    
    import javax.swing.JPanel;
    
    
    public class StopWatchPanel extends JPanel{
    	
    	StopWatch stpwch;
    	
    	public StopWatchPanel(StopWatch stpwch){
    		setSize(200, 50);
    		stpwch = this.stpwch;
    		
    	}
    	
    	public void paint(Graphics g) {
    		g.setFont(new Font("Courier New", Font.PLAIN, 24));
    
    	}
    
    	
    	
    
    }
    Code:
    //the main method class for the primitive stop watch, included for completion
    public class StopWatchTest {
    	
    	public static void main(String[] args){
    		StopWatch wnd = new StopWatch();
    		wnd.setLocation(25, 25);
    		wnd.setTitle("Stopwatch");
    		wnd.setVisible(true);
    
    	}
    
    }

    I believe I have gotten everything right so far, however I have one big problem: as you can see here

    Code:
                while(timer.isRunning()){
                	
                	try {
    					Thread.sleep(10);
    				} catch (InterruptedException e1) {
    					e1.printStackTrace();
    				}
                	
                	testTextField.setText("");
                	
                	hunSec++;
                	
                	if(hunSec == 9){
                		hunSec = 0;
                		tenSec++;
                	}
                	
                	if(tenSec == 9){
                		tenSec = 0;
                		sec++;
                	}
                	
                	testTextField.setText(sec + (":") + tenSec + hunSec);
                	
                	
    
                }
    I have the incrementation of the stop watch values within a (basically infinite) while loop and I cannot for the life of me figure out how to properly include an interrupt into the whole thing so that it stops when I click on the stop button.

    Any help on this subject would be greatly appreciatedan if you have any other suggestions or further information is required please don't hesitate to let me know.

    Thanks in advance,
    Dän
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    received help from another source concerning this exercise
    Could you tell us where else is this question posted?

    stops when I click on the stop button.
    Add a boolean variable that both methods can see. Test it in the loop for a changed value and set it in the listener.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    8
    Rep Power
    0
    Originally Posted by NormR
    Could you tell us where else is this question posted?
    This is so far the only forum I have posted my question.


    Originally Posted by NormR
    Add a boolean variable that both methods can see. Test it in the loop for a changed value and set it in the listener.
    I've tried that. I put the loop in an external method which is called when I click the START button, but then I get the same reaction (or, more precisely, the complete lack thereof) from the other buttons.

    Here's what I've changed (for comparison see my post above):

    Code:
    	//ActionListener code for the buttons
    	@Override
    	public void actionPerformed(ActionEvent e) {
    		
    		//Starts the primitive stop Watch. Every 1/100th of a second the time displayed is incremented
    		//previously contained the while loop as seen below
            if(e.getSource() == this.starter){
            	timer.start();
            	isActive = true;
            	this.timeRunner();
    
            }
            
            
            //This button is *SUPPOSED* to terminate the incrementation, effectively terminating the stop watch.
            //
            if(e.getSource() == this.stopper){
                timer.stop();
                isActive = false;
    
            }
            
            //resets the timer and all stop watch components to 0 if the timer is *not* running. Otherwise it does nothing.
            if(e.getSource() == this.resetter){
                if(timer.isRunning() == false){
            		timer.restart();
            		hunSec = 0;
            		tenSec = 0;
            		sec = 0;
            		}
                else{}
    
            }
    		
    
    
    	}
    	
    	//This method contains the while loop previously placed within the ActionListener
    	private void timeRunner(){
        	while(isActive==true){
        		try {
        			Thread.sleep(1);
        		} catch (InterruptedException e1) {
        			e1.printStackTrace();
        		}
        		
        		testTextField.setText("");
        		
        		hunSec++;
        		
        		if(hunSec == 9){
        			hunSec = 0;
        			tenSec++;
        		}
        		
        		if(tenSec == 9){
        			tenSec = 0;
        			sec++;
        		}
        	
        		testTextField.setText(sec + (":") + tenSec + hunSec);
        	}
       	}
    The isActive boolean is instanced with the other attributes and is set to false by standard.
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    Does the program react to any button presses after you have Pressed the Start button?
    For example can the window be closed when the timer is running?

IMN logo majestic logo threadwatch logo seochat tools logo