0

I'm supposed to make a connect four game in gridworld with Pieces as the discs but my teacher for some reason told us nothing about the mouselistener! so i did look it up a little but i still cant figure out how to add a mouse listener to the grid to track mouse clicks.

PS: If you really want the code for the Piece class i can add it, and im pretty sure that World extends Jframe.

import java.awt.Color;    
import java.util.ArrayList;

import info.gridworld.grid.Grid;
import info.gridworld.grid.Location;
import info.gridworld.world.World;
import info.gridworld.grid.BoundedGrid;

import java.awt.MouseInfo;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class ConnectFourWorld extends World<Piece> implements MouseListener
{
   private String whosTurn;
   private boolean gameOver;
   private String winner;
   Piece X = new Piece("ex", Color.WHITE, Color.RED);
   Piece O = new Piece("oh", Color.YELLOW, Color.BLUE);


   public ConnectFourWorld()
   {
    super(new BoundedGrid<Piece>(6,7));

    winner="no winner";
    whosTurn="X";
    gameOver=false;
      setMessage("Welcome to Connect Four World!  - -  Click a spot - "+whosTurn+"'s             turn.");           
   }

   public boolean locationClicked(Location loc)
   {
        Grid<Piece> grid = getGrid();
      if(grid == null)
        return false;

      if(grid.get(loc)!=null)
        return false;

      if(gameOver == true)
      {

        return false;
      }



      return true;  
   }

   public Location addPiece(Location loc)
   {

      return null;  
   }

  public void step()
   {    
   }

   public boolean isWorldFull()
   {
    return false;
   }

   public void resetWorld()
   {
   }

   public String getWinner(Location loc)
   {
        return "";
   }

@Override
public void mouseClicked(MouseEvent e) {
    if(e.getButton()==1&&whosTurn.equals("X")){

    }
}

@Override
public void mouseEntered(MouseEvent e) {

}

@Override
public void mouseExited(MouseEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void mousePressed(MouseEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void mouseReleased(MouseEvent e) {
    // TODO Auto-generated method stub

    }
}
4

2 回答 2

0

不,World 类的实例变量是:

public class World<T>
{
    private Grid<T> gr;
    private Set<String> occupantClassNames;
    private Set<String> gridClassNames;
    private String message;
    private JFrame frame;

    private static Random generator = new Random();

    private static final int DEFAULT_ROWS = 10;
    private static final int DEFAULT_COLS = 10;
    ...
}

所以实际上 World不是JFrame,但实际上一个,因为它是私有的,所以无法访问。但是,您可以创建自己的世界类,只需将私有JFrame更改为受保护的JFrame,然后对其进行扩展。然后您可以访问 JFrame 并通过使用添加鼠标侦听器

WorldVariableName.frame.addMouseListener(new listener).

PS 我一直认为在主类中创建一个单独的私有 MouseListener 类比在主类中实现 MouseListener 更好。

于 2014-01-31T21:35:45.173 回答
0

您可以使用反射来绕过可访问性限制,但如果您甚至不需要创建自己的 World 类,这超出了您的范围。

Fields[] fields = World.class.getDeclaredFields();

for(Field f: fields){
    if(f.getName().equalsIgnoreCase("frame")){
        try{
            f.setAccessible(true);
            JFrame frame = (JFrame) f.get(worldInstance);
            f.setAccessible(false);
            return frame;
        }catch(IllegalArgumentException | IllegalAccessException e){
            e.printStackTrace();
        }
   }
}

return null;

这就是我强制抓取 JFrame 所做的,因此我可以不受限制地使用点击和按键。

于 2014-06-06T17:20:56.233 回答