Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i'm trying to code a Minesweeping game in java and i don't know how to create the board.

What I have tried:

this is my code:
Java
<pre>import javax.swing.*;
import java.awt.*;

public class Board extends JFrame
{
    static final int Col=9;
    static final int Raw=9;
   

    public Board(String title)
        {
            super(title);
            setVisible(true);
            setSize(300,300);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            InitBoard();

        }

        public void InitBoard()
        {
            Board b;
            Button[][] myboard=new Button[Raw][Col];
            Panel p =new Panel();
            p.setLayout(new GridLayout(Raw,Col));
            for (int i=0; i<Col;i++)
            {
                for (int j=0; j<Raw; j++)
                {
                    ImageIcon ic = new ImageIcon("mine.png");
                    Image im=ic.getImage();              
                    myboard[i][j]= new Button(im);
                    p.add(myboard[i][j]);
                    add(p);
                }
            }


        }

    public static void main(String[] args)
    {
        new Board("Minesweeper");

    }


class Button:

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

public class Button extends JButton {
    private Image img;

    public Button(Image img)
    {

        this.img = img;
    }
    public Image getIm()
    {

        return this.img;
    }

    public void SetIm(Image im)
    {
        this.img=im;
    }

    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        g.drawImage(img, 0, 0, getWidth(), getHeight(),null);
    }
}
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900