1

I have a Stratego board being drawn on a JPanel, being used to set up the player's pieces. After two different instances of these panels are correctly arranged by the players, the main board (that has the same format and layout, just will have different piece appearance and logical behavior) will show.

My issue is that the second input panel and the main board (which currently has no functionality or pieces on it) are SOMETIMES set up with extra width on the bottom and the right, causing the 10x10 grid to not take up the entire board space like it should.

The initial input panel for player 1 seems to be working fine, never having this problem. The second panel and the main panel only SOMETIMES have this issue, so I'm not entirely sure where this is stemming from.

Here is the main method which sets up the panels and stuff.

public class Core {

    public static void main(String[] args) {

        LogicInterpreter logic = new LogicInterpreter();

        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();

        InputFrame inputPlayer1 = new InputFrame(logic, 1, "red", 600, 600);
        inputPlayer1.setLocation(dim.width / 2 - inputPlayer1.getSize().width/2,
            dim.height / 2 - inputPlayer1.getSize().height / 2);

        while(!logic.isSetUp1()){
            //Just to make it work
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        //Now bring up board 2

        InputFrame inputPlayer2 = new InputFrame(logic, 2, "blue", 600, 600);
        inputPlayer2.setLocation(dim.width / 2 - inputPlayer2.getSize().width/2,
                dim.height / 2 - inputPlayer2.getSize().height / 2);

        while(!logic.isSetUp2()){
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        openBoards(logic);
    }

    public static void openBoards(LogicInterpreter logic) {
        try {
            Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
            MainBoard board = new MainBoard(logic);
            board.setLocation(dim.width / 2 - board.getSize().width / 2, dim.height / 2 - board.getSize().height / 2);
        } catch (Exception e) {
            System.exit(1);
        }
    }
}

Also, here is the code inside the input panels. I'm not sure what is relevant, so I can't take stuff out. Sorry. I will withhold the main board setup code because it is really the same.

public class InputFrame extends JFrame {
    private static final long serialVersionUID = 1L;
    private LogicInterpreter holder;
    private Panel2 jp;
    private int height, width;
    private Map<Integer, ArrayList<Integer>> lakeCoords = new HashMap<>();
    private List<Piece> pieces = new ArrayList<>();
    private int playernumber;
    private String playerColor;
    Piece selectedPiece;
    Piece secondSelectedPiece;
    boolean hidePieces = false;

    JButton submit = new JButton("SUBMIT");

    public void addCoords() {
        lakeCoords.put(3, new ArrayList<Integer>(Arrays.asList(6, 5)));
        lakeCoords.put(4, new ArrayList<Integer>(Arrays.asList(6, 5)));
        lakeCoords.put(7, new ArrayList<Integer>(Arrays.asList(6, 5)));
        lakeCoords.put(8, new ArrayList<Integer>(Arrays.asList(6, 5)));
    }

    public void createPieces() {
        int y = 1;

        if (playernumber == 2) {
            y = 7;
        }

        List<Integer> openValues = new ArrayList<>();

        openValues.add(1);
        openValues.add(2);
        openValues.add(11);
        openValues.add(12);
        for (int x = 0; x < 2; x++) {
            openValues.add(3);
        }
        for (int x = 0; x < 3; x++) {
            openValues.add(4);
        }
        for (int x = 0; x < 4; x++) {
            openValues.add(5);
            openValues.add(6);
            openValues.add(7);
        }
        for (int x = 0; x < 5; x++) {
            openValues.add(8);
        }
        for (int x = 0; x < 8; x++) {
            openValues.add(9);
        }
        for (int x = 0; x < 6; x++) {
            openValues.add(10);
        }

        Collections.sort(openValues);

        System.out.println(openValues.size());
        System.out.println(pieces.size());

        for (int x = 1; x <= 10; x++) {
            for (int z = y; z <= y + 3; z++) {  

                // 1x1 Marshal
                // 2x1 General
                // 3x2 Colonel
                // 4x3 Major
                // 5x4 Captain
                // 6x4 Lieutenant
                // 7x4 Sergeant
                // 8x5 Miner
                // 9x8 Scout
                // 10x6 Bomb
                // 11x1 Flag
                // 12x1 Spy 

                Piece piece = new Piece(new Coords(x, z), openValues.get(0), playerColor);

                openValues.remove(0);
                pieces.add(piece);
            }
        }
    }

    public InputFrame(LogicInterpreter holder, int playerNumber, String playerColor, int height, int width) {
        this.height = height;
        this.width = width;
        playernumber = playerNumber;
        this.playerColor = playerColor;
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        addCoords();
        this.holder = holder;
        createPieces();
        jp = new Panel2(height, width);
        setResizable(false);
        jp.setBackground(new Color(235, 202, 158));
        setTitle("Player " + playerNumber + " Arrangement GUI     ||     Click Submit When Ready");
        jp.setPreferredSize(new Dimension(600, 600));
        jp.setLayout(null);
        jp.addMouseListener(new HandleMouse());
        if(playernumber == 1)
            submit.setBounds(width / 10 * 4, height / 10 * 7, width / 10 * 2, height / 10 * 2);
        else
            submit.setBounds(width / 10 * 4, height / 10, width / 10 * 2, height / 10 * 2);
        submit.setFont(new Font("Arial", Font.BOLD, width * 20 / 600));
        submit.setBackground(Color.LIGHT_GRAY);
        submit.addActionListener(new CloseListener(this));
        jp.add(submit);
        getContentPane().add(jp);
        pack();
        setVisible(true);
    }

    class CloseListener implements ActionListener {
        private InputFrame frame;

        public CloseListener(InputFrame frame) {
            this.frame = frame;
        }

        public void actionPerformed(ActionEvent event) {
            // Do the stuff here before closing
            hidePieces = true;
            repaint();
            if (playernumber == 1) {
                holder.setP1Pieces(pieces);
                JOptionPane.showMessageDialog(null, "Press When Ready for Next Player");
                holder.setSetUp1(true);
            } else {
                holder.setP2Pieces(pieces);
                JOptionPane.showMessageDialog(null, "Press When Player 1 is Ready");
                holder.setSetUp2(true);
            }
            frame.dispose();
        }
    }

    public class Panel2 extends JPanel {

        private static final long serialVersionUID = 1L;
        int height = 0;
        int width = 0;

        public Panel2(int height, int width) {
            this.height = height;
            this.width = width;
        }

        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            for (int x = 0; x < width; x += width / 10) {
                for (int y = 0; y < height; y += height / 10) {
                    boolean fill = false;
                    for (Entry<Integer, ArrayList<Integer>> coords : lakeCoords.entrySet()) {
                        if ((coords.getKey() - 1 == x / 60 && coords.getValue().get(0) - 1 == y / 60)
                                || (coords.getKey() - 1 == x / 60 && coords.getValue().get(1) - 1 == y / 60)) {
                            fill = true;
                            break;
                        }
                    }
                    if (fill) {
                        g.setColor(Color.BLUE);
                        g.fillRect(x, y, width / 10, height / 10);
                        g.setColor(Color.BLACK);
                        g.drawRect(x, y, width / 10, height / 10);
                    } else {
                        g.setColor(Color.BLACK);
                        g.drawRect(x, y, width / 10, height / 10);
                    }
                }
            }

            if(hidePieces){
                for (Piece piece : pieces) {
                    try {
                        g.drawImage(ImageIO.read(new File(playerColor + "_pieces/" + (playerColor.equals("blue") ? "Blue" : "Red") + "_Strat_Piece"
                                + ".png")), piece.getX() * width / 10 - width / 10,
                                piece.getY() * height / 10 - height / 10, width / 10, height / 10, null);
                    } catch(Exception e){}
                }
            } else {
                for (Piece piece : pieces) {
                    g.drawImage(piece.getImage(), piece.getX() * width / 10 - width / 10,
                            piece.getY() * height / 10 - height / 10, width / 10, height / 10, null);
                }

                if (selectedPiece != null) {
                    g.setColor(Color.BLUE);
                    g.drawImage(selectedPiece.getImage(), selectedPiece.getX() * width / 10 - width / 10,
                            selectedPiece.getY() * height / 10 - height / 10, width / 10, height / 10, null);
                    g.drawRect(selectedPiece.getX() * width / 10 - width / 10,
                            selectedPiece.getY() * height / 10 - height / 10, width / 10, height / 10);
                }
            }
        }
    }

    private class HandleMouse extends MouseAdapter {
        public void mousePressed(MouseEvent e) {
            int x = e.getX();
            int y = e.getY();
            Coords coordinates = holder.getClickedBox(x, y, width, height);
            boolean found = false;
            boolean move = false;

            for (Piece piece : pieces) {
                if (piece.getX() == coordinates.getX() && piece.getY() == coordinates.getY()) {
                    found = true;
                    if (selectedPiece == null) {
                        selectedPiece = piece;
                    } else {
                        move = true;
                        secondSelectedPiece = piece;
                    }
                }
            }
            if (move) {
                pieces.remove(selectedPiece);
                pieces.remove(secondSelectedPiece);
                Coords storage = selectedPiece.getCoords();
                selectedPiece.setCoords(secondSelectedPiece.getCoords());
                secondSelectedPiece.setCoords(storage);
                pieces.add(selectedPiece);
                pieces.add(secondSelectedPiece);
                selectedPiece = null;
                secondSelectedPiece = null;
            } else if (!found) {
                if (selectedPiece != null) {
                    selectedPiece = null;
                }
            }
            repaint();
        }
    }
}
2
  • It's really hard to figure out what is going on especially since you're saying it's randomly being a problem. What have you tried in regards to debugging? Have you tried removing/setting anything to see if the problem persists?
    – KyleKW
    Nov 21, 2016 at 17:26
  • 1
    "SOMETIMES set up with extra width on the bottom and the right" Problems that occasionally manifest are often related to not starting the GUI on the EDT. For better help sooner, post a minimal reproducible example or Short, Self Contained, Correct Example. Nov 21, 2016 at 20:58

1 Answer 1

0

Did you check how many pixels are "sometimes" added?

When i call setResizable(true) on my jframe, i notice that "sometimes" there are 10 pixels more, both in width and height, than i expect.

If this is your case, i answered a similar question here. I was able to solve the problem using a 'trick', calling pack more than one time.

In the answer i also linked another similar question (this one), where i wasn't able to find another solution, but there some users described the bug.

1
  • You got it exactly! Thanks so much! Wasn't going to figure that one out any time soon...
    – user5554725
    Nov 22, 2016 at 15:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.