I'm a beginner at Java, here I'm trying to create a Minesweeper game (I still have to learn a lot, so I'm challenging myself by making this game with JLabel, not JButton or any things that is the same kind, by the way, the code is all by myself :> proudly I can say, haha). Anyway, I'm at the part where I need to detect if a slot contains a bomb, or a number, or none. You know, when playing, if you click on a slot that contains nothing, the game will automatically open all the nearby slot (which also contain nothing). My idea is to use MouseAdapter, so I can use MouseEvent.getSource(), or getButton(). But I'm literally stuck! Can someone figure out what do I need to do? It will be very thankful! (and so for my bad English, I'm not a native speaker)
First, I have to inform that I make two package in the same folder, one is for the display, or UI, and one is all the values that connects to the other one.
(Sorry I don't have enough reputation! So you would mind click the link to see my image)
This is the "Square" class, which is a JLabel, I want each of the slot inside the game to interact with player's mouse, so I implement the MouseListener here.
package Graphics;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Square extends JLabel implements MouseListener {
private static final long serialVersionUID = 1L;
public int squareWidth = 50, squareLength = 50;
Color foreGround;
Color grass1 = new Color(170, 215, 80);
Color grass2 = new Color(165, 210, 75);
Color dirt1 = new Color(230, 195, 160);
Color dirt2 = new Color(215, 185, 155);
public Square(int x, int y, Color fg, Color bg) {
foreGround = fg;
this.setForeground(bg);
this.setFont(new Font("ArcadeClassic", Font.BOLD, 40));
this.setHorizontalAlignment(JLabel.CENTER);
this.setPreferredSize(getPreferredSize());
this.setBounds(x, y, squareWidth, squareLength);
this.setBackground(bg);
this.setOpaque(true);
this.addMouseListener(this);
}
@Override
public void mouseClicked(MouseEvent e) {}
@Override
public void mousePressed(MouseEvent e) {}
@Override
public void mouseReleased(MouseEvent e) {
this.setForeground(foreGround);
if(this.getBackground().getRed() == 170) this.setBackground(dirt1);
if(this.getBackground().getRed() == 165) this.setBackground(dirt2);
System.out.println("" + this.getX() + " - " + this.getY());
}
@Override
public void mouseEntered(MouseEvent e) {}
@Override
public void mouseExited(MouseEvent e) {}
}
This is inside the "GamePanel" class, "extends JPanel", I'm just trying to initialize a 2-D ArrayList with datatype is Square, set the x-coord, y-coord, background color and foreground color for the slot.
package Graphics;
import java.awt.*;
import javax.swing.*;
import Logics.Field;
public class GamePanel extends JPanel {
private static final long serialVersionUID = 1L;
Color grass1 = new Color(170, 215, 80);
Color grass2 = new Color(165, 210, 75);
Color dirt1 = new Color(230, 195, 160);
Color dirt2 = new Color(215, 185, 155);
int row = 5, col = 5;
Square square[][] = new Square[row][col];
Field field = new Field(row, col);
public GamePanel() {
this.setBackground(new Color(75, 120, 45));
field.createBomb(3);
field.printValue();
int x = 20, y = 5;
int len = 0;
byte step = 0;
for(int i = 0; i < row; i++) {
for(int j = 0; j < col; j++) {
Color temp = null;
switch(field.getValue(i, j)) {
case 1: {
temp = Color.blue; break;}
case 2: {
temp = new Color(75, 120, 45); break;}
case 3: {
temp = Color.red; break;}
case 4: {
temp = new Color(125, 30, 165); break;}
case 5: {
temp = new Color(255, 145, 0); break;}
case 6: {
temp = new Color(0, 150, 170); break;}}
if(step == 0) {
square[i][j] = new Square(x, y, temp, grass1);
step = 1;}
else {
square[i][j] = new Square(x, y, temp, grass2);
step = 0;}
this.add(square[i][j]);
x += square[i][j].squareWidth;
len = square[i][j].squareLength;}
y += len;
x = 20;}
this.setSquareText();
this.setLayout(null);
}
public void setSquareText() {
for(int i = 0; i < row; i++) {
for(int j = 0; j < col; j++) {
if(field.getValue(i, j) == 0) square[i][j].setText("");
else if(field.getValue(i, j) == -1) square[i][j].setText("x");
else square[i][j].setText(Integer.toString(field.getValue(i, j)));
}
}
}
}
If needed, I will capture the Logics package for you guys! (Sorry if the code is hard to watch, I have to shorten it down so that I can capture it!)