Search code examples
javauser-interfacegraphicsminesweeper

I am having trouble calling code from a class


So I am fairly new to java and I am working on a graphical user interface for a simple game of minesweeper. Originally I had all my code integrated into one class and instantiated my Panel and Frame in the same class, but my professor insisted I place my Frame and Panel in separate classes. I am now stuck on how to implement my panel in my frame. I attempted creating an instance of the class in my main method, but when I ran my program it just displayed an empty frame. Before I set up separate classes my code ran perfectly but now my panel is not being added to my frame. Am I missing something in my panel? Here is my code so far:

import java.awt.event.InputEvent;
import java.awt.*;
import java.util.Random;
import javax.swing.*;

public class Minesweeper 
{

        public static void main(String[] args)
        {


            Frame frame = new Frame();

            frame.getContentPane().add(new Panel());
            frame.setVisible(true);

        }
}

This is just the frame.

    class Frame extends JFrame
    {
        public Frame()
        {

            setTitle("Minesweeper");
            setExtendedState(JFrame.MAXIMIZED_BOTH);
            setLocationRelativeTo(null);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
        }
    }

And this is my panel class I am having trouble with.

    class Panel extends JPanel 
    {
        public Panel() 
            {
                super(); 
                this.setLayout(new BorderLayout());
                add(getJPanel());
                add(getJContentPane());

            }

            private int columns = 8;
            private int rows = 8;
            boolean jBombs[][] = new boolean[rows][columns];
            boolean jShown[][] = new boolean[rows][columns];
            int jCells[][] = new int[rows][columns];
            private int currX, currY = 0;
            JToggleButton jButtons[] = new JToggleButton[columns*rows];
            private JPanel jPanel = null;
            private JToolBar jToolBar = null;
            private JPanel jContentPane = null;
            private JButton jBtnNewGame = null;
            private JProgressBar jProgressBar = null;






            private JPanel getJPanel()
            {
                if (jPanel == null)
                {
                        jPanel = new JPanel();
                        jPanel.setLayout(new BorderLayout());
                        jPanel.add(getJContentPane(), BorderLayout.CENTER);
                        jPanel.add(getJProgressBar(), BorderLayout.SOUTH);

                }
                return jPanel;
            }



            private JPanel getJContentPane() 
            {
                if (jContentPane == null)
                {
                        GridLayout gridLayout = new GridLayout();
                        gridLayout.setRows(rows);
                        gridLayout.setColumns(columns);
                        jContentPane = new JPanel();
                        jContentPane.setLayout(gridLayout);
                        BuildBoard();
                }
                return jContentPane;
            }

            private void BuildBoard()
            {

                if(jProgressBar != null)
                {
                        jProgressBar.setValue(0);
                }
                jContentPane.removeAll();
                int i = 0;
                for(int x = 0; x < rows; x++)
                {
                        for(int y = 0; y < columns; y++)
                        {
                                currX = x;
                                currY = y;
                                Random randBomb = new Random();
                                jBombs[x][y] = randBomb.nextBoolean() && randBomb.nextBoolean() && randBomb.nextBoolean();
                                jButtons[i] = new JToggleButton("?");
                                jButtons[i].addMouseListener(new java.awt.event.MouseAdapter(){
                                        public void mouseReleased(java.awt.event.MouseEvent e) {
                                                if(e.getModifiers() == InputEvent.BUTTON3_MASK)
                                                {
                                                        markCell(e);
                                                }
                                                else if(e.getModifiers() == InputEvent.BUTTON1_MASK)
                                                {
                                                        showCell(e);
                                                }
                                        }
                                });
                                jContentPane.add(jButtons[i]);
                                i++;
                        }
                }
                for(int x = 0; x < rows; x++)
                {
                        for(int y = 0; y < columns; y++)
                        {
                                jCells[x][y] = bombCount(x, y);
                                jShown[x][y] = false; 
                        }
                }
                jContentPane.setEnabled(true);
                this.repaint();
                this.validate();
            }

            private JProgressBar getJProgressBar()
            {
                if (jProgressBar == null) 
                {
                        jProgressBar = new JProgressBar();
                        jProgressBar.setMaximum(columns * rows);
                }
                return jProgressBar;
            }


            private void showAllBombs()
            {
                for(int x = 0; x < rows; x++)
                {
                        for(int y = 0; y < columns; y++)
                        {
                                if(jBombs[x][y] == true)
                                {
                                        JToggleButton jButton = findButton(x,y);
                                        if(jButton.isEnabled()) 
                                        {
                                                jProgressBar.setValue(jProgressBar.getValue() + 1);
                                        }
                                        jButton.setText("X");
                                        jButton.setSelected(true);
                                        jButton.setEnabled(false);
                                }
                        }
                }
            }

            private void clearCells(int x, int y)
            {
                if(inBounds(x, y))
                {
                        if(!jShown[x][y] && jBombs[x][y] == false)
                        {
                                jShown[x][y] = true;
                                JToggleButton jButton = findButton(x,y);
                                if(jCells[x][y] > 0)
                                {
                                        jButton.setText(Integer.toString(jCells[x][y]));
                                }
                                else
                                {
                                        jButton.setText("");
                                }
                                if(jButton.isEnabled())
                                {
                                        jProgressBar.setValue(jProgressBar.getValue() + 1);
                                }
                                jButton.setSelected(true);
                                jButton.setEnabled(false);
                                if(jCells[x][y] == 0)
                                {
                                        for(int r = -1; r <= 1; r++)
                                        {
                                                for(int c = -1; c <= 1; c++)
                                                {
                                                        clearCells(x + r, y + c);
                                                }
                                        }
                                }
                        }
                }
            }

            private boolean inBounds(int x, int y)
            {
                return 0 <= x && x < jCells.length && 0 <= y && y < jCells[x].length;
            }

            private boolean isBomb(JToggleButton jButton)
            {
                int i = 0;
                for(int x = 0; x < rows; x++)
                {
                        for(int y = 0; y < columns; y++)
                        {
                                if(jButton == jButtons[i])
                                {
                                        currX = x;
                                        currY = y;
                                        return jBombs[x][y];
                                }
                                i++;
                        }
                }
                return false;
            }

            private void disableBoard()
            {
                for(int x = 0; x < rows; x++)
                {
                        for(int y = 0; y < columns; y++)
                        {
                                JToggleButton jButton = findButton(x,y);
                                jButton.setEnabled(false);
                        }
                }
            }

            private JToggleButton findButton(int x, int y)
            {
                return jButtons[(x*rows+y)];
            }

            private void showCell(java.awt.event.MouseEvent e)
            {
                JToggleButton jButton = (JToggleButton)e.getSource();
                if(jButton.isEnabled())
                {
                        jProgressBar.setValue(jProgressBar.getValue() + 1);
                        jButton.setEnabled(false);

                        if(isBomb(jButton))
                        {
                                showAllBombs();
                                jButton.setEnabled(false);
                                JOptionPane.showMessageDialog(null,"You lost " + Math.round((jProgressBar.getPercentComplete() * 100)) + "% through.", "You Lost!", JOptionPane.INFORMATION_MESSAGE);
                                disableBoard();
                                System.exit(0);
                        }

                        else
                        {
                                if(jCells[currX][currY] > 0)
                                {
                                        jButton.setText(Integer.toString(jCells[currX][currY]));
                                }
                                else if(jCells[currX][currY] == 0)
                                {
                                        clearCells(currX, currY);
                                }

                        }
                }
            }

            private int bombCount(int x, int y)
            {
                int bombCount = 0;
                for(int r = -1; r <= 1; r++)
                {
                        for(int c = -1; c <= 1; c++)
                        {
                                int newx = x + r;
                                int newy = y + c;
                                if(inBounds(newx, newy))
                                {
                                        if(jBombs[newx][newy] == true)
                                        {
                                                bombCount++;
                                        }
                                }
                        }
                }
                return bombCount;
            }

            private void markCell(java.awt.event.MouseEvent e) 
            {
                JToggleButton jButton = (JToggleButton)e.getSource();
                if(jButton.isEnabled())
                {
                        if(jButton.getText() != "!")
                        {
                                jButton.setText("!");                          
                        }
                        else
                        {
                                jButton.setText("");
                        }
                }
            }

    }

Solution

  • You never actually call anything that would add (or generate) content of your Panel class.

    In you Panel constuctor, try calling getJPanel and getJContentPane and actually add those panels to your Panel class.

    Also, this...

    Panel panel = new Panel();
    Frame frame = new Frame();
    
    frame.getContentPane().add(new Panel());
    

    makes no sense, you're creating two instances Panel, but are only using one. You could just use

    public static void main(String[] args) {
    
        Frame frame = new Frame();
    
        frame.getContentPane().add(new Panel());
        //...
    
    }
    

    instead.

    Also, instead of using...

    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    int width = screenSize.width;
    int height = screenSize.height;
    setSize(width, height);
    

    which could result in your frame been displayed beneath other OS elements (ie the task bar), you should use...

    setExtendedState(JFrame.MAXIMIZED_BOTH);
    

    instead.