0

I am creating a snakes and ladders game. My issue is that I have two classes, one the main GUI in a JFrame for the game with an image of a snakes and ladders board, and the other a 2D array of a grid which I want to superimpose over the board game, so the squares in the image match the squares of the grid.

I figure I need to call it as an instance of the Grid class, but I cant seem to get it to work (or perhaps, placed in the correct position!). Can anybody help me?

Thanks in advance

GameBoard class:

public class GameBoard extends javax.swing.JFrame {
private JLabel Board;
private JLabel playerNumber;
private ButtonGroup group;
private JButton startButton;
private JRadioButton fourPlayer;
private JRadioButton threePlayer;
private JRadioButton twoPlayer;


/**
* Auto-generated main method to display this JFrame
*/
public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            GameBoard inst = new GameBoard();
            inst.setLocationRelativeTo(null);
            inst.setVisible(true);
        }
    });
}

public GameBoard() {
    super();
    initGUI();
}

private void initGUI() {
    try {
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        getContentPane().setLayout(null);
        {
            Board = new JLabel();
            getContentPane().add(Board);
            Board.setText("jLabel1");
            Board.setIcon(new ImageIcon(getClass().getClassLoader().getResource("images/board.jpg")));
            Board.setBounds(199, 0, 742, 484);
        }
        {
            playerNumber = new JLabel();
            getContentPane().add(playerNumber);
            playerNumber.setText("Number of Players");
            playerNumber.setBounds(40, 22, 117, 27);
        }
        {
            twoPlayer = new JRadioButton();
            getContentPane().add(twoPlayer);
            twoPlayer.setText("Two Player");
            twoPlayer.setBounds(40, 55, 93, 20);
        }
        {
            threePlayer = new JRadioButton();
            getContentPane().add(threePlayer);
            threePlayer.setText("Three Players");
            threePlayer.setBounds(40, 76, 88, 20);
        }
        {
            fourPlayer = new JRadioButton();
            getContentPane().add(fourPlayer);
            fourPlayer.setText("Four Players");
            fourPlayer.setBounds(40, 99, 82, 20);
        }
        {
            startButton = new JButton();
            getContentPane().add(startButton);
            startButton.setText("Start Game");
            startButton.setBounds(43, 136, 83, 23);
        }

        {
         //Group the radio buttons.
        ButtonGroup group = new ButtonGroup();
        group.add(twoPlayer);
        group.add(threePlayer);
        group.add(fourPlayer);
        }

        pack();
        this.setSize(963, 523);
    } catch (Exception e) {
        //add your error handling code here
        e.printStackTrace();
    }


    }



}

;

Grid class:

public class Grid {

int[][] multi = {
        {0,0,-1,0,0,-1,0,-1,0,0},
        {0,0,0,0,0,0,-1,0,0,0},
        {0,0,0,0,0,0,0,0,0,1},
        {0,-1,0,-1,0,0,0,0,0,0},
        {0,0,0,0,0,0,-1,0,0,1},
        {0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0},
        {1,0,0,0,0,0,0,1,0,0},
        {0,0,0,-1,0,0,0,0,0,0},
        {0,0,0,1,0,0,0,0,1,0}
};

}

1
  • Yes, I guess GameBoard needs an instance of Grid... Mar 26, 2015 at 3:07

1 Answer 1

1

I'm guessing that GameBoard will need an instance Grid in order for it to know where to place the game pieces.

You could change GameBoard so that it required an instance of Grid to be passed to it...

public class GameBoard extends javax.swing.JFrame {
    //...
    private Grid grid;
    public GameBoard(Grid grid) {
        this.grid = grid;
        //...

Then create and pass an instance of Grid when you create an instance of GameBoard...

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            Grid grid = new Grid();
            GameBoard inst = new GameBoard(grid);
            inst.setLocationRelativeTo(null);
            inst.setVisible(true);
        }
    });
}

But, I'd also add some functionality to Grid to control how it's modified, but that's me

I cant seem to get it work using your original method. I would like to just simply create an instance but for the life of me I can't seem to get it working

Seems to work okay for me...

import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

public class GameBoard extends javax.swing.JFrame {

    private JLabel Board;
    private JLabel playerNumber;
    private ButtonGroup group;
    private JButton startButton;
    private JRadioButton fourPlayer;
    private JRadioButton threePlayer;
    private JRadioButton twoPlayer;

    private Grid grid;

    /**
     * Auto-generated main method to display this JFrame
     */
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                Grid grid = new Grid();
                GameBoard inst = new GameBoard(grid);
                inst.setLocationRelativeTo(null);
                inst.setVisible(true);
            }
        });
    }

    public GameBoard(Grid grid) {
        super();
        this.grid = grid;
        initGUI();
    }

    private void initGUI() {
        try {
            setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
            getContentPane().setLayout(null);
            {
                Board = new JLabel();
                getContentPane().add(Board);
                Board.setText("jLabel1");
                Board.setIcon(new ImageIcon(getClass().getClassLoader().getResource("images/board.jpg")));
                Board.setBounds(199, 0, 742, 484);
            }
            {
                playerNumber = new JLabel();
                getContentPane().add(playerNumber);
                playerNumber.setText("Number of Players");
                playerNumber.setBounds(40, 22, 117, 27);
            }
            {
                twoPlayer = new JRadioButton();
                getContentPane().add(twoPlayer);
                twoPlayer.setText("Two Player");
                twoPlayer.setBounds(40, 55, 93, 20);
            }
            {
                threePlayer = new JRadioButton();
                getContentPane().add(threePlayer);
                threePlayer.setText("Three Players");
                threePlayer.setBounds(40, 76, 88, 20);
            }
            {
                fourPlayer = new JRadioButton();
                getContentPane().add(fourPlayer);
                fourPlayer.setText("Four Players");
                fourPlayer.setBounds(40, 99, 82, 20);
            }
            {
                startButton = new JButton();
                getContentPane().add(startButton);
                startButton.setText("Start Game");
                startButton.setBounds(43, 136, 83, 23);
            }

            {
                //Group the radio buttons.
                ButtonGroup group = new ButtonGroup();
                group.add(twoPlayer);
                group.add(threePlayer);
                group.add(fourPlayer);
            }

            pack();
            this.setSize(963, 523);
        } catch (Exception e) {
            //add your error handling code here
            e.printStackTrace();
        }

    }

}
6
  • As an example...You could also use a setter method or simply create an instance of Grid inside of GameBoard, but I like the ability to pass a new Grid to the GameBoard Mar 26, 2015 at 3:27
  • I cant seem to get it work using your original method. I would like to just simply create an instance but for the life of me I can't seem to get it working Mar 26, 2015 at 3:29
  • Thats the code I used and whilst it compiles fine, next to Private Grid grid; it says 'the value of the field GameBoard.grid is not used'? Mar 26, 2015 at 3:38
  • Yeah, have you used it anywhere, other then assigning it? Mar 26, 2015 at 3:46
  • Sorry if I sound silly, its late and my minds wandering! No, I dont think I have, i simply want to use the grid to be able to move my counters throughout each square, as if they were actually on the snakes and ladders board (which is just an image). - Does that even make sense haha? I've marked the question as answered however as you have done so Mar 26, 2015 at 4:04

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.