1

这一直让我感到困惑,我有一块板,我希望能够将棋子从一个地方移动到另一个地方。我已经将框架设置为网格布局,每个网格单元都有一个新的 JPanel 实现mouseListener ..如何注册点开始和结束,以便我可以移动棋盘格?我无法将我所在的点保存到一个临时点,因为每当我单击另一个点时它都会改变..

这是代码:

public class Tiles extends JPanel implements MouseListener {

Color c2, cc, tmp;
boolean isWhite, hasChecker, isHighlighted;
int i, j;
ArrayList<Point> al = new ArrayList<Point>();
ArrayList<Point> TempArray = new ArrayList<Point>();
Point start;
Point temp;

public Tiles(Color c, Point s){

     this.setSize(75, 75);
     this.setLayout(null);
     this.addMouseListener(this);
     this.c2 = c;
     tmp = c2;
     this.start = s;

  }

public void setWhite(boolean isWhite){
    this.isWhite = isWhite;
}

public void hasChecker(boolean hasChecker){
    this.hasChecker = hasChecker;
}

public void paintComponent(Graphics g){
    Graphics2D g2 = (Graphics2D)g;
    super.paintComponent(g2);
    //if(isClicked == true)
    //  highlightPossibleMoves(start);
    drawTile(g2);
    if(hasChecker == true)
        addCheckers(g2);



}

public void drawTile(Graphics2D g2){

    g2.setColor(c2);

    g2.fillRect(3, 3, 75, 75);

}

public void addCheckers(Graphics2D g2){
    if(isWhite == true){
        g2.setColor(Color.white);
    }
    else{
        g2.setColor(Color.black);
    }
    Ellipse2D.Double circle = new Ellipse2D.Double(13, 11, 50, 50);
    g2.fill(circle);
}

public boolean highlightPossibleMoves(Point start){
    al = LOA.b.getPossibleMoves(start);
    if(!al.isEmpty()){
        for(int i = 0; i<al.size(); i++){
            LOA.Jboard[al.get(i).getY()][al.get(i).getX()].c2 = Color.green;
            LOA.Jboard[al.get(i).getY()][al.get(i).getX()].repaint();
        }
        return true;
    }
    return false;
}

public Point getThisPoint(){
    return this.start;
}

public boolean isPossible(Point p){
    if (TempArray.contains(p)){
        System.out.println("contains");
        return true;
    }
    return false;
}

    @Override
public void mouseClicked(MouseEvent e) {

}

@Override
public void mouseEntered(MouseEvent arg0) {

}

@Override
public void mouseExited(MouseEvent arg0) {

}

@Override
public void mousePressed(MouseEvent e) {

}

@Override
public void mouseReleased(MouseEvent e) {
        if(isHighlighted = true){
            LOA.initBoard();
        }
        if(highlightPossibleMoves(getThisPoint()) == true){
            this.isHighlighted = true;
        }           
        if(isPossible(getThisPoint()))
            LOA.move(this.temp, getThisPoint());
        else{
            System.out.println("x =" + this.start.getX() + "y =" + this.start.getY());
            this.temp = new Point(getThisPoint().getX(), getThisPoint().getY());
        }
        this.repaint();
        this.validate();                        
}
}
4

2 回答 2

2

我可以为您提供相同的业务逻辑:

  • 使用网格布局创建板
  • 而不是 JPanels,我认为 JButtons(根据它们的位置设置 JButton 的名称 a1、a2、a3..)
  • 用户单击按钮后,可能会将所选按钮颜色更改为绿色(您可以轻松存储所选按钮名称并知道选择了哪个按钮..您还可以将计数更新为一个以跟踪该部分还没搬家)
  • 此后,当用户单击另一个按钮时,即用户选择另一个有效的网格..您可以在板上移动

这样您就不必担心保存点而是保存选定的组件

于 2013-05-10T00:22:20.630 回答
1

你需要在你的 mousePressed(...) 方法中有某个地方,

if (start != null) {  // second press
  // second point pressed
  //.... do some junk

  start = null; then set start to null

} else { // first press
   start = ...;
}
于 2013-05-10T00:20:42.613 回答