10 Replies - 7379 Views - Last Post: 22 June 2010 - 10:39 AM Rate Topic: -----

#1 saSBH2  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 02-March 10

my Minesweeper Program

Posted 19 June 2010 - 11:04 AM

I should write the Minesweeper game Code. I am sure I will have a lot of problem. so it is my topic for asking Question.

My first Question:
Why doesn't the method "controlButtonsEvent" work?

My Code:
import javax.swing.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class newGui{
	GridLayout g;
	JFrame f;
	JPanel p1=new JPanel();
	JPanel p2=new JPanel();
	JPanel p3=new JPanel();
	static String [][]code=new String [9][9];
	static JButton[][] myButtons;
	public static void main(String[]args){
		newGui myB=new newGui();
		myB.draw();
		newGui.read();
		myB.controlButtonsEvent();
	}
	public void draw()
	{
		f=new JFrame();
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		p1.setPreferredSize(new Dimension(162,20));
		p1.setMaximumSize(new Dimension(162,20));
		f.getContentPane().add(BorderLayout.NORTH,p1);
		p2.setBackground(Color.lightGray);
		p2.setPreferredSize(new Dimension(162,45));
	    p2.setMaximumSize(new Dimension(162, 45));
	    f.getContentPane().add(BorderLayout.CENTER, p2);
	    p3.setPreferredSize(new Dimension(162,160));
	    p3.setMaximumSize(new Dimension(162,160));
		f.getContentPane().add(BorderLayout.SOUTH,p3);
		p3.setLayout((g=new GridLayout(9,9)));
		myButtons=new JButton[9][9];
		for(int j=0;j<9;j++)
			for(int i=0;i<9;i++)
			{
				myButtons[j][i]=new JButton();
				p3.add(myButtons[j][i]);
			}
		f.setSize(170,260);
		f.setVisible(true);
	}
	public static void read()//read from a file
	{
		Scanner s=null;
		try{
			s=new Scanner (new File("minesweeper.txt"));
			for(int i=0;i<9;i++)
				for(int j=0;j<9;j++)
					code[i][j]=s.next();
		}
		catch(IOException e){}
	}
	public void controlButtonsEvent()
	{
		myButtons[0][0].addActionListener(new Listener00());
		//add action listener to other buttons
	}
	class Listener00 implements ActionListener{

		public void actionPerformed(ActionEvent arg0) {
			// TODO Auto-generated method stub
			if(code[0][0]=="b")
				myButtons[0][0].setBackground(Color.LIGHT_GRAY);
		}
	}
	//here goes other inner class for other buttons
}


File "minesweeper.txt" is like this:
(b for blank, and * as bombs)
b b b 1 1 2 * 1 b
b 1 1 2 * 2 1 1 b
b 1 * 2 1 1 b b b
1 2 3 2 1 b b b b
1 * 2 * 2 1 b b b
1 1 2 2 * 1 b b b
b b b 1 2 2 1 b b
b 1 2 2 2 * 1 1 1
b 1 * * 2 1 1 1 *


Is This A Good Question/Topic? 0
  • +

Replies To: my Minesweeper Program

#2 saSBH2  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 02-March 10

Re: my Minesweeper Program

Posted 19 June 2010 - 11:04 AM

I should write the Minesweeper game Code. I am sure I will have a lot of problem. so it is my topic for asking Question.

My first Question:
Why doesn't the method "controlButtonsEvent" work?

My Code:
import javax.swing.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class newGui{
	GridLayout g;
	JFrame f;
	JPanel p1=new JPanel();
	JPanel p2=new JPanel();
	JPanel p3=new JPanel();
	static String [][]code=new String [9][9];
	static JButton[][] myButtons;
	public static void main(String[]args){
		newGui myB=new newGui();
		myB.draw();
		newGui.read();
		myB.controlButtonsEvent();
	}
	public void draw()
	{
		f=new JFrame();
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		p1.setPreferredSize(new Dimension(162,20));
		p1.setMaximumSize(new Dimension(162,20));
		f.getContentPane().add(BorderLayout.NORTH,p1);
		p2.setBackground(Color.lightGray);
		p2.setPreferredSize(new Dimension(162,45));
	    p2.setMaximumSize(new Dimension(162, 45));
	    f.getContentPane().add(BorderLayout.CENTER, p2);
	    p3.setPreferredSize(new Dimension(162,160));
	    p3.setMaximumSize(new Dimension(162,160));
		f.getContentPane().add(BorderLayout.SOUTH,p3);
		p3.setLayout((g=new GridLayout(9,9)));
		myButtons=new JButton[9][9];
		for(int j=0;j<9;j++)
			for(int i=0;i<9;i++)
			{
				myButtons[j][i]=new JButton();
				p3.add(myButtons[j][i]);
			}
		f.setSize(170,260);
		f.setVisible(true);
	}
	public static void read()//read from a file
	{
		Scanner s=null;
		try{
			s=new Scanner (new File("minesweeper.txt"));
			for(int i=0;i<9;i++)
				for(int j=0;j<9;j++)
					code[i][j]=s.next();
		}
		catch(IOException e){}
	}
	public void controlButtonsEvent()
	{
		myButtons[0][0].addActionListener(new Listener00());
		//add action listener to other buttons
	}
	class Listener00 implements ActionListener{

		public void actionPerformed(ActionEvent arg0) {
			// TODO Auto-generated method stub
			if(code[0][0]=="b")
				myButtons[0][0].setBackground(Color.LIGHT_GRAY);
		}
	}
	//here goes other inner class for other buttons
}


File "minesweeper.txt" is like this:
(b for blank, and * as bombs)
b b b 1 1 2 * 1 b
b 1 1 2 * 2 1 1 b
b 1 * 2 1 1 b b b
1 2 3 2 1 b b b b
1 * 2 * 2 1 b b b
1 1 2 2 * 1 b b b
b b b 1 2 2 1 b b
b 1 2 2 2 * 1 1 1
b 1 * * 2 1 1 1 *

Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10376
  • View blog
  • Posts: 38,415
  • Joined: 27-December 08

Re: my Minesweeper Program

Posted 19 June 2010 - 12:41 PM

Your problem is that you are comparing Strings in your ActionListener using the == operator here if(code[0][0]=="b"). As with other types of Objects, you should compare Strings using the .equals() method instead of the == operator, as the equals() method compares the contents of the Strings, while the == operator compares memory addresses. So: if(code[0][0].equals("b")). Also, you only add the listener to and validate for a single block.
Was This Post Helpful? 1
  • +
  • -

#4 m-e-g-a-z  Icon User is offline

  • Winning
  • member icon


Reputation: 496
  • View blog
  • Posts: 1,453
  • Joined: 19-October 09

Re: my Minesweeper Program

Posted 19 June 2010 - 05:57 PM

This is wrong.

myButtons[0][0].addActionListener(new Listener00());

You can add the action listener to all the buttons the same way as you inserted each JButton within the array and added it in the panel.

The algorithms for this is pretty simple, one thing I would recommend you to do is each time you click a button, you will have a for loop iterating to check which button was clicked. You would then get the indexes j and i of the button and pass this to a method which you can create called 'validate'. This method will then compare the indexes of the button clicked to the indexes of the grid. You can then check what the index of the element holds and apply conditions such as set text and color to the JButton or terminate the game if a bomb was found.

If you need more info, dont hesitate to ask :)
Was This Post Helpful? 1
  • +
  • -

#5 saSBH2  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 02-March 10

Re: my Minesweeper Program

Posted 20 June 2010 - 01:33 AM

View Postm-e-g-a-z, on 19 June 2010 - 04:57 PM, said:

If you need more info, dont hesitate to ask :)


Thanks. I add .equals() method and it works. But about what you recommended. Do you mean this?
// import
public class MineSweeper{
	static int x,y, z,k;
	
	//Codes above
	
	public void controlButtonsEvent()
	{
		for(int i=0;i<9;i++)
			for(int j=0;j<9;j++)
			{
				myButtons[i][j].addActionListener(new ButtonListener());
				z=i;
				k=j;
			}
				
	}
	class ButtonListener implements ActionListener{

		public void actionPerformed(ActionEvent arg0) {
			// TODO Auto-generated method stub
			x=z;
			y=k;
		}
	}
	public void validate()
	{
		//working with x and y
	}
}

Was This Post Helpful? 0
  • +
  • -

#6 m-e-g-a-z  Icon User is offline

  • Winning
  • member icon


Reputation: 496
  • View blog
  • Posts: 1,453
  • Joined: 19-October 09

Re: my Minesweeper Program

Posted 20 June 2010 - 06:23 AM

You have made things more hard for yourself by creating variables you dont need. Why do you need an inner class for the ActionListener? You can simply add the ActionListener to all the buttons when you insert them within the array and add them to the panel, it makes sense doing it that way. Your class newGui would implement the Action Listener interface which can then override the ActionPerformed method. Im not sure why you have static methods/members. Remember when you create a static method, its members need to have the static modifier which there is no need since you are instantiating the newGui object in the main() and invoking the methods.

Heres an updated version of the code, I will leave you to do the checking.

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

 public class newGui implements ActionListener{
	 
	GridLayout g;
	JFrame f;
	JPanel p1=new JPanel();
	JPanel p2=new JPanel();
	JPanel p3=new JPanel();
	//changed from static to non-static
	String [][]code=new String [9][9];
	JButton[][] myButtons;
	
	public static void main(String[]args){
		newGui myB=new newGui();
		myB.draw();
		myB.read();

	}
	public void draw()
	{
		f=new JFrame();
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		p1.setPreferredSize(new Dimension(162,20));
		p1.setMaximumSize(new Dimension(162,20));
		f.getContentPane().add(BorderLayout.NORTH,p1);
		p2.setBackground(Color.lightGray);
		p2.setPreferredSize(new Dimension(162,45));
		p2.setMaximumSize(new Dimension(162, 45));
		f.getContentPane().add(BorderLayout.CENTER, p2);
		p3.setPreferredSize(new Dimension(162,160));
		p3.setMaximumSize(new Dimension(162,160));
		f.getContentPane().add(BorderLayout.SOUTH,p3);
		p3.setLayout((g=new GridLayout(9,9)));
		myButtons=new JButton[9][9];
		for(int j=0;j<9;j++)
			for(int i=0;i<9;i++)
			{
				myButtons[j][i]=new JButton();
				p3.add(myButtons[j][i]);
				myButtons[j][i].addActionListener(this);
			}
		f.setSize(170,260);
		f.setVisible(true);
	}
	//changed from static to non-static
	public void read()//read from a file
	{
		Scanner s=null;
		try{
			s=new Scanner (new File("minesweeper.txt"));
			for(int i=0;i<9;i++)
				for(int j=0;j<9;j++)
					code[i][j]=s.next();
		}
		catch(IOException e){
			e.printStackTrace();
		}
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		//loop through each button
		for(int i=0;i<9;i++){
			for(int j=0;j<9;j++){
				//see which one was clicked
				if(e.getSource()==myButtons[i][j]){
					//set color and disable the button so it cannot be clicked.
					myButtons[i][j].setBackground(Color.DARK_GRAY);
					myButtons[i][j].setEnabled(false);
					/*create method validate to check i and j within the code array.
					 * You will simply pass these indexes and do the checking. 
					 */
				}

			}
		}
	}
}



Was This Post Helpful? 1
  • +
  • -

#7 saSBH2  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 02-March 10

Re: my Minesweeper Program

Posted 21 June 2010 - 06:59 AM

Thanks for your help. It was very useful. I didn't use "this" before for my Listeners.

but I have another problem. I think it is simple but i didn't find any way.
I add these to my code and I also change "actionlistner" to "Mouselistener":
//import
public class newGui implements MouseListener{
	//
	//
	//
	public void controlButtonsEvent()
	{
		for(int i=0;i<9;i++)
			for(int j=0;j<9;j++)
				myButtons[i][j].addMouseListener(this);
	}
	public void mouseClicked(MouseEvent e) {
		for(int i=0;i<9;i++)
			for(int j=0;j<9;j++)
			{
				if(e.getSource()==myButtons[i][j])
				{
					if(SwingUtilities.isLeftMouseButton(e))
					{
						this.validate(i, j);
					}
				}
			}
	}
	public void validate(int x,int y)
	{
		System.out.println(code[x][y]);
		System.out.println(newGui.isNumber(code[x][y]));
		//isNumber is a method that returns true if code[x][y] is a number
		if(newGui.isNumber(code[x][y]))
		{
			myButtons[x][y].setText(code[x][y]);
			myButtons[x][y].setFont(new Font("Verdana",Font.BOLD,10));
			myButtons[x][y].setEnabled(false);
		}
		else
		{
			//other codes go here
		}
	}
}


My problem is that in a button that is a number I don't see any number. I only see "...". I change the Font Size in the button but no result.
:(
Was This Post Helpful? 0
  • +
  • -

#8 m-e-g-a-z  Icon User is offline

  • Winning
  • member icon


Reputation: 496
  • View blog
  • Posts: 1,453
  • Joined: 19-October 09

Re: my Minesweeper Program

Posted 21 June 2010 - 02:34 PM

Could you post your full code ? :) It will be easier for us to find out the problem by quickly running it.
Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10376
  • View blog
  • Posts: 38,415
  • Joined: 27-December 08

Re: my Minesweeper Program

Posted 21 June 2010 - 04:52 PM

Without seeing your entire code, I'm going to take a guess that this is due to sizing issues of the JButtons fitting onto the JPanel. When a GUI component cannot fit with the expanded text, it appends a ... instead.
Was This Post Helpful? 0
  • +
  • -

#10 saSBH2  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 02-March 10

Re: my Minesweeper Program

Posted 22 June 2010 - 01:51 AM

View Postm-e-g-a-z, on 21 June 2010 - 01:34 PM, said:

Could you post your full code ? :) It will be easier for us to find out the problem by quickly running it.


I am sorry.

import javax.swing.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class newGui implements MouseListener{
	GridLayout g;
	JFrame f;
	JPanel p1=new JPanel();
	JPanel p2=new JPanel();
	JPanel p3=new JPanel();
	String [][]code=new String [9][9];
	JButton[][] myButtons;
	public static void main(String[]args){
		newGui myB=new newGui();
		myB.draw();
		myB.read();
		myB.controlButtonsEvent();
	}
	public void draw()
	{
		f=new JFrame();
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		p1.setPreferredSize(new Dimension(162,20));
		p1.setMaximumSize(new Dimension(162,20));
		f.getContentPane().add(BorderLayout.NORTH,p1);
		p2.setBackground(Color.lightGray);
		p2.setPreferredSize(new Dimension(162,45));
	    p2.setMaximumSize(new Dimension(162, 45));
	    f.getContentPane().add(BorderLayout.CENTER, p2);
	    p3.setPreferredSize(new Dimension(162,160));
	    p3.setMaximumSize(new Dimension(162,160));
		f.getContentPane().add(BorderLayout.SOUTH,p3);
		p3.setLayout((g=new GridLayout(9,9)));
		myButtons=new JButton[9][9];
		for(int j=0;j<9;j++)
			for(int i=0;i<9;i++)
			{
				myButtons[j][i]=new JButton();
				p3.add(myButtons[j][i]);
			}
		f.setSize(170,260);
		f.setVisible(true);
	}
	public void read()//read from a file
	{
		Scanner s=null;
		try{
			s=new Scanner (new File("minesweeper.txt"));
			for(int i=0;i<9;i++)
				for(int j=0;j<9;j++)
					code[i][j]=s.next();
		}
		catch(IOException e){}
	}
	public void controlButtonsEvent()
	{
		for(int i=0;i<9;i++)
			for(int j=0;j<9;j++)
				myButtons[i][j].addMouseListener(this);
	}
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
		int i,j;
		for(i=0;i<9;i++)
			for(j=0;j<9;j++)
			{
				if(e.getSource()==myButtons[i][j])
				{
					if(SwingUtilities.isLeftMouseButton(e))
					{
						this.validate(i, j);
					}
				}
			}
	}
	public void mouseEntered(MouseEvent arg0) {}
	public void mouseExited(MouseEvent arg0) {}
	public void mousePressed(MouseEvent e) {}
	public void mouseReleased(MouseEvent arg0) {}
	public void validate(int x,int y)
	{
		System.out.println(code[x][y]);
		System.out.println(newGui.isNumber(code[x][y]));
		if(newGui.isNumber(code[x][y]))
		{
			myButtons[x][y].setText(code[x][y]);
			myButtons[x][y].setEnabled(false);
		}
		else
		{
			//other Code
		}
	}
	public static boolean isNumber(String str)
	{
		boolean t=false;
		String numbers[]={"1","2","3","4","5","6","7","8","9"};
		for(int i=0;i<9;i++)
		{
			if(str.equals(numbers[i]))
				t=true;
		}
		return t;
	}
}


View Postmacosxnerd101, on 21 June 2010 - 03:52 PM, said:

Without seeing your entire code, I'm going to take a guess that this is due to sizing issues of the JButtons fitting onto the JPanel. When a GUI component cannot fit with the expanded text, it appends a ... instead.


Yes, my problem is exactly what you guess.
I send my entire code.
:smile2:
Was This Post Helpful? 0
  • +
  • -

#11 m-e-g-a-z  Icon User is offline

  • Winning
  • member icon


Reputation: 496
  • View blog
  • Posts: 1,453
  • Joined: 19-October 09

Re: my Minesweeper Program

Posted 22 June 2010 - 10:39 AM

Why do you have your grid so small? no wonder why you get trailing dots...
It would be impossible to set text to the button you ciurrently had with it being properly visible. I've changed the size of your panel and frame and used setFont method on your button.

import javax.swing.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class newGui implements MouseListener{
        GridLayout g;
        JFrame f;
        JPanel p1=new JPanel();
        JPanel p2=new JPanel();
        JPanel p3=new JPanel();
        String [][]code=new String [9][9];
        JButton[][] myButtons;
        public static void main(String[]args){
                newGui myB=new newGui();
                myB.draw();
                myB.read();
                myB.controlButtonsEvent();
        }
        public void draw()
        {
                f=new JFrame();
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                p1.setPreferredSize(new Dimension(162,20));
                p1.setMaximumSize(new Dimension(162,20));
                f.getContentPane().add(BorderLayout.NORTH,p1);
                p2.setBackground(Color.lightGray);
                p2.setPreferredSize(new Dimension(162,45));
            p2.setMaximumSize(new Dimension(162, 45));
            f.getContentPane().add(BorderLayout.CENTER, p2);
            p3.setPreferredSize(new Dimension(162,160));
            p3.setMaximumSize(new Dimension(500,500));
                f.getContentPane().add(BorderLayout.SOUTH,p3);
                p3.setLayout((g=new GridLayout(9,9)));
                f.pack();
                myButtons=new JButton[9][9];
                for(int j=0;j<9;j++)
                        for(int i=0;i<9;i++)
                        {
                                myButtons[j][i]=new JButton();
                                p3.add(myButtons[j][i]);
                        }
                f.setSize(370,460);
                f.setVisible(true);
        }
        public void read()//read from a file
        {
                Scanner s=null;
                try{
                        s=new Scanner (new File("minesweeper.txt"));
                        for(int i=0;i<9;i++)
                                for(int j=0;j<9;j++)
                                        code[i][j]=s.next();
                }
                catch(IOException e){}
        }
        public void controlButtonsEvent()
        {
                for(int i=0;i<9;i++)
                        for(int j=0;j<9;j++)
                                myButtons[i][j].addMouseListener(this);
        }
        public void mouseClicked(MouseEvent e) {
                // TODO Auto-generated method stub
                int i,j;
                for(i=0;i<9;i++)
                        for(j=0;j<9;j++)
                        {
                                if(e.getSource()==myButtons[i][j])
                                {
                                        if(SwingUtilities.isLeftMouseButton(e))
                                        {
                                                this.validate(i, j);
                                        }
                                }
                        }
        }
        public void mouseEntered(MouseEvent arg0) {}
        public void mouseExited(MouseEvent arg0) {}
        public void mousePressed(MouseEvent e) {}
        public void mouseReleased(MouseEvent arg0) {}
        public void validate(int x,int y)
        {
                System.out.println(code[x][y]);
                System.out.println(newGui.isNumber(code[x][y]));
                //set a new font with characteristics within the parameters
                myButtons[x][y].setFont(new Font("sansserif", Font.BOLD, 8));
                if(newGui.isNumber(code[x][y]))
                {
                        myButtons[x][y].setText(code[x][y]);
                        
                       
                        myButtons[x][y].setEnabled(false);
                }
                else
                {
                        //other Code
                }
        }
        public static boolean isNumber(String str)
        {
                boolean t=false;
                String numbers[]={"1","2","3","4","5","6","7","8","9"};
                for(int i=0;i<9;i++)
                {
                        if(str.equals(numbers[i]))
                                t=true;
                }
                return t;
        }
}


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1