将 mouseListener/ mouseEvent 添加到对象列表中的每个对象

Adding a mouseListener/ mouseEvent to each object in an objects list

我正在尝试为对象列表中的每个对象添加一个鼠标侦听器。我正在渲染一个框架以及多个对象,使用 canvas 和图形 g.

这是我的主要 class:这会渲染框架和对象。这一切都很好,这里没有问题。

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 < 8; i++){
            for(int j = 0; j < 8; j++){

              handler.newObject(new Player(j * (640 / initPieces),i * (640 /
              initPieces),initPieces,ID.playerOne,new_Game[i][j],handler));

             }
         }

这是我不确定我是否做对的地方。我已经创建了上面的对象,并将它们渲染到框架中。然后我尝试向每个对象添加一个 mouslistener,以便它们每个都可以有自己的 MouseEvent。这还在我的主class.

for(int i = 0; i < handler.gameObjects.size(); i++){

    GameObjects temp = handler.gameObjects.get(i);

    this.addMouseListener(new MouseInput(temp));

   }

}

其余部分渲染帧并开始游戏循环。

这是我的经纪人class。 class 创建一个对象列表,并在需要时添加和删除对象。

import java.awt.Graphics;
import java.util.LinkedList;

public class GameHandler {

    LinkedList<GameObjects> gameObjects = new LinkedList<GameObjects>();

    public void tick(){
        for(int i = 0; i < gameObjects.size(); i++){
            GameObjects temp = gameObjects.get(i);
            temp.tick();
        }
    }

    public void render(Graphics g){
        for(int i = 0; i < 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);
    }
}

这是 MouseInput class:每个对象都被发送到并有一个 MouseEvent。我在这里想要的是每个对象都应该有一个不同的 MouseEvent ID,所以当它被点击时 returns 每个对象都有一个唯一的 ID。但是此时它 returns 所有对象的值都相同。我希望能够 select 和对象,通过单击它,然后单击一个新位置,该对象应该移动到那里。

    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

            }

        }

对所有代码表示歉意。我尽量post。

I try to add a mouslistener to each object so that they can each have their own MouseEvent.

for(int i = 0; i < handler.gameObjects.size(); i++)
{
    GameObjects temp = handler.gameObjects.get(i);
    this.addMouseListener(new MouseInput(temp));
}

该代码将 MouseListener 添加到同一个对象,而不是不同的对象。相反,我猜代码应该是:

//this.addMouseListener(new MouseInput(temp));
temp.addMouseListener(new MouseInput(temp));

so when it is clicked on it returns a unique ID for each object.

如果您想知道单击了哪个对象,可以从 MouseEvent 中获取该信息:

GameObjects temp = (GameObjects)e.getSource();