Machine Problem Problems

help me. i have a few questions about the machine problem i'm doing as partial requirement for this semester:

1. how do you initialize JPanel with a layout? could someone provide me with a sample code?

2. my professor was vague in teaching how to use event handling. what exactly must i do to make a button "transform" into a label? could someone provide me with a sample code?

sample codes given will not be taken as my own. you will be duly acknowledged.

thanks in advance.

[507 byte] By [FolkFolka] at [2007-11-26 22:51:21]
# 1

/* ?GeorgeMC 2007 */

JPanel panel = new JPanel(new FlowLayout());

ask your professor for some clarity regarding point 2. doesn't make much sense really. transform buttons to labels? event handling? demand that he explain himself!

a handy link for the future http://java.sun.com/j2se/1.5.0/docs/api/

georgemca at 2007-7-10 12:13:16 > top of Java-index,Java Essentials,Java Programming...
# 2

@georgemc

thanks for the block of code. i really appreciated it.

now i have a new problem.

how do you create an two-dimensional array of classes?

import javax.swing.*;

import java.awt.*;

class MinesweeperMain{

JFrame frame;

JPanel mainPanel;

JPanel gridSize;

JPanel mineArea;

JPanel mineCounter;

JButton button5;

JButton button10;

JButton newGame;

Container contentPane;

//I don't know what to put here to create a new array of classes

void launch(){

frame = new JFrame("Minesweeper");

mainPanel = new JPanel(new BorderLayout());

gridSize = new JPanel(new BorderLayout());

mineArea = new JPanel(new GridLayout(5, 5));

mineCounter = new JPanel(new BorderLayout());

button5 = new JButton("5 x 5");

button10 = new JButton("10 x 10");

newGame = new JButton("Start a new game");

contentPane = frame.getContentPane();

gridSize.add(button5, BorderLayout.WEST);

gridSize.add(newGame, BorderLayout.CENTER);

gridSize.add(button10, BorderLayout.EAST);

for(int i = 0; i < 5; i++){

for(int j = 0; j < 5; j++){

mineArea.add(MB[i][j].getButton());

}

}

mainPanel.add(gridSize, BorderLayout.NORTH);

mainPanel.add(mineArea, BorderLayout.CENTER);

mainPanel.add(mineCounter, BorderLayout.SOUTH);

contentPane.add(mainPanel);

frame.pack();

frame.setVisible(true);

}

public static void main(String args[]){

MinesweeperMain MM = new MinesweeperMain();

MM.launch();

}

}

here's the class i need to have as an array:

import java.math.*;

import javax.swing.*;

public class MinesweeperButtons{

private int bomb = 0;

private String strBomb = "";

private JButton button;

public MinesweeperButtons(int rows, int columns){

MinesweeperButtons MB[][] = new MinesweeperButtons[rows][columns];

}

public void createButtons(int bomb, String strBomb, JButton button){

this.bomb = (int) ((Math.random()) * 10) % 3;

this.strBomb = Integer.toString(bomb);

this.button = new JButton(strBomb);

}

public JButton getButton(){

return button;

}

}

FolkFolka at 2007-7-10 12:13:17 > top of Java-index,Java Essentials,Java Programming...
# 3
Crossposted continuation at: http://forum.java.sun.com/thread.jspa?threadID=5153040
warnerjaa at 2007-7-10 12:13:17 > top of Java-index,Java Essentials,Java Programming...
# 4

MinesweeperButtons[][] msButtons;

msButtons = new MinesweeperButtons[rows][cols];

for (int i=0; i<rows; i++) {

for (int j=0; j><cols; j++) {

msButtons[i][i] = MinesweeperButtons(x, y);

}

}

>

