title: Why is this java applet code not picking up every MouseClicked event?
body: <p>I'm following this [slightly old tutorial for 2d java games here][1].</p>
  
  <p>I have a basic applet that runs in a thread with a mouselistener.</p>
  
  <p>On left button click I can shoot up to 10 claypigeons (balls) from the bottom of the window. On right button click I "shoot" and if I hit a pigeon it is removed from the screen.</p>
  
  <p>I've noticed however that sometimes right clicks are not getting picked up. This is not necessarily when there is a lot going on on the screen, although never at the beginning before it all kicks off. At worst it can take 3 or 4 clicks before one is registered.</p>
  
  <p>I'm guessing that I'm doing something obviously wrong in my code, but I'm not sure what. My first thought was the for loops that loop through every object every frame to recalculate their position or check if they have been "shot"? Could they been "blocking" the mouselistener?! </p>
  
  <p>Any tips on how to debug this are welcome!</p>
  
  <p><em><strong></em>**<em>*</em>**<em>*</em>**<em>*</em></strong><em>EDIT</em><strong><em>*</em>**<em>*</em>**<em>*</em>**<em>*</em>*</strong></p>
  
  <p>Ok I've taken the very good advice given below and reduced the code to the smallest nutshell that reproduces the bug. I think I had it in my head that it was all the for loops and the complexity that were causing the problems which is why I included so much in my first code.</p>
  
  <p>So as it happens I can reproduce this bug with almost no code, but at the same time it the bug is much milder. With the code below I just have the basic applet and the mouselistener and on right click a count increments in console and prints its value to screen. This works fine most of the time, but every now and again, a right click is "lost" and not registered.</p>
  
  <p>With the full class, I could sometimes get sequences of 3 or 4 or more right clicks not being registered, so the bug was much more obvious. Anyway code is below, only one class this time:</p>
  
  <p>Main class code:</p>
  
  <pre><code>package javacooperation;
  
  import java.applet.*;
  import java.awt.*;
  import java.awt.event.*;
  import java.util.*;
  import java.net.*;
  
  public class ClayPigeonGame extends Applet implements Runnable, MouseListener {
      //counters and flags for debugging
      private boolean flag = true;
      private int click_count = 0;
      private boolean game_running;
  
      public void init(){
          //set boolean for while loop
          game_running=true;
          addMouseListener(this);
      }
      public void start() {
          //threading this applet.. why?
          Thread th = new Thread(this);
          //does this call the run method in the class?
          th.start();
      }
  
      public void stop(){
          game_running=false;
      }
  
      public void destroy() { }
  
      public void run(){
          while(game_running) {
              //updatePigeonPosition();
              repaint();
              try{
                  //stop thread for 20 milliseconds
                  Thread.sleep(20);
              } catch (InterruptedException ex){ }
          }
      }
  
      public void paint(Graphics g){  }
  
      public void mouseClicked(MouseEvent e) {
              switch (e.getButton()){
                  case MouseEvent.BUTTON1:
                  case MouseEvent.BUTTON2:
                      break;
                  case MouseEvent.BUTTON3:
                      click_count ++;
                      System.out.println("Right click count: " + click_count);
                      break;
                  default:
                      break;
              }
      }
      //all the mouse listening events required because we implemented MouseListener
      public void mouseReleased(MouseEvent e) { }
      public void mouseEntered(MouseEvent e) { }
      public void mousePressed(MouseEvent e) { }
      public void mouseExited(MouseEvent e) { }
  }
  </code></pre>