title: Adding a mouseListener/ mouseEvent to each object in an objects list
body: <p>I am trying to add a mouselistener to each object that is in an objects list. I am rendering a frame as well as multiple objects, using canvas and graphics g.</p>
  
  <p>This is my main class: this renders the frame as well as the objects. This all works well and there is no problem here.</p>
  
  <pre><code>import java.awt.Canvas;
  import java.awt.Color;
  import java.awt.Graphics;
  import java.awt.image.BufferStrategy;
  
  public class Proj2 extends Canvas implements Runnable{
  
      private final int[][] Init_board = 
            {{1,0,1,0,1,0,1,0},
             {0,1,0,1,0,1,0,1},
             {1,0,1,0,1,0,1,0},
             {0,1,0,1,0,1,0,1},
             {1,0,1,0,1,0,1,0},
             {0,1,0,1,0,1,0,1},
             {1,0,1,0,1,0,1,0},
             {0,1,0,1,0,1,0,1}};
  
      private final int[][] new_Game = 
            {{5,5,5,5,5,5,5,5},
            {5,5,5,5,5,5,5,5},
            {0,0,0,0,0,0,0,0},
            {0,0,0,0,0,0,0,0},
            {0,0,0,0,0,0,0,0},
            {0,0,0,0,0,0,0,0},
            {4,4,4,4,4,4,4,4},
            {4,4,4,4,4,4,4,4}};
  
      public static final int WIDTH = 640;
      public static final int HEIGHT = 640;
      private final int initPieces = 8;
  
     private Thread thread;
    private boolean running = false;
  
      private Board board;
      private GameHandler handler;    
  
      public Proj2(){
  
          handler = new GameHandler();
          new Window(WIDTH,HEIGHT,"Checkers",this);
          board = new Board(WIDTH,HEIGHT,Init_board);
  
           for(int i = 0; i &lt; 8; i++){
              for(int j = 0; j &lt; 8; j++){
  
                handler.newObject(new Player(j * (640 / initPieces),i * (640 /
                initPieces),initPieces,ID.playerOne,new_Game[i][j],handler));
  
               }
           }
  </code></pre>
  
  <p>This is where I'm not sure if I am doing it right. I have created the objects above, and render them to the frame. Then I try to add a mouslistener to each object so that they can each have their own MouseEvent. This is still in my main class.</p>
  
  <pre><code>for(int i = 0; i &lt; handler.gameObjects.size(); i++){
  
      GameObjects temp = handler.gameObjects.get(i);
  
      this.addMouseListener(new MouseInput(temp));
  
     }
  
  }
  </code></pre>
  
  <p>The rest renders the frame and starts the game loop.</p>
  
  <p>This is my handler class. This class creates an objects list, and adds and removes objects when required.</p>
  
  <pre><code>import java.awt.Graphics;
  import java.util.LinkedList;
  
  public class GameHandler {
  
      LinkedList&lt;GameObjects&gt; gameObjects = new LinkedList&lt;GameObjects&gt;();
  
      public void tick(){
          for(int i = 0; i &lt; gameObjects.size(); i++){
              GameObjects temp = gameObjects.get(i);
              temp.tick();
          }
      }
  
      public void render(Graphics g){
          for(int i = 0; i &lt; gameObjects.size(); i++){
              GameObjects temp = gameObjects.get(i);
              temp.render(g);
          }
      }
  
      public void newObject(GameObjects object){
  
          this.gameObjects.add(object);
  
      }
  
      public void deleteObject(GameObjects object){
  
          this.gameObjects.remove(object);
      }
  }
  </code></pre>
  
  <p>This is the MouseInput class: Where each object is sent to and has a MouseEvent. What I want here is that each object should have a different MouseEvent ID, so when it is clicked on it returns a unique ID for each object. However at this moment it returns the same value for all objects. I want to be able to select and object, by clicking on it, and then click on a new position and that object should move there.</p>
  
  <pre><code>    import java.awt.event.MouseEvent;
      import java.awt.event.MouseListener;
      import java.util.LinkedList;
  
  
      public class MouseInput implements MouseListener {
  
              private GameHandler handler;
              private GameObjects object;
  
              public MouseInput(GameObjects Object){
  
              this.object = Object;
  
  
              }
  
  
  
              @Override
              public void mouseClicked(MouseEvent arg0) {
  
              }
  
              @Override
              public void mouseEntered(MouseEvent e) {
  
              }
  
              @Override
              public void mouseExited(MouseEvent arg0) {
  
              }
  
              @Override
              public void mousePressed(MouseEvent e) {
  
  
  
                  int x = e.getX();
                  int y = e.getY();
                  int b = e.getClickCount();
  
                  int c = e.getID();
  
                  /*NOT SURE WHAT TO DO HERE*/
  
  
                  /*THIS JUST MOVES THE OBJECT WHERE I CLICK, BUT WHEN I HAVE MULTIPLE OBJECTS AND I CLICK, ONLY ONE OBJECT MOVES AND THE REST DISAPEAR*/
                      if(b == 2){
                          if(object.getID() == ID.playerOne ){
  
                              object.setX(x);
                              object.setY(y);
                          }
                      }
  
  
  
              }
  
  
  
  
  
              @Override
              public void mouseReleased(MouseEvent arg0) {
                  // TODO Auto-generated method stub
  
              }
  
          }
  </code></pre>
  
  <p>Apologies for all the code. I tried to post as little as possible.</p>