hunter9000a at 2007-7-10 12:13:17 > top of Java-index,Java Essentials,Java Programming...
# 5
> for (int j=0; j><cols; j++) {To avoid this forum code-formatting bug, put spaces around ><...Doh!Message was edited by: DrLaszloJamf
DrLaszloJamfa at 2007-7-10 12:13:17 > top of Java-index,Java Essentials,Java Programming...
# 6

what did i do wrong?

when i tried to compile this code, it returned 3 errors:

"<identifier> expected" at line 15

"illegal start of type" at line 16

"<identifier> expected" at line 50

help me...

import javax.swing.*;

import java.awt.*;

class MinesweeperMain{

JFrame frame;

JPanel mainPanel;

JPanel gridSize;

JPanel mineArea;

JPanel mineCounter;

JButton button5;

JButton button10;

JButton newGame;

Container contentPane;

MinesweeperButtons[][] MB;

MB[][] = new MinesweeperButtons[5][5]; //line 15

for(int i = 0; i < 5; i++){ //line 16

for(int j = 0; j < 5; j++){

MB[i][j] = MinesweeperButtons(5, 5);

}

}

void launch(){

frame = new JFrame("Minesweeper");

mainPanel = new JPanel(new BorderLayout());

gridSize = new JPanel(new BorderLayout());

mineArea = new JPanel(new GridLayout(5, 5));

mineCounter = new JPanel(new BorderLayout());

button5 = new JButton("5 x 5");

button10 = new JButton("10 x 10");

newGame = new JButton("Start a new game");

contentPane = frame.getContentPane();

gridSize.add(button5, BorderLayout.WEST);

gridSize.add(newGame, BorderLayout.CENTER);

gridSize.add(button10, BorderLayout.EAST);

for(int i = 0; i < 5; i++){

for(int j = 0; j < 5; j++){

mineArea.add(MB[i][j].getButton());

}

}

mainPanel.add(gridSize, BorderLayout.NORTH);

mainPanel.add(mineArea, BorderLayout.CENTER);

mainPanel.add(mineCounter, BorderLayout.SOUTH);

contentPane.add(mainPanel);

frame.pack();

frame.setVisible(true);

}

public static void main(String args[]){

MinesweeperMain MM = new MinesweeperMain();

MM.launch();

}

} //line 50

EDIT: the MinesweeperButtons class is at the first post.

Message was edited by:

FolkFolk

FolkFolka at 2007-7-10 12:13:17 > top of Java-index,Java Essentials,Java Programming...
# 7

MinesweeperButtons[][] MB;

MB[][] = new MinesweeperButtons[5][5]; //line 15

for(int i = 0; i < 5; i++){ //line 16

for(int j = 0; j < 5; j++){

MB[i][j] = MinesweeperButtons(5, 5);

}

}

The first line declares an array called MB

The second line initializes that array. Don't put [][] after MB here, MB is already declared as an array.

The rest of this if a loop that initializes each position of the array with a new MinesweeperButton.

Only declarations can be outside of a method or constructor. Put the second and third parts in the constructor. If you want to initialize the array and declare it at the same time, do this:

MinesweeperButtons[][] MB = new MinesweeperButtons[5][5];

Read through this tutorial for more information:

http://java.sun.com/docs/books/tutorial/java/javaOO/classes.html

hunter9000a at 2007-7-10 12:13:17 > top of Java-index,Java Essentials,Java Programming...
# 8

did i put the for loops in the right place?

because when i compiled the corrected code (thanks hunter9000), the first "<identifier> expected" error in line 15 disappeared, but the "illegal start of type" still lingers in line 16, and so does the "<identifier> expected" error in line 50.

i'm starting to doubt whether i chose the right course...

FolkFolka at 2007-7-10 12:13:17 > top of Java-index,Java Essentials,Java Programming...
# 9

> did i put the for loops in the right place?

No, not in the last code you posted. Put this with the member declarations:

MinesweeperButtons[][] MB;

And put this in the constructor:

MB = new MinesweeperButtons[5][5]; // notice the removed [][] after MB

for(int i = 0; i < 5; i++){

for(int j = 0; j < 5; j++){

MB[i][j] = new MinesweeperButtons(5, 5);// notice the new keyword

}

}

hunter9000a at 2007-7-10 12:13:17 > top of Java-index,Java Essentials,Java Programming...
# 10

i'm going crazy here...

not only because it's already 2:00 am, but because i can't get this code right

now it returns a "cannot find symbol method MinesweeperButtons(int,int)" error at line 18

import javax.swing.*;

import java.awt.*;

public class MinesweeperMain{

JFrame frame;

JPanel mainPanel;

JPanel gridSize;

JPanel mineArea;

JPanel mineCounter;

JButton button5;

JButton button10;

JButton newGame;

Container contentPane;

MinesweeperButtons[][] MB;

void launch(){

MB = new MinesweeperButtons[5][5];

for(int i = 0; i < 5; i++){

for(int j = 0; j < 5; j++){

MB[i][j] = MinesweeperButtons(5,5); //line 18

}

}

frame = new JFrame("Minesweeper");

mainPanel = new JPanel(new BorderLayout());

gridSize = new JPanel(new BorderLayout());

mineArea = new JPanel(new GridLayout(5, 5));

mineCounter = new JPanel(new BorderLayout());

button5 = new JButton("5 x 5");

button10 = new JButton("10 x 10");

newGame = new JButton("Start a new game");

contentPane = frame.getContentPane();

gridSize.add(button5, BorderLayout.WEST);

gridSize.add(newGame, BorderLayout.CENTER);

gridSize.add(button10, BorderLayout.EAST);

for(int i = 0; i < 5; i++){

for(int j = 0; j < 5; j++){

mineArea.add(MB[i][j].getButton());

}

}

mainPanel.add(gridSize, BorderLayout.NORTH);

mainPanel.add(mineArea, BorderLayout.CENTER);

mainPanel.add(mineCounter, BorderLayout.SOUTH);

contentPane.add(mainPanel);

frame.pack();

frame.setVisible(true);

}

public static void main(String args[]){

MinesweeperMain MM = new MinesweeperMain();

MM.launch();

}

}

i hope someone could help me through my simple yet crippling problem...

Message was edited by:

FolkFolk

FolkFolka at 2007-7-10 12:13:17 > top of Java-index,Java Essentials,Java Programming...
# 11
That's because you didn't copy the code I gave you correctly. Reread reply 9 to see what you did wrong.
hunter9000a at 2007-7-10 12:13:17 > top of Java-index,Java Essentials,Java Programming...
# 12

ok, the succeeding code FINALLY compiled:

import javax.swing.*;

import java.awt.*;

public class MinesweeperMain{

JFrame frame;

JPanel mainPanel;

JPanel gridSize;

JPanel mineArea;

JPanel mineCounter;

JButton button5;

JButton button10;

JButton newGame;

Container contentPane;

MinesweeperButtons[][] MB;

void launch(){

MB = new MinesweeperButtons[5][5];

for(int i = 0; i < 5; i++){

for(int j = 0; j < 5; j++){

MB[i][j] = new MinesweeperButtons(5, 5);

}

}

frame = new JFrame("Minesweeper");

mainPanel = new JPanel(new BorderLayout());

gridSize = new JPanel(new BorderLayout());

mineArea = new JPanel(new GridLayout(5, 5));

mineCounter = new JPanel(new BorderLayout());

button5 = new JButton("5 x 5");

button10 = new JButton("10 x 10");

newGame = new JButton("Start a new game");

contentPane = frame.getContentPane();

gridSize.add(button5, BorderLayout.WEST);

gridSize.add(newGame, BorderLayout.CENTER);

gridSize.add(button10, BorderLayout.EAST);

for(int i = 0; i < 5; i++){

for(int j = 0; j < 5; j++){

mineArea.add(MB[i][j].getButton());

}

}

mainPanel.add(gridSize, BorderLayout.NORTH);

mainPanel.add(mineArea, BorderLayout.CENTER);

mainPanel.add(mineCounter, BorderLayout.SOUTH);

contentPane.add(mainPanel);

frame.pack();

frame.setVisible(true);

}

public static void main(String args[]){

MinesweeperMain MM = new MinesweeperMain();

MM.launch();

}

}

but now i receive this runtime error:

Exception in thread "main" java.lang.NullPointerException

at java.awt.Container.addImpl(Container.java:1019)

at java.awt.Container.add(Container.java:351)

at MinesweeperMain.launch(MinesweeperMain.java:35)

at MinesweeperMain.main(MinesweeperMain.java:47)

FolkFolka at 2007-7-10 12:13:17 > top of Java-index,Java Essentials,Java Programming...
# 13

In the MinesweeperButtons class, you never initialize the JButton. The MinesweeperButtons constructor creates an array of MinesweeperButtons, but never initializes them. You should not be creating an array of that class in the constructor for that class. If you were initializing them, it would cause an infinite loop until you ran out of memory. What are the width and height that you pass to the constructor used for? You have a method createButtons that initializes each of the members bomb, strBomb, and button, but you never call it, so button is always null, hence the null pointer exception when you call getButton().

import java.math.*;

import javax.swing.*;

public class MinesweeperButtons{

private int bomb = 0;

private String strBomb = "";

private JButton button;

public MinesweeperButtons(int rows, int columns){

//MinesweeperButtons MB[][] = new MinesweeperButtons[rows][columns];// don't do this, it's not needed for anything.

createButtons();

}

public void createButtons(/*int bomb, String strBomb, JButton button*/){

this.bomb = (int) ((Math.random()) * 10) % 3;

this.strBomb = Integer.toString(bomb);

this.button = new JButton(strBomb);

}

public JButton getButton(){

return button;

}

}

hunter9000a at 2007-7-10 12:13:17 > top of Java-index,Java Essentials,Java Programming...
# 14
OH MY GOD THANK YOU HUNTER9000you've been sooooo helpful
FolkFolka at 2007-7-10 12:13:17 > top of Java-index,Java Essentials,Java Programming...
# 15
> OH MY GOD > > THANK YOU HUNTER9000> > you've been sooooo helpfulYeah, I am pretty awesome aren't I?;-)You're very welcome.
hunter9000a at 2007-7-21 19:17:55 > top of Java-index,Java Essentials,Java Programming...
# 16
uhm...new problem...i'm using java.swing objects in my code, but the ways i know how to handle events require java.awt objects.how do i handle events with java.swing objects?one specific problem:how do I "transform" a button into a label?
FolkFolka at 2007-7-21 19:17:55 > top of Java-index,Java Essentials,Java Programming...
# 17
help me...please...I'm desperate...
FolkFolka at 2007-7-21 19:17:55 > top of Java-index,Java Essentials,Java Programming...
# 18
the event handling part is the only part of my machine problem missingif someone could help me how to handle events with javax.swing objects, i would really appreciate it.
FolkFolka at 2007-7-21 19:17:55 > top of Java-index,Java Essentials,Java Programming...
# 19
Everything You Ever Wanted To Know About Event Listeners, But Were Too Afraid To Ask: http://www.google.com/search?q=java+event+listener
hunter9000a at 2007-7-21 19:17:55 > top of Java-index,Java Essentials,Java Programming...
# 20
> > /* GeorgeMC 2007 */> JPanel panel = new JPanel(new FlowLayout());> This is why I think George is awesome, lol.
TuringPesta at 2007-7-21 19:17:55 > top of Java-index,Java Essentials,Java Programming...
# 21

ok i have a new problem (boy talk about machine problem PROBLEMS)

how can i make a certain class which requires event handling a subclass of another class?

for example, i have a class MinesweeperMain

i have another class MinesweeperButton5x5

which needs event handling, therefore having the declaration

public class MinesweeperButton5x5 extends JPanel implements ActionListener{

}

how do i make MinesweeperButton5x5

a subclass of MinesweeperMain

?

i hope i'm not being too much of a nuisance...but this is due in 48 hours...any help will be duly acknowledged.

FolkFolka at 2007-7-21 19:17:55 > top of Java-index,Java Essentials,Java Programming...
# 22

I dont understand your question?

Classes can only Extend from ONE other class but they

can implement as many things as they want.

Logically a button would not be a subclass of the Main Program.

I think you need to give some serious thought to your design.

(Or explain the problem just a bit better)

TuringPesta at 2007-7-21 19:17:55 > top of Java-index,Java Essentials,Java Programming...
# 23

help...

here's what i want to happen:

whenever I click on the button with 10x10, i want the grid size to change.

this is my code:

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class MinesweeperMain extends JPanel implements ActionListener{

protected int areaDimension = 5; //I think this is where the problem lingers

JFrame frame;

JPanel mainPanel;

JPanel gridSize;

JPanel mineArea;

JPanel mineCounter;

JButton newGame;

JLabel counter;

Container contentPane;

MinesweeperButton5x5 MB5x5;

MinesweeperButton10x10 MB10x10;

MinesweeperButtons[][] MB;

void launch(){

int mines = 0;

MB5x5 = new MinesweeperButton5x5();

MB10x10 = new MinesweeperButton10x10();

MB = new MinesweeperButtons[areaDimension][areaDimension];

for(int i = 0; i < areaDimension; i++){

for(int j = 0; j < areaDimension; j++){

MB[i][j] = new MinesweeperButtons();

}

}

for(int i = 0; i < areaDimension; i++){

for(int j = 0; j < areaDimension; j++){

if(MB[i][j].getBomb() == 0)

mines++;

}

}

frame = new JFrame("Minesweeper");

mainPanel = new JPanel(new BorderLayout());

gridSize = new JPanel(new BorderLayout());

mineArea = new JPanel(new GridLayout(areaDimension, areaDimension));

mineCounter = new JPanel(new BorderLayout());

counter = new JLabel("Mines in the area: "+mines+"");

newGame = new JButton("Start a new game");

contentPane = frame.getContentPane();

gridSize.add(MB5x5.getButton5x5(), BorderLayout.WEST);

gridSize.add(newGame, BorderLayout.CENTER);

gridSize.add(MB10x10.getButton10x10(), BorderLayout.EAST);

for(int i = 0; i < areaDimension; i++){

for(int j = 0; j < areaDimension; j++){

mineArea.add(MB[i][j].getButton());

}

}

mineCounter.add(counter, BorderLayout.EAST);

mainPanel.add(gridSize, BorderLayout.NORTH);

mainPanel.add(mineArea, BorderLayout.CENTER);

mainPanel.add(mineCounter, BorderLayout.SOUTH);

contentPane.add(mainPanel);

frame.pack();

frame.setVisible(true);

}

public void actionPerformed(ActionEvent e){ //This has no function. It's here just to override the ActionListener class.

}

public static void main(String args[]){

MinesweeperMain MM = new MinesweeperMain();

MM.launch();

}

}

here's the MinesweeperButton10x10 class:

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class MinesweeperButton10x10 extends MinesweeperMain{

private JButton button10x10;

public MinesweeperButton10x10() {

button10x10 = new JButton("10x10");

}

public void actionPerformed(ActionEvent e){

super.areaDimension = 10;

}

public JButton getButton10x10(){

return button10x10;

}

}

what happens is that the code compiles, but the grid size doesn't change. what did i do wrong?

FolkFolka at 2007-7-21 19:17:55 > top of Java-index,Java Essentials,Java Programming...
# 24

JButtons have an addActionListener method that you use to "wire" your buttons to the code that runs when they're clicked. If you make MinesweeperButton5x5 an ActionListener (like it is in your code) then you'd use code like this to hook them together.

JButton button = new Jbutton();

MinesweeperButton5x5 msb = new MinesweeperButton5x5();

button.addActionListener(msb);

In the actionPerformed method of MinesweeperButton5x5, handle whatever happens when that button is clicked.

Why does MinesweeperButton5x5 have "button" in it's name if it's a type of panel?

hunter9000a at 2007-7-21 19:17:55 > top of Java-index,Java Essentials,Java Programming...
# 25

*deep breath*

almost done...

however, i have a problem.

i redid the design of Minesweeper so that the game would simply alter the difficulty of the game. however, I couldn't seem to change the number of bombs in the game whenever i click on the buttons.

here's the main class:

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class MinesweeperMain extends JButton implements ActionListener{

protected int areaDimension = 10;

protected int difficultyModifier = 10; //used in MinesweeperButtons

JFrame frame;

JPanel mainPanel;

JPanel panelNewGame;

JPanel setDifficulty;

JPanel mineArea;

JPanel mineCounter;

JButton newGame;

JLabel counter;

Container contentPane;

SetEasy easy;

SetNormal normal;

SetHard hard;

MinesweeperButtons[][] MB;

protected void launch(){

int mines = 0;

easy = new SetEasy();

normal = new SetNormal();

hard = new SetHard();

MB = new MinesweeperButtons[areaDimension][areaDimension];

for(int i = 0; i < areaDimension; i++){

for(int j = 0; j < areaDimension; j++){

MB[i][j] = new MinesweeperButtons();

}

}

for(int i = 0; i < areaDimension; i++){

for(int j = 0; j < areaDimension; j++){

MB[i][j].createBomb(difficultyModifier);

if(MB[i][j].getBomb() == 0)

mines++; //I can't seem to change the value of mines, the indication of how many mines there are in the game.

}

}

frame = new JFrame("Minesweeper");

mainPanel = new JPanel(new BorderLayout());

panelNewGame = new JPanel(new BorderLayout());

setDifficulty = new JPanel(new BorderLayout());

mineArea = new JPanel(new GridLayout(areaDimension, areaDimension));

mineCounter = new JPanel(new BorderLayout());

counter = new JLabel("Mines in the area: "+mines+"");

newGame = new JButton("Start a new game");

contentPane = frame.getContentPane();

panelNewGame.add(newGame, BorderLayout.CENTER);

setDifficulty.add(easy.getButtonEasy(), BorderLayout.WEST);

setDifficulty.add(normal.getButtonNormal(), BorderLayout.CENTER);

setDifficulty.add(hard.getButtonHard(), BorderLayout.EAST);

for(int i = 0; i < areaDimension; i++){

for(int j = 0; j < areaDimension; j++){

mineArea.add(MB[i][j].getButton());

}

}

mineCounter.add(counter, BorderLayout.EAST);

mainPanel.add(panelNewGame, BorderLayout.WEST);

mainPanel.add(setDifficulty, BorderLayout.NORTH);

mainPanel.add(mineArea, BorderLayout.CENTER);

mainPanel.add(mineCounter, BorderLayout.SOUTH);

contentPane.add(mainPanel);

frame.pack();

frame.setVisible(true);

}

public void actionPerformed(ActionEvent e){ //This has no function. It's here just to override the ActionListener class.

}

public static void main(String args[]){

MinesweeperMain MM = new MinesweeperMain();

MM.launch();

}

}

here's the SetEasy class:

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class SetEasy extends MinesweeperMain {

private JButton buttonEasy;

public SetEasy() {

buttonEasy = new JButton("Easy");

buttonEasy.addActionListener(easy);

}

public void actionPerformed(ActionEvent e){

super.difficultyModifier = 10;

super.launch();

}

public JButton getButtonEasy(){

return buttonEasy;

}

}

here's the SetNormal class

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class SetNormal extends MinesweeperMain {

private JButton buttonNormal;

public SetNormal() {

buttonNormal = new JButton("Normal");

buttonNormal.addActionListener(normal);

}

public void actionPerformed(ActionEvent e){

super.difficultyModifier = 5;

super.launch();

}

public JButton getButtonNormal(){

return buttonNormal;

}

}

here's the SetHard class:

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class SetHard extends MinesweeperMain {

private JButton buttonHard;

public SetHard() {

buttonHard = new JButton("Hard");

buttonHard.addActionListener(hard);

}

public void actionPerformed(ActionEvent e){

super.difficultyModifier = 2;

super.launch();

}

public JButton getButtonHard(){

return buttonHard;

}

}

and, finally, the MinesweeperButtons class. by the way, i don't know what to put in the parameters of addActionListener method.

import java.math.*;

import javax.swing.*;

import java.awt.*;

public class MinesweeperButtons extends MinesweeperMain{

private int bomb = 0;

private String strBomb = "";

private JButton button;

public MinesweeperButtons(){

createButtons(button);

}

public void createBomb(int difficultyModifier){

this.difficultyModifier = difficultyModifier; //higher value means less instances of having 0 as a value for bomb, therefore making the game easier

bomb = (int) (Math.random() * 10) % difficultyModifier;

}

public void createButtons(JButton button){

this.button = new JButton();

this.button.addActionListener(/*what do I put here?*/);

}

public JButton getButton(){

return button;

}

public int getBomb(){

return bomb;

}

}

FolkFolka at 2007-7-21 19:17:55 > top of Java-index,Java Essentials,Java Programming...