PDA

View Full Version : Java button help


Blixinator
03-02-2009, 11:32 AM
Hey there, I just MADE minesweeper.
Well, the basics behind it. It generates the field.

But I've always sucked with Applets. We've done hardly anything with them in my Java class.

So what I want to do is have the program bring up the app and put down all the buttons, but I don't know where to start. The problem is, I'm in an online class so I don't have much teacher assistance and I teach myself most of the stuff.

Do the buttons each have the value assigned to it or are they all generic buttons which refer to the array I have of the values of the field?

Also, how do I generate the buttons without naming each one individually?

Here's the console code which outputs the mine field.
*s are mines and _s are blank spaces. Each number refers to the number of mines in the 8 squares surrounding it


import java.util.Random;
public class Minesweeper
{
public Minesweeper()
{}
public String difficulty = "INTERMEDIATE";
//-------~*~BEGINNER~*~-------
/*
public int width = 9;
public int height = 9;
public int nMines = 10;
*/
//-------~*~INTERMEDIATE~*~-------

public int width = 16;
public int height = 16;
public int nMines = 40;

//-------~*~EXPERT~*~-------
/*
public int width = 30;
public int height = 16;
public int nMines = 99;
*/

public boolean minePositions[][] = new boolean[height+1][width+1];
public void newline(int num)
{
for(int a=0; a<num; a++)
System.out.println();
}
public int getRand(int num)
{
Random r = new Random();
return Math.abs((r.nextInt() % num))+0;
}
public int getMines(int x, int y)
{
int n=0;
for(int a=-1; a<=1; a++)
{
for(int b=-1; b<=1; b++)
{
try{
if(minePositions[x+a][y+b]==true)
n++;
}
catch( ArrayIndexOutOfBoundsException e)
{}
}
}
return n;
}


public static void main(String args[])
{
Minesweeper ms = new Minesweeper();
for(int a=0; a<ms.height; a++)
{
for(int b=0; b<ms.width; b++)
{
ms.minePositions[a][b] = false;
}
}
int x=0;
int y=0;
int n=0;
while(n<ms.nMines)
{
x=ms.getRand(ms.height);
y=ms.getRand(ms.width);
if(ms.minePositions[x][y]==false)
{
ms.minePositions[x][y] = true;
n++;
}
}
char mineField2[][] = new char[ms.height+1][ms.width+1];
for(int a=0; a<ms.height; a++)
{
for(int b=0; b<ms.width; b++)
{
mineField2[a][b] = '_';
}
}
for(int a=0; a<ms.height; a++)
{
for(int b=0; b<ms.width; b++)
{
if(ms.minePositions[a][b])
mineField2[a][b] = '*';
else
{
mineField2[a][b] = Integer.valueOf(ms.getMines(a,b)).toString().charA t(0);
if(mineField2[a][b]=='0')
mineField2[a][b] = '_';
}
}
}
for(int a=0; a<ms.height; a++)
{
for(int b=0; b<ms.width; b++)
{
System.out.print(mineField2[a][b] + " ");
}
ms.newline(1);
}

}
}

Blixinator
05-01-2009, 09:18 AM
Epic bump for a good reason.
So yeah, I got my code to make an application with a 9x9 grid of buttons using the following code:

getContentPane().add(p,BorderLayout.CENTER);
p.setLayout(new GridLayout(width,height));

for(int a=0; a<width*height; a++)
{
p.add(new Button(Integer.toString(a+1))); //Creates a new button with a number printed on it
}


This causes the following to pop up:
http://truimagz.com/hosting/Public/minesweeper.bmp

Now, my question is:
How do I differentiate between all these buttons? How would I go about determining which button in the grid was pressed? Forget that this is for minesweeper and assume all I want to do is print the number on the button that I click.

If I can figure that out, I should be able to move forward on my own.

eedok
05-01-2009, 09:36 AM
http://java.sun.com/docs/books/tutorial/uiswing/components/button.html

Blixinator
05-01-2009, 09:37 AM
Yes, I have that open already. But what am I looking for?

eedok
05-01-2009, 09:38 AM
maybe the .setActionCommand part

alternatively you can get the source on the ActionEvent

Blixinator
05-01-2009, 09:53 AM
I'm so confused.
What's the difference between an action and an actioncommand?
I understand that I set the action to be take when I'm making the button, but I don't understand how to do that.

An example would be nice.

eedok
05-01-2009, 09:55 AM
an actionCommand is part of the action, the command part to be specific

for(int a=0; a<width*height; a++)
{
Button b = new Button(Integer.toString(a+1));
b.setActionCommand("mine "+ b.getText());
p.add(b);
b.addActionListener(this);
}

Blixinator
05-01-2009, 10:04 AM
an actionCommand is part of the action, the command part to be specific
So what does the string (In this case "mine ") mean?



U:\Java\Minesweeper.java:72: cannot find symbol
symbol : method getText()
location: class java.awt.Button
b.setActionCommand("mine "+ b.getText());
Points to b.getText()

So I removed that part and made it

b.setActionCommand("mine ");


That then calls

public void actionPerformed(ActionEvent ae)
{
System.out.println("test");
}


And they all do make it print "test" to the command prompt

So now they're all listened too, but how do I make them print the numbers on top of them?

eedok
05-01-2009, 10:12 AM
try doing this:

b.setActionCommand("mine "+(a + 1));

and change your listener to this

public void actionPerformed(ActionEvent ae)
{
ae.getActionCommand()
}

Blixinator
05-01-2009, 10:23 AM
Sweet! It works!

EDIT:

Got pretty far with this. Stored the buttons in an array so I could refer back to them and make changes to them in actionPerformed()

I've got this so far:

import java.util.Random;

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

public class Minesweeper extends JFrame implements ActionListener
{

public JPanel p = new JPanel();

public String test = "Test";
public Button buttons[] = new Button[width*height];

public JButton button = new JButton("Button");
public Minesweeper()
{
GridLayout glo = new GridLayout(width,height);
getContentPane().add(p,BorderLayout.CENTER);
getContentPane().add(button,BorderLayout.SOUTH);
p.setLayout(glo);
glo.setHgap(3);
glo.setVgap(3);



for(int a=0; a<width*height; a++)
{
buttons[a] = new Button();
buttons[a].setActionCommand(Integer.toString(a+1));
p.add(buttons[a]);
buttons[a].addActionListener(this);
}

}

public static void update()
{
}

public static String difficulty = "INTERMEDIATE";
//-------~*~BEGINNER~*~-------

public static int width = 9;
public static int height = 9;
public static int nMines = 10;

//-------~*~INTERMEDIATE~*~-------

//public static int width = 16;
//public static int height = 16;
//public static int nMines = 40;

//-------~*~EXPERT~*~-------
/*
public static int width = 30;
public static int height = 16;
public static int nMines = 99;
*/

public static boolean minePositions[][] = new boolean[height+1][width+1];
public static void newline(int num)
{
for(int a=0; a<num; a++)
System.out.println();
}
public static int getRand(int num)
{
Random r = new Random();
return Math.abs((r.nextInt() % num))+0;
}
public static int getMines(int x, int y)
{
int n=0;
for(int a=-1; a<=1; a++)
{
for(int b=-1; b<=1; b++)
{
try{
if(minePositions[x+a][y+b]==true)
n++;
}
catch( ArrayIndexOutOfBoundsException e)
{}
}
}
return n;
}

public static char mineField2[][] = new char[height+1][width+1];


public static void main(String args[])
{


Minesweeper ms = new Minesweeper();


ms.setLocation(100,100);
ms.setTitle("Application1");
ms.setSize(500,500);
ms.show();
for(int a=0; a<height; a++)
{
for(int b=0; b<width; b++)
{
minePositions[a][b] = false;
}
}
int x=0;
int y=0;
int n=0;
while(n<nMines)
{
x=getRand(height);
y=getRand(width);
if(minePositions[x][y]==false)
{
minePositions[x][y] = true;
n++;
}
}

for(int a=0; a<height; a++)
{
for(int b=0; b<width; b++)
{
mineField2[a][b] = '_';
}
}
for(int a=0; a<height; a++)
{
for(int b=0; b<width; b++)
{
if(minePositions[a][b])
mineField2[a][b] = '*';
else
{
mineField2[a][b] = Integer.valueOf(getMines(a,b)).toString().charAt(0 );
if(mineField2[a][b]=='0')
mineField2[a][b] = '_';
}
}
}

for(int a=0; a<ms.height; a++)
{
for(int b=0; b<ms.width; b++)
{
System.out.print(mineField2[a][b] + " ");
}
ms.newline(1);
}

}

public void actionPerformed(ActionEvent ae)
{
int n = Integer.parseInt(ae.getActionCommand())-1;
buttons[n].setLabel(mineField2[n%width][(int)(n/width)]+"");
buttons[n].setEnabled(false);
}
}