4 Replies - 617 Views - Last Post: 20 June 2010 - 11:08 AM Rate Topic: -----

#1 hamishehiran  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 73
  • Joined: 03-February 10

using outer classes variables in inner classes or finding indexes of a

Posted 20 June 2010 - 05:50 AM

I'm trying to write a program for minesweeper game and I face problems when I want to find the index of a component.

I've wrote the inner class in a loop.but of course i and j won't be the i and j above and if I use static variables I'll have NullPointerException and also I can't make objects.
and I don't know how to find the index of the component from the layout.(I've commented methods I tried,but that seems wrong).
generally I don't understand inner classes and how variables work in them.
it's just the begging of the program and I've just built the buttons
and I don't know even if my algorithm is correct because I've couldn't try it yet.
:
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.GridLayout;


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;



import javax.swing.JFrame;
import javax.swing.JPanel;


public class MineSweeper {
static int [][]nearbyBombs;
static boolean [][] inBomb=new boolean[9][9];
static Button [][] cells=new Button[9][9];
static int x,y;
	public static void main(String[] args) {

		JFrame myFrame=new JFrame("MineSweeper");
		GridLayout myGrid=new GridLayout(9,9);
		myGrid.setHgap(-5);
		myGrid.setVgap(-5);
		JPanel upperPanel=new JPanel();
		
		myFrame.add(upperPanel,BorderLayout.NORTH);
		
		JPanel myPanel=new JPanel(myGrid);
		
		myFrame.add(myPanel);
		
		for (int i=0;i<10;){
			int x=(int)(Math.random()*9);
			int y=(int)(Math.random()*9);
			if(!inBomb[x][y]){
		          inBomb[x][y]=true;
		          i++;
			}
			}
		
		
		
		for (int i=0;i<9;i++){
			for(int j=0;j<9;j++){
				cells[i][j]=new Button();
				
				myPanel.add(cells[i][j]);
				x=i;
				y=j;
		cells[i][j].addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				/* MineSweeper.x=((Button)e.getSource()).getWidth();
				 MineSweeper.y=((Button)e.getSource()).getHeight();*/
				for(int i=(x-1);(i<(x+1))&&(0<i && i<9);i++)
					for(int j=(y-1);(j<(y+1))&&(0<j && j<9);j++)
						nearbyBombs[x][y]++;
				((Button)e.getSource()).setLabel(""+nearbyBombs[x][y]);

			}
		}
		);
		}
		}
		myFrame.setVisible(true);
	
		
		

	}

}



Is This A Good Question/Topic? 0
  • +

Replies To: using outer classes variables in inner classes or finding indexes of a

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

  • Winning
  • member icon


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

Re: using outer classes variables in inner classes or finding indexes of a

Posted 20 June 2010 - 06:46 AM

Your main method should be as small as possible, you only need to instantiate the MineSweeper object in the main() as you should do everything within the constructor which you should create. To find the index of a button that was clicked, all you would have to do is loop through every button and check which one was clicked.

Something like this..

	@Override
	public void actionPerformed(ActionEvent e) {
		for(int i=0;i<9;i++){
			for(int j=0;j<9;j++){
				//see which button was clicked
				if(e.getSource()==cells[i][j]){
					//set color and disable the button so it cannot be clicked.
					cells[i][j].setBackground(Color.BLACK);
					cells[i][j].setEnabled(false);
	     }
	 }
   }
}


Was This Post Helpful? 1
  • +
  • -

#3 hamishehiran  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 73
  • Joined: 03-February 10

Re: using outer classes variables in inner classes or finding indexes of a

Posted 20 June 2010 - 07:22 AM

View Postm-e-g-a-z, on 20 June 2010 - 05:46 AM, said:

Your main method should be as small as possible, you only need to instantiate the MineSweeper object in the main() as you should do everything within the constructor which you should create. To find the index of a button that was clicked, all you would have to do is loop through every button and check which one was clicked.

Something like this..

	@Override
	public void actionPerformed(ActionEvent e) {
		for(int i=0;i<9;i++){
			for(int j=0;j<9;j++){
				//see which button was clicked
				if(e.getSource()==cells[i][j]){
					//set color and disable the button so it cannot be clicked.
					cells[i][j].setBackground(Color.BLACK);
					cells[i][j].setEnabled(false);
	     }
	 }
   }
}


thank you very much....
it seems like your telling me how to omit some cells at the beginning.actually I haven't wrote code for that yet.but the code you wrote would probably work.but it will be checking 81 cells for 81 buttons.wouldn't that be a bad performance?
and about the main thing.you mean my style is bad?I'm not used to write a lot of things in a constructor yet.then that wouldn't be a problem for the inner class?

This post has been edited by hamishehiran: 20 June 2010 - 07:23 AM

Was This Post Helpful? 0
  • +
  • -

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

  • Winning
  • member icon


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

Re: using outer classes variables in inner classes or finding indexes of a

Posted 20 June 2010 - 08:13 AM

What I meant is that you main method should only have this in it.
new MineSweeper(); and then build your gui in a constructor. As for the checking, how would you know which button was clicked? This is why you have to loop through each button to see which one was clicked. One you know the button that was clicked, you can get the variables and then check whether their is a bomb etc..

Your design should look something like this.

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


public class Minesweeper extends JFrame implements ActionListener{


	public Minesweeper(){

		//This is for the gui

	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		/*for checking which button 
		was clicked and triggering an event*/

	}

	public static void main (String [] args){		
		new Minesweeper();
	}

}


Was This Post Helpful? 0
  • +
  • -

#5 hamishehiran  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 73
  • Joined: 03-February 10

Re: using outer classes variables in inner classes or finding indexes of a

Posted 20 June 2010 - 11:08 AM

ok thanks...
I understand:)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1