我正在尝试为对象列表中的每个对象添加一个mouselistener。我正在渲染一个帧以及多个对象,使用画布和图形g.
这是我的主要类:它渲染帧和对象。这一切都很好,这里没有问题。
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));
}
}
这就是我不确定我做得是否正确的地方。我已经创建了上面的对象,并将它们渲染到框架中。然后,我尝试为每个对象添加一个mouslister,这样它们就可以各自拥有自己的MouseEvent。这仍然在我的主课上。
for(int i = 0; i < handler.gameObjects.size(); i++){
GameObjects temp = handler.gameObjects.get(i);
this.addMouseListener(new MouseInput(temp));
}
}
其余部分渲染帧并开始游戏循环。
这是我的处理程序类。此类创建对象列表,并在需要时添加和删除对象。
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类:其中每个对象都被发送到并具有一个MouseEvent。我想要的是,每个对象都应该有一个不同的MouseEvent ID,所以当点击它时,它会为每个对象返回一个唯一的ID。然而,此时它为所有对象返回相同的值。我希望能够选择和对象,点击它,然后点击一个新的位置,对象应该移动到那里。
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
}
}
对所有代码表示歉意。我尽量少发帖。
我试着给每个对象添加一个mouslister,这样它们就可以各自拥有自己的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));
因此当点击它时,它为每个对象返回一个唯一的ID。
如果你想知道哪个对象被点击了,你可以从鼠标事件中获得信息:
GameObjects temp = (GameObjects)e.getSource();