I'm trying to remake Minesweeper, but I am having problems displaying the numbers behind the buttons once I add other components into the screen, like the mine counter.
In this particular case, I have two JPanels. My first JPanel will contain multiple components, such as the mine counter and another JPanel containing the board (an array of rows by columns of custom JButtons).
Before I added the mine counter, everything seemed to work fine, the numbers drew fine, the logic worked, etc. But that isn't where my problem lies.
In my custom JPanel (the panel that contains the label, board, etc.), I am over riding the paintComponent() method to print the numbers onto the screen. But, once I added the other JPanel, the one where I need to add the numbers onto the screen, I ran into some problems.
Firstly, I didn't know how to differentiate between which panel I was drawing on, but I think I found the fix with a line such as: "g = boardPanel.getGraphics()". If this works, I am having problems elsewhere. Mose likely with how I have structured my paintComponent() method, I'm unsure of how the order should look.
Here's some sort of code from what I have:
public void paintComponent(Graphics g) {
super.paintComponent(g);
g = boardPanel.getGraphics();
//Draw the lines and numbers behind and separating the tiles. These, in my
//mind, are drawn to the graphics object regarding the playable board (the
//board, not the panel including the mine counter, etc.) But, when I click
//on a tile, there are no mine indicators behind it.
}
I guess, in essence, I am just unsure of how to structure the paintComponent() method since I believe I have to mess with two different graphics objects, the one for the board panel and the one that holds the mine count and board panel.
Any help would be appreciated. Thanks.
You have to implement two different classes for each JPanel. Then they will have their own paintComponent() methods and you won't have to "guess" as to which JPanel you're painting on.
And I wouldn't implement each mine tile as a JButton, but that's another issue.