Results 1 to 6 of 6
  1. #1
    andrewche is offline Member
    Join Date
    May 2011
    Posts
    2
    Rep Power
    0

    Default 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).

    Java 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

    Java 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.

    Java 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

  2. #2
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    4

    Default

    This is probably unrelated to your question, but I don't think the following lines do what you expect them to do:

    Java Code:
        unopenedBoxes =- 1;
        flaggedMines =- 1;
        flaggedMines =+ 1;
    These are legal, but x =- 1 parses as x = -1. If you want to do compound assignment, the equals sign has to go on the right: += or -=.

    Alternatively, since you're incrementing and decrementing by 1, you can use the ++ and -- operators.

    Edit: Also, you're comparing strings with ==. This also won't provide the result you're expecting, since it compares object references instead of string content. Use .equals() instead.
    Last edited by Iron Lion; 05-03-2011 at 07:04 PM.

  3. #3
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    Your mineGui is a local variable (declared inside a method) in the MinesweeperGUI class. So you can't access it inside other class even if you have the imports declared.

    Either make mineGui as instance variable in MinesweeperGUI class or re-declare it in the class where you want to use it.

    Hope that helps,

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  4. #4
    andrewche is offline Member
    Join Date
    May 2011
    Posts
    2
    Rep Power
    0

    Default

    Quote Originally Posted by Iron Lion View Post
    This is probably unrelated to your question, but I don't think the following lines do what you expect them to do:

    Java Code:
        unopenedBoxes =- 1;
        flaggedMines =- 1;
        flaggedMines =+ 1;
    These are legal, but x =- 1 parses as x = -1. If you want to do compound assignment, the equals sign has to go on the right: += or -=.

    Alternatively, since you're incrementing and decrementing by 1, you can use the ++ and -- operators.

    Edit: Also, you're comparing strings with ==. This also won't provide the result you're expecting, since it compares object references instead of string content. Use .equals() instead.
    Thank's for catching this! Would have taken me forever to figure that out.





    Quote Originally Posted by goldest View Post
    Your mineGui is a local variable (declared inside a method) in the MinesweeperGUI class. So you can't access it inside other class even if you have the imports declared.

    Either make mineGui as instance variable in MinesweeperGUI class or re-declare it in the class where you want to use it.

    Hope that helps,

    Goldest
    How would I make an instance variable?

    Also, thank you both for your responses!

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by andrewche View Post
    How would I make an instance variable?
    An instance variable is a non-static variable that is declared in the class, not in a constructor, method or other block:

    Java Code:
    class Foo
      private int bar = 3;  // **** I'm an instance variable!! ****
    
      public Foo() {
         int spam = 2; // *** I'm not since I'm local to a constructor
      }
    
      public void baz() {
         int barlicious = 100; // *** also not an instance variable as I'm declared in a method
      }
    }

  6. #6
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    Some details about variable types in Java : Java Variable Types

    Have a look!

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

Similar Threads

  1. Calling a method in another class
    By uncopywritable in forum New To Java
    Replies: 9
    Last Post: 10-22-2012, 04:01 PM
  2. Calling a method from another class
    By snoopy11 in forum New To Java
    Replies: 4
    Last Post: 03-13-2011, 04:18 AM
  3. Child-Class Calling a Method in a Parent-Class
    By Blah_ in forum New To Java
    Replies: 5
    Last Post: 09-29-2009, 02:48 AM
  4. Replies: 29
    Last Post: 09-25-2008, 07:55 PM
  5. Calling a method on original class from created class
    By kpedersen in forum Advanced Java
    Replies: 4
    Last Post: 08-20-2008, 12:25 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •