Threads for Timing and Right Mouse Button

The following program shows how we can use a Thread to set up a timer. It just counts the seconds, minutes and hours while it is running.

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

public class Clock
{
   JFrame frame = new JFrame("Clock");
   Drawing draw = new Drawing();
   TimeKeeper time = new TimeKeeper();
   int hours=0, minutes=0, seconds=0;
   
   public Clock()
   {
      frame.add(draw,"Center");
      frame.setSize(300,300);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setVisible(true);
      time.start();
   }
   
   public static void main (String[] args)
   {
      new Clock();
   }

   class Drawing extends JComponent
   {
      public void paint(Graphics g)
      {
         g.drawString(hours + ":" + minutes + ":" + seconds, 100,100);
      }
   }
   
   class TimeKeeper extends Thread
   {
      public void run()
      {
         try
         {
            while (true)
            {
               sleep(1000); // update the time once a second
               seconds++;
               if (seconds > 59)
               {
                  seconds = 0;
                  minutes++;
                  if (minutes > 59)
                  {
                     minutes = 0;
                     hours++;
                  }
               }
               draw.repaint();
            }
         }
         catch (InterruptedException e)
         {
         }
      }
   }
} 

A variation on the previous example uses the right mouse button to start and stop the timer. In the MouseListener/MouseAdapter methods you use this statement:

if (e.isPopupTrigger()) 
to see if it was the right mouse button that was pressed.

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

public class Clock3 
{
   JFrame frame = new JFrame("Clock");
   Drawing draw = new Drawing();
   TimeKeeper time = new TimeKeeper();
   int hours=0, minutes=0, seconds=0;
   boolean stopped = false;
   
   public Clock3()
   {
      frame.add(draw,"Center");
      frame.setSize(300,300);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setVisible(true);
      frame.addMouseListener(new MouseListen());
      time.start();
   }
   
   public static void main (String[] args)
   {
      new Clock3();
   }
   
   class MouseListen extends MouseAdapter
   {
      public void mouseReleased(MouseEvent e)
      {
         if (e.isPopupTrigger()) // if right mouse button was pressed
            stopped = !stopped;  // toggle the timer start/stop
      }
   }
   
   class Drawing extends JComponent
   {
      public Drawing()
      {
         repaint();
      }
      
      public void paint(Graphics g)
      {
         g.drawString(hours + ":" + minutes + ":" + seconds, 100,100);
      }
   }
   
   class TimeKeeper extends Thread
   {
      public void run()
      {
         try
         {
            while (true)
            {
               sleep(1000);
               if (!stopped) // check if timer is running
               {
                  seconds++;
                  if (seconds > 59)
                  {
                     seconds = 0;
                     minutes++;
                     if (minutes > 59)
                     {
                        minutes = 0;
                        hours++;
                     }
                  }
                  draw.repaint();
               }
            }
         }
         catch (InterruptedException e)
         {
         }
      }
   }
}