0

I am making a simple minesweeper game(no menus at top and other stuff) All i need to have is a 9x9 board and when each tile is clicked it either is blank if no near bombs, or says how many bombs are near it or says B for bomb.

I have all above correctly done and it works perfectly, my problem is that I need to put in a timer that starts from 0 and continually counts up from 0 while I play the game until i click on a bomb where it stops. Then I will have below my board a place where the user can put in their name and beneath it there is a button called submit which will std out the person's name and time. (Note the player can do this at anytime)

My problem is that whenever I try to add a JPanel or JLabel or anything really to my frame it won't be put on it and that my 9x9 grid keeps scaling to size of my window. How should I add a timer display and a submitting name part so i can see it on the window!

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;




public class MS extends JFrame implements ActionListener{

    static JFrame bframe;
    static JPanel p;
    static double[] bomb;


    public MS() {   
        p = new JPanel(new GridLayout(9,10));  

        JButton btn;
        bomb = new double[81];
        for(int i=0; i<81; i++)
            bomb[i] = Math.random();

        for (int i=0; i< 81; i++)  {
            btn = new JButton();
            btn.addActionListener(this);
            btn.setActionCommand(Integer.toString(i));
            p.add(btn);
        }
    }

    public static void main(String[] args) {
        bframe=new MS();    //CREATE me and
        bframe.add(timelabel);
        bframe.add(p);      //add the JPanel

        bframe.setLocation(32,32);              //where my upper left hand corner goes
                //bframe.setSize(64*width, 64*height);
                bframe.setSize(500,500);
        bframe.setVisible(true);                //I start out invisible
        bframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  //need this for the window manager
    }


    public void actionPerformed(ActionEvent e) {

        JButton jb = (JButton) e.getSource();
        int temp = Integer.parseInt(e.getActionCommand());

        System.out.printf("%s\n", e.getActionCommand());        

        int count = 0;
        String total = "";

        if(bomb[temp] > 0.15) {
            //for top line
            if((temp!=0) && (temp!=1) && (temp!=2) && (temp!=3) && (temp!=4) &&(temp!=5) && (temp!=6) && (temp!=7) && (temp!=8)) {
                if((temp!=0) && (temp!=9) && (temp!=18) && (temp!=27) && (temp!=36) && (temp!=45) && (temp!=54) && (temp!=63) && (temp!=72))
                    if(bomb[temp-10] <= 0.15) 
                        count++;
                if(bomb[temp-9] <= 0.15)
                    count++;
                if((temp!=8) && (temp!=17) && (temp!=26) && (temp!=35) && (temp!=44) && (temp!=53) && (temp!=62) && (temp!=71) && (temp!=80))
                    if(bomb[temp-8] <= 0.15)
                        count++;
            }
            if((temp!=0) && (temp!=9) && (temp!=18) && (temp!=27) && (temp!=36) && (temp!=45) && (temp!=54) && (temp!=63) && (temp!=72))
                if(bomb[temp-1] <= 0.15)
                    count++;    
            if((temp!=8) && (temp!=17) && (temp!=26) && (temp!=35) && (temp!=44) && (temp!=53) && (temp!=62) && (temp!=71) && (temp!=80))
                if(bomb[temp+1] <= 0.15)
                    count++;

            if((temp!=72) && (temp!=73) && (temp!=74) && (temp!=75) && (temp!=76) && (temp!=77) && (temp!=78) && (temp!=79) && (temp!=80)) {

                if((temp!=0) && (temp!=9) && (temp!=18) && (temp!=27) && (temp!=36) && (temp!=45) && (temp!=54) && (temp!=63) && (temp!=72))
                    if(bomb[temp+8] <= 0.15)
                        count++;
                if(bomb[temp+9] <= 0.15)
                    count++;
                if((temp!=8) && (temp!=17) && (temp!=26) && (temp!=35) && (temp!=44) && (temp!=53) && (temp!=62) && (temp!=71) && (temp!=80))
                    if(bomb[temp+10] <= 0.15)
                        count++;
            }
            if(count==0)
                jb.setText("");

            else {
                total=Integer.toString(count);
                jb.setText(total);              
                if(count==1) 
                    jb.setForeground(Color.blue);
                if(count==2)
                    jb.setForeground(Color.green);
                if(count==3)
                    jb.setForeground(Color.red);
            }
        }
        else
            jb.setText("B");
    }
}              

For clarity: What i wish to know is why is my 9x9 grid box's scaling with the window, how I can fix it, and how can i add it another 1 or 2 jpanels so I can complete the other parts of my project.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Kurt E
  • 367
  • 1
  • 2
  • 9
  • 1
    Are you _sure_ this is the code you are using? From what I see, this shouldn't even compile. For example, there is no constructor that matches `new MS(9, 9)` and `gameOver(boolean)` does not have a closing brace. –  Mar 13 '13 at 21:16
  • Oh, yes I just removed a few things so that my program was shorter and you all could see it easier, i just forgot to remove a couple of things – Kurt E Mar 13 '13 at 21:18
  • @KurtE If you need help with timer you need to use timer class. Follow this question to get help with it. [Swing timer not stopping](http://stackoverflow.com/questions/14409868/swing-timer-not-stopping/14410163#14410163) – Smit Mar 13 '13 at 21:24
  • @smit i need help on how to first add other things besides the 9x9 gird – Kurt E Mar 13 '13 at 21:29
  • @KurtE Actually you should not extend jframe. Make a new jpanel and then add all those components to it with specific layout same way you did with p jpanel. Set proper size for jpanels and jframe. Your design is completely insane. – Smit Mar 13 '13 at 21:37

1 Answers1

0

Try using this (untested):

public Test() {
    setContentPane(new JPanel(new BorderLayout()));
    JPanel p = new JPanel(new GridLayout(9, 9));
    JButton btn;
    bomb = new double[81];
    for (int i = 0; i < 81; i++)
        bomb[i] = Math.random();
    for (int i = 0; i < 81; i++) {
        btn = new JButton();
        btn.addActionListener(this);
        btn.setActionCommand(Integer.toString(i));
        p.add(btn);
    }
    getContentPane().add(p, BorderLayout.CENTER); // add the JPanel
    JPanel timerPanel = new JPanel();
    timerPanel.add(timelabel);
    getContentPane().add(timerPanel, BorderLayout.EAST);
    setLocation(32, 32); // where my upper left hand corner goes
    // bframe.setSize(64*width, 64*height);
    setSize(500, 500);
    setVisible(true); // I start out invisible
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // need this for the window manager
}

public static void main(String[] args) {
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            new Test();
        }
    });
}

All Swing-related events should be put on the Event Dispatch Thread, which is why you should use the invokeLater(Runnable) method. You need to use another parent panel so you can have the grid in the center and the timerPanel on the left. BorderLayout is probably the best layout manager for this. It adds the grid panel to the center and the timerPanel to the left. Anything related with timing should be added to the timerPanel. Any other components should be added like this:

getContentPane().add(yourComponent, constraint);

where constraint is one of BorderLayout.NORTH, BorderLayout.SOUTH, BorderLayout.WEST, BorderLayout.EAST, etc. Hope this helped!

  • It comes up as a problem when you call " timerPanel.add(timelabel, BorderLayout.EAST);" – Kurt E Mar 13 '13 at 23:09
  • In your main function do I have to do javax.swing? thing? or should i just do "new MS();" – Kurt E Mar 13 '13 at 23:24
  • @KurtE You should keep it like how it is now. It makes sure that the program is on the Event Dispatch Thread (EDT). All swing operations should be on the EDT. –  Mar 14 '13 at 01:57