4

I am creating a mini game and I'm stuck. This grid is supposed to be nxn but in this case it's 6x6 until I figure things out.

Anyhow, I'd like to create a transparent border

enter image description here

which will add centered numbers above each cell (on the left side and up, however, later on I must add the rights side and down). What would be a good way to do this? I searched around because I know that chess boards have this kind of "border" and I actually searched them up, but with no luck.

This is a code snippet, the cells are contained in a simple JPanel while everything else is in the JFrame.

public GameFrame() {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (ClassNotFoundException | InstantiationException | IllegalAccessException
                    | UnsupportedLookAndFeelException ex) {
            }

            JFrame frame = new JFrame("Game");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLayout(new BorderLayout());
            frame.add(new GamePanel());
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);

            JMenuBar menubar = new JMenuBar();
            JMenu menu1 = new JMenu ("New");
            menubar.add(menu1);
            JMenu menu2 = new JMenu ("Load");
            menubar.add(menu2);
            JMenu menu3 = new JMenu ("Save");
            menubar.add(menu3);
            JMenu menu4 = new JMenu ("Size");
            menubar.add(menu4);
            JMenu menu5 = new JMenu ("Check");
            menubar.add(menu5);
            JMenu menu6 = new JMenu ("Solve");
            menubar.add(menu6);
            frame.setJMenuBar(menubar);
        }
    });

}

public class GamePanel extends JPanel {
    public GamePanel() {
        setLayout(new GridBagLayout());

        GridBagConstraints gbc = new GridBagConstraints();
        for (int row = 0; row < 6; row++) {
            for (int col = 0; col < 6; col++) {
                gbc.gridx = col;
                gbc.gridy = row;

                CellPanel cellPanel = new CellPanel();
                Border border = null;
                if (row < 5) {
                    if (col < 5) {
                        border = new MatteBorder(1, 1, 0, 0, Color.GRAY);
                    } else {
                        border = new MatteBorder(1, 1, 0, 1, Color.GRAY);
                    }
                } else {
                    if (col < 5) {
                        border = new MatteBorder(1, 1, 1, 0, Color.GRAY);
                    } else {
                        border = new MatteBorder(1, 1, 1, 1, Color.GRAY);
                    }
                }
                cellPanel.setBorder(border);
                add(cellPanel, gbc);
            }
        }
    }
}
public class CellPanel extends JPanel {
    Color defaultBackground;

    public CellPanel() {
        addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                defaultBackground = getBackground();

                if (getBackground().equals(Color.BLUE)) { 
                    setBackground(null);
                } else {
                    setBackground(Color.BLUE);
                }
            }

        });
    }

    public Dimension getPreferredSize() {
        return new Dimension(50, 50);
    }
}

Picture

user1803551
  • 12,965
  • 5
  • 47
  • 74
Intai'sei
  • 99
  • 7
  • You could add some `JLabel`s to the accordant place within your grid. – pzaenger Jan 13 '16 at 14:08
  • In the sourcecode the class for the `CellPanels` is missing. If you could add this class, then the program would be testable by SO users. – hamena314 Jan 13 '16 at 14:48
  • @pzaenger Alright. I'll check it out, thanks. – Intai'sei Jan 13 '16 at 15:30
  • @Intai'sei Could you add the part with the main method also? I think pzaenger is right, simply adding some labels might be the easiest way to accomplish your task. – hamena314 Jan 13 '16 at 16:06
  • @hamena314 The main method is just `new GameFrame();`. I'll try it out but I'm not sure how well it'll work when I try to assign a nxn grid. – Intai'sei Jan 13 '16 at 16:13
  • 1
    1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) Provide ASCII art or a simple drawing of the *intended* layout of the GUI at minimum size, and if resizable, with more width and height. 3) See also [Making a robust, resizable Swing Chess GUI](http://stackoverflow.com/q/21142686/418556). – Andrew Thompson Jan 13 '16 at 20:49

1 Answers1

0

Maybe try to add an extra column and row to your gridbaglayout. Then either control them as special cases in your for() construct or start your indexes at 1 and then go back and address them outside your for() construct.

Create another inherited JPanel that you can use as CellLabels, especially if they are all going to look alike.

essa
  • 155
  • 7