I am just trying to construct a gameboard with tiles to work with. I got it to work by painting the tile into rows and columns with BufferedImage, but I am going to need to constantly update the GUI as the game is played.
So I tried to add the tile image to an ImageIcon, then to a JLabel array so that I could set the position. After that I would add the labels to the gui JPanel, but they had these default gaps between them.
Am I approaching this incorrectly? I am very new to Swing and Java
public static void main (String[] args) throws IOException{
int NUMROWS = 10;
int NUMCOLS = 10;
int NUMMINES = 10;
int[] mineList = GameBoard.setMines(NUMMINES, NUMROWS, NUMCOLS);
int[][] cellNums = GameBoard.setCellNum(NUMROWS, NUMCOLS);
boolean[][] mineField = GameBoard.getMineField(mineList, cellNums);
int[][] adjacentMineVals = GameBoard.getAdjacentMineVals(cellNums, mineField);
ImageIcon img = new ImageIcon(ImageIO.read(new File("GraphicFile\\Cell.png")));
JLabel[][] label = new JLabel[NUMROWS][NUMCOLS];
for (int i = 0; i < NUMROWS; i++){
for (int j = 0; j < NUMCOLS; j++){
label[i][j] = new JLabel();
label[i][j].setIcon(img);
label[i][j].setLocation(i*img.getIconHeight(), j*img.getIconWidth());
}
}
JFrame frame = buildFrame();
int fX = 2*frame.getInsets().left;
int fY = (frame.getInsets().top + frame.getInsets().bottom);
JPanel GUI = new JPanel();
GUI.setSize(NUMCOLS*img.getIconWidth(), NUMROWS*img.getIconHeight());
for (int i = 0; i < NUMCOLS; i++){
for (int j = 0; j < NUMROWS; j ++){
GUI.add(label[i][j]);
}
}
frame.setSize(NUMCOLS*img.getIconWidth() + fX, NUMROWS*img.getIconHeight() + fY);
frame.add(GUI);
}
public static JFrame buildFrame(){
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
return frame;
}
Here's what it gives me https://i.stack.imgur.com/rX3yM.png
Update, to new version. Previous problem has been solved by using a grid and putting the images into the buttons as icons
Here is what I have. It is not putting the label with the text onto the button that has been pressed. I debugged, and its receiving input for coords and for text, but its just not painting it onto the panel
public MinesweeperGraphics() throws IOException {
GUI.setOpaque(false);
for (int i = 0; i < NUMROWS; i++){
for (int j = 0; j < NUMCOLS; j++){
buttons[i][j] = new JButton();
buttons[i][j].setIcon(tileUp);
buttons[i][j].setBorder(null);
buttons[i][j].addActionListener(this);
buttons[i][j].setPressedIcon(tilePressed);
GUI.add(buttons[i][j]);
}
}
frame.add(GUI, BorderLayout.CENTER);
frame.add(reset, BorderLayout.NORTH);
reset.addActionListener(this);
frame.setResizable(false);
frame.pack();
GUI.setLayout(null);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(reset)){
for (int i = 0; i < NUMROWS; i++){
for (int j = 0; j < NUMCOLS; j++){
buttons[i][j].setIcon(tileUp);
}
}
}else {
for (int i = 0; i < buttons.length; i++) {
for (int j = 0; j < buttons.length; j++) {
if (e.getSource().equals(buttons[i][j])){
if (mineField[i][j] == false){
buttons[i][j].setIcon(tileEmpty);
numberText.setOpaque(false);
numberText.setSize(buttons[i][j].getWidth(), buttons[i][j].getHeight());
numberText.setText("a");
numberText.setLocation(buttons[i][j].getLocation());
GUI.add(numberText);
} else if (mineField[i][j] == true){
buttons[i][j].setIcon(tileExplodedMine);
}
//isn't working here
buttons[i][j].setEnabled(false);
}
}
}
}
}