Calling a method from another class
I'm trying to be able to access the methods in the variable mineGui from the class Minebutton.
MinesweeperGUI (mineGui is from this class).
Code:
package MSweep;
import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.math.*;
public class MinesweeperGUI extends JFrame {
private Minebutton[][] mineGrid;
private static JPanel GUI;
private int rows;
private int cols;
private boolean minesSet = false;
private static int totalMines;
private int flaggedMines = 0;
private int unopenedBoxes;
private int setMines;
private int count = 0;
private int qNum = 1;
private int checkRow;
private int checkCol;
private int[][] queueList = new int[10][2];
public MinesweeperGUI() {
//GRID Variables Declared (Rows, Columns, Mines)
rows = 8;
cols = 8;
totalMines = 10;
setMines = 0;
mineGrid = new Minebutton[20][20];
unopenedBoxes = (rows * cols);
int[][] queueList = new int[unopenedBoxes][2];
//Create initial Grid
for (int i = 0; i < rows; i++ ){
for (int j = 0; j < cols; j++ ) {
mineGrid[i][j] = new Minebutton(i, j);
}
}
//Create GUI
GUI = new JPanel();
GUI.setLayout(new GridLayout(rows, cols, 0, 0));
drawGrid();
add(GUI);
setTitle("Minesweeper");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setSize(350,350);
//Set mines
if (minesSet == false){
while (setMines < totalMines) {
int mineRow = ((int)(Math.random() * (rows) ));
int mineCol = ((int)(Math.random() * (cols) ));
if (mineGrid[mineRow][mineCol].getMined() == false) {
mineGrid[mineRow][mineCol].setMined(true);
setMines += 1;
}
}
minesSet = true;
}
//Draw Grid
}
public static void main() {
MinesweeperGUI mineGui = new MinesweeperGUI();
mineGui.setVisible(true);
} // end main()
public void drawGrid() {
GUI.removeAll();
for (int i = 0; i < rows; i++ )
for (int j = 0; j < cols; j++ )
{
GUI.add(mineGrid[i][j].getFace());
}
GUI.updateUI();
}
public void Opening(int checkRow, int checkCol) {
setQNum(getQNum() - 1);
mineGrid[checkRow][checkCol].setOpened(true);
unopenedBoxes =- 1;
count = 0;
if (unopenedBoxes == ((rows * cols) - totalMines))
Win();
for ( int i = 0; i < 3; i++ ) {
for ( int j = 0; j < 3; j++ ) {
try{
if (mineGrid[(checkRow + (i - 1))][(checkCol + (j - 1))].getOpened() == false) {
if (mineGrid[(checkRow + (i - 1))][(checkCol + (j - 1))].getMined() == true)
count += 1;
mineGrid[checkRow][checkCol].setShown(Integer.toString(count));
if (count > 0){
if (mineGrid[(checkRow + (i - 1))][(checkCol + (j - 1))].getOpened() == false) {
if (mineGrid[(checkRow + (i - 1))][(checkCol + (j - 1))].getMined() == false) {
makeQueue(qNum, (checkRow + (i - 1)), (checkCol + (j - 1)));
qNum += 1;
}
}
}
else{
runQueue(qNum);
}
}
}
catch (ArrayIndexOutOfBoundsException e){
System.out.println("Skipped out of bounds button");
}
}
}
}
public void makeQueue (int place, int qRow, int qCol){
queueList[place][0] = qRow;
queueList[place][1] = qCol;
}
public void runQueue(int placeHold){
while (placeHold != 0){
Opening(queueList[placeHold][0], queueList[placeHold][1]);
}
}
public void addGoodFlag(){
flaggedMines =+ 1;
if (flaggedMines == totalMines){
Win();
}
}
public void remGoodFlag(){
flaggedMines =- 1;
}
public static void Lose(){
Object[]options = {"Okay", "Quit"};
JOptionPane.showOptionDialog(null, "Would you like to play again", "You lost!", JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
}
public static void Win(){
Object[]options = {"Okay", "Quit"};
JOptionPane.showOptionDialog(null, "Would you like to play again", "You win!", JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
}
public int getQNum(){
return qNum;
}
public void setQNum(int setQNum){
qNum = setQNum;
}
}
I try to access mineGui on lines 97, 117, and 121 of Minesquare
Code:
package MSweep;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import MSweep.MinesweeperGUI.*;
public class Minebutton{
private boolean mined;
private int minecount;
private boolean opened;
private String shown;
private int[] location = new int[2];
private JButton showBtn;
private JLabel showLbl;
public Minebutton(int row, int col) {
setMined (false);
setOpened (false);
setShown (" ");
setMinecount (0);
setLocation (row, col);
showBtn = new JButton(getShown());
showLbl = new JLabel(getShown());
showBtn.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
showLbl.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
showLbl.setHorizontalAlignment(JTextField.CENTER);
}
//Getters and Setters
public boolean getMined(){
return mined;
}
public void setMined(boolean setmined){
this.mined = setmined;
}
public boolean getOpened(){
return opened;
}
public void setOpened(boolean setopened){
this.opened = setopened;
}
public String getShown(){
return shown;
}
public void setShown(String setShown){
this.shown = setShown;
}
public int getMinecount(){
return minecount;
}
public void setMinecount(int setMinecount){
this.minecount = setMinecount;
}
public JComponent getFace() {
if (getOpened() == true)
return showLbl;
else
return showBtn;
}
public void setLocation(int setRow, int setCol) {
this.location[0] = setRow;
this.location[1] = setCol;
}
public int[] getLocation(){
return location;
}
public void actionPerformed(ActionEvent e){
setOpened(true);
if (mined == true){
MinesweeperGUI.Lose();
}
else
mineGui.Opening(location[0], location[1]);
}
class RightClicker extends MouseAdapter {
public void mousePressed(MouseEvent e) {
if (e.isMetaDown()) {
JButton B = (JButton) e.getSource();
nextSymbol(B);
}
}
}
public void nextSymbol(JButton B) {
if (B.getText() == " ") {
B.setText("F");
if (mined == true)
mineGui.addGoodFlag();
} else if (B.getText() == "F") {
B.setText("?");
if (mined == true)
mineGui.remGoodFlag();
} else if (B.getText() == "F") {
B.setText("?");
} else {
B.setText(" ");
}
}
}
I get the following errors when I compile.
Code:
3 errors found:
File: C:\...\MSweep\Minebutton.java [line: 97]
Error: C:\...\MSweep\Minebutton.java:97: cannot find symbol
symbol : variable mineGui
location: class MSweep.Minebutton
File: C:\...\MSweep\Minebutton.java [line: 117]
Error: C:\...MSweep\Minebutton.java:117: cannot find symbol
symbol : variable mineGui
location: class MSweep.Minebutton
File: C:\...\MSweep\Minebutton.java [line: 121]
Error: C:\...\MSweep\Minebutton.java:121: cannot find symbol
symbol : variable mineGui
location: class MSweep.Minebutton