0

I am working on a project for school and I need to have all of my classes as separate files. Thus, I'm extending all of the classes that I previously had as subclasses to the main class. The only problem is, one of the classes that extends the main class is USED in the constructor of the main class, resulting in stackoverflow errors. How can I do this without errors?

Main code constructor:

public Minesweeper()
{
    setSize(400,200);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setTitle("Minesweeper");
    //This sets up the menu that allows the user to start either a small, medium, or large game.
    JMenu gameSize = new JMenu("Select Size");
    MenuItemListener l = new MenuItemListener();
    JMenuItem small = new JMenuItem("Small");
    small.addActionListener(l);
    gameSize.add(small);
    JMenuItem medium = new JMenuItem("Medium");
    medium.addActionListener(l);
    gameSize.add(medium);
    JMenuItem large = new JMenuItem("Large");
    large.addActionListener(l);
    gameSize.add(large);
    JMenuBar menubar = new JMenuBar();
    menubar.add(gameSize);
    setJMenuBar(menubar);       
}

Class that is extending:

public class MenuItemListener extends Minesweeper implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent arg0) {
        String command = arg0.getActionCommand();
        switch(command){
        //This runs when the user selects small.
        case "Small":
            if(notInGame){
                notInGame = false;
                smallG = true;
                JPanel top = new JPanel();
                time = new JLabel();
                //The timer is started and displayed below.
                timer = true;
                mines = new JLabel();
                mines.setText("Mines: 10");
                top.setLayout(new BorderLayout());
                top.add(time, BorderLayout.WEST);
                top.add(mines, BorderLayout.EAST);
                top.setSize(400, 20);
                JPanel game = new JPanel();
                game.setLayout(new GridLayout(9, 9));
                game.setSize(400, 420);
                setSize(400, 440);
                setTitle("Minesweeper");

                for(int i = 1; i <= 9; i++){
                    for(int j = 1; j <= 9; j++){
                        //This creates all of the buttons.
                        JButton b = new JButton();
                        b.addActionListener(new MyButtonListener());
                        b.addMouseListener(new MyButtonListener());
                        /*Each of the things below are like "labels" for buttons.  Using putClientProperty() will allow you to assign
                         * a specific "category" of information to a button and then assign information to that category.
                         * In this case I keep track of the x and y positions, Whether or not the button is a mine, whether the
                         * button has been clicked (enabled), and whether the button is currently flagged.                  
                         */
                        b.putClientProperty("row", i);
                        b.putClientProperty("column", j);
                        b.putClientProperty("mines", 0);
                        b.putClientProperty("enabled", 0);
                        b.putClientProperty("flag", 0);
                        small.add(b);
                        smallNM.add(b);
                        game.add(b);
                    }   
                }

                for(int i = 1; i <= 10; i++){
                    //This creates the mines and adds them to the Mine array.
                    Random rn = new Random();
                    int rand = rn.nextInt(smallNM.size());
                    smallNM.get(rand).putClientProperty("mines", 1);
                    smallM.add(smallNM.get(rand));
                    smallNM.remove(rand);
                }                   

                add(top, BorderLayout.NORTH);
                add(game, BorderLayout.CENTER);
                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                break;
            }
            else{
                System.out.println("Please exit the program and restart if you wish to play again.");

            }
            break;

        //This runs when the user selects small.
        case "Medium":
            if(notInGame){
                notInGame = false;
                mediumG = true;
                JPanel top = new JPanel();
                time = new JLabel();
                //The timer is started
                timer = true;
                mines = new JLabel();
                mines.setText("Mines: 40");

                top.setLayout(new BorderLayout());
                top.add(time, BorderLayout.WEST);
                top.add(mines, BorderLayout.EAST);
                top.setSize(700, 20);
                JPanel game = new JPanel();
                game.setLayout(new GridLayout(16, 16));
                game.setSize(700, 600);
                setSize(700, 620);

                setTitle("Minesweeper");
                for(int i = 1; i <= 16; i++){
                    for(int j = 1; j <= 16; j++){
                        //The buttons are created and labeled.
                        JButton b = new JButton();
                        b.addActionListener(new MyButtonListener());
                        b.addMouseListener(new MyButtonListener());
                        b.putClientProperty("row", i);
                        b.putClientProperty("column", j);
                        b.putClientProperty("mines", 0);
                        b.putClientProperty("enabled", 0);
                        b.putClientProperty("flag", 0);
                        game.add(b);
                        medium.add(b);
                        mediumNM.add(b);
                    }   
                }

                for(int i = 1; i <= 40; i++){
                    //The mines are created.
                    Random rn = new Random();
                    int rand = rn.nextInt(mediumNM.size());
                    mediumNM.get(rand).putClientProperty("mines", 1);
                    mediumM.add(mediumNM.get(rand));
                    mediumNM.remove(rand);
                }

                add(top, BorderLayout.NORTH);
                add(game, BorderLayout.CENTER);

                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                break;
            }
            else{
                System.out.println("Please exit the program and restart if you wish to play again.");

            }
            break;

        //This runs when the user selects "large" from the menu.
        case "Large":
            if(notInGame){
                notInGame = false;
                largeG = true;
                JPanel top = new JPanel();
                time = new JLabel();
                //The timer starts
                timer = true;
                mines = new JLabel();
                mines.setText("Mines: 99");

                top.setLayout(new BorderLayout());
                top.add(time, BorderLayout.WEST);
                top.add(mines, BorderLayout.EAST);
                top.setSize(1400, 20);
                JPanel game = new JPanel();
                game.setLayout(new GridLayout(16, 30));
                game.setSize(1400, 600);
                setSize(1400, 620);

                setTitle("Minesweeper");
                for(int i = 1; i <= 16; i++){
                    for(int j = 1; j <= 30; j++){
                        //The buttons are created and labeled.
                        JButton b = new JButton();
                        b.addActionListener(new MyButtonListener());
                        b.addMouseListener(new MyButtonListener());
                        b.putClientProperty("row", i);
                        b.putClientProperty("column", j);
                        b.putClientProperty("mines", 0);
                        b.putClientProperty("enabled", 0);
                        b.putClientProperty("flag", 0);
                        game.add(b);
                        large.add(b);
                        largeNM.add(b);
                    }   
                }

                for(int i = 1; i <= 99; i++){
                    //The mines are created.
                    Random rn = new Random();
                    int rand = rn.nextInt(largeNM.size());
                    largeNM.get(rand).putClientProperty("mines", 1);
                    largeM.add(largeNM.get(rand));
                    largeNM.remove(rand);
                }

                add(top, BorderLayout.NORTH);
                add(game, BorderLayout.CENTER);

                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                break;
            }
            else{
                System.out.println("Please exit the program and restart if you wish to play again.");
            }
            break;
        }
    }       
}

The reason I am extending it is because I need to have all of the classes using the same variables, and I can't do this without extending. Please recommend the least complicated solution you can think of.

Jesse Wiatrak
  • 73
  • 1
  • 1
  • 6
  • 4
    Your design pattern is seriously wrong. Not every file has to extend another file. `MenuItemListener` doesn't have to extend `MineSweeper` in your example. – Code Different May 28 '15 at 21:37
  • 2
    *Please recommend the least complicated solution you can think of.*, how about just about any other way that is not doing what you are doing. You need read up on extremely remedial object oriented theory and how to create instances of classes and pass them around. –  May 28 '15 at 21:41
  • 2
    There's no reason to share data only via subclass access to private member variables... have you considered having your classes interact by passing parameters to methods of each other? This is the normal way that components are made to interact. – Nick May 28 '15 at 21:41

0 Answers0