JPanel不会在setLocation

时间:2017-05-30 17:28:10

标签: java swing

我想制作一个Ludo / Parcheesi游戏,所以我使用GridLayout(11x11)来添加棋子。

enter image description here

我有绿色,蓝色和黄色棋子的问题(只有红色棋子的颜色是正确的  并出现在新的setLocation)。在我掷骰子和setLocation pawn开始细胞后,pawn消失(我不知道为什么不在新位置重新绘制)。我将mouseClicked事件测试到getLocation个典当,并确定新位置是​​正确的,但不再出现典当。

这是Pawn类:

import javax.swing.*;
import java.awt.*;

public class Pawn extends JPanel {
    private int id;
    private Color color;
    private String sColor;
    private int position;
    private boolean isActive;
    private Point homeCoords;
    private Point currentCoords;

    //isActive = FALSE means pawn in HOME

    public Pawn(int id, String color, Point currentCoords) {
        this.currentCoords =

 new Point(currentCoords);
        this.id = id;
        position = -1;
        isActive = false;
        sColor = color;

    switch (color) {
        case "RED": {
            this.color = Color.red;
            break;
        }
        case "GREEN": {
            this.color = Color.green;
            break;
        }
        case "BLUE": {
            this.color = Color.blue;
            break;
        }
        case "YELLOW": {
            this.color = Color.yellow;
            break;
        }
    }
}

public void setInitialPosition() {
    Point p = getLocation();
    homeCoords = p;
//        System.out.println("[" + sColor + " " + id + "] = Location: " + homeCoords);
    currentCoords = new Point(homeCoords.x, homeCoords.y);
}

public void start() {
    position = 0;
    isActive = true;
    setPosition();
}

public void increasePosition(int moves) {
    position += moves;
    setPosition();
}

public boolean isActive() {
    return isActive;
}

public int getPosition() {
    return position;
}

public int getId() {
    return id;
}

public boolean getOut(Pawn pawn) {
    return ((currentCoords.getX() == pawn.currentCoords.getX()) && (currentCoords.getY() == pawn.currentCoords.getY()));
}

public void goHome() {
    position = -1;
    isActive = false;
    setPosition();
}

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);

    g.setColor(color);
    g.fillOval(0, 0, getWidth(), getHeight());
    setLocation((int) currentCoords.getX(), (int) currentCoords.getY());
//        System.out.println("[" + sColor + " " + id + "] = Location: " + currentCoords);
}

public void setPosition() {
    switch (sColor) {
        case "RED": {
            switch (position) {
                case -1: {
                    currentCoords = new Point((int) homeCoords.getX(), (int) homeCoords.getY());
                    setLocation((int) homeCoords.getX(), (int) homeCoords.getY());
                    break;
                }
                case 0: {
                    currentCoords = new Point(0, 274);
                    setLocation(0, 274);
                    break;
                }
                case 1: {
                    currentCoords = new Point(81, 274);
                    setLocation(81, 274);
                    break;
                }
               //and so on...
            }


    System.out.println("RED NEW POSITION: " + currentCoords);
                break;
            }
            case "GREEN": {
                switch (position) {
                    case -1: {
                        currentCoords = new Point((int) homeCoords.getX(), (int) homeCoords.getY());
                        setLocation((int) homeCoords.getX(), (int) homeCoords.getY());
                        break;
                    }
                    case 0: {
                        currentCoords = new Point(486, 2);
                        setLocation(486, 2);
                        break;
                    }
                    case 1: {
                        currentCoords = new Point(486, 70);
                        setLocation(486, 70);
                        break;
                    }
`//and so on..`.
                }
                System.out.println("GREEN NEW POSITION: " + currentCoords);
                break;
            }
            case "BLUE": {
                switch (position) {
                    case -1: {
                        currentCoords = new Point((int) homeCoords.getX(), (int) homeCoords.getY());
                        setLocation((int) homeCoords.getX(), (int) homeCoords.getY());
                        break;
                    }
                    case 0: {
                        currentCoords = new Point(324, 682);
                        setLocation(324, 682);
                        break;
                    }
    // and so on...

                System.out.println("YELLOW NEW POSITION: " + currentCoords);
                break;
            }
        }
    }
}

所以,我有一个JPanel玩家的棋子,MouseAdapter mouseClicked可以在玩家掷骰子后移动棋子X的位置:

    public Playground(int noPlayers) {

    //some JButtons and variable ...

        JPanel board = new JPanel(new GridLayout(11, 11));
        int redPawnCount = 0, greenPawnCount = 0, bluePawnCount = 0, yellowPawnCount = 0;

           //generate the board with pawns and cells

                Pawn temp = players.get(0).getPawn(redPawnCount++);
                pawns.add(temp);
                temp.addMouseListener(new MouseAdapter() {
                    @Override
                    public void mouseClicked(MouseEvent e) {
                        super.mouseClicked(e);
                        System.out.println("RED PAWN");

                        for (int i = 0; i < 4; i++)
                            System.out.println(players.get(0).getPawn(i).getLocation());

                        if (!hasMoved && playerTurn == 0) {
                            if (temp.isActive()) {
                                if ((temp.getPosition() + diceResult <= 43) && !checkFriendlyPawnsSamePosition(0, temp, temp.getPosition() + diceResult)) {
                                    temp.increasePosition(diceResult);
                                    checkForEnemy(0, temp);
                                    hasMoved = true;
                                } else {
                                    System.out.println("[INFO] Cannot move there.");
                                    return;
                                }
                            } else if (!temp.isActive() && diceResult == 6 && !checkFriendlyPawnsSamePosition(0, temp, temp.getPosition() + diceResult)) {
                                temp.start();
                                checkForEnemy(0, temp);
                                hasMoved = true;
                            } else {
                                if (diceResult == 6) {
                                    System.out.println("[INFO] The START cell is occupied.");
                                } else {
                                    System.out.println("[INFO] Roll 6 to START.");
                                }
                                return;
                            }
                        } else {
                            if (playerTurn != 0) {
                                System.out.println("[INFO] Not your turn.");
                            } else {
                                System.out.println("[INFO] You already moved.");
                            }
                        }
                    }
                });
                board.add(temp);

0 个答案:

没有答案