Can't access variable - Newbie - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Can't access variable - Newbie

alice35alice35 Posts: 1Member
Hi everybody,
I'm quite the newbie to Java and this is my first post on this forum.
The question i have is actually for a homework project (a kind of PacMan game).
the code creates a button array of rows*columns buttons that should each have an action listener attached. by clicking a button, a block should be placed in a grid. My problem is that i can't access the value (row, column) of the button within putBlockListener.

This is my code, and i receive an error at line 58 where it says
TwoWindowsMarc.aGrid.block(MonsterBlocks.row, MonsterBlocks.col);
cannot find symbol

[code]
package FirstMonster;

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

public class MonsterBlocks extends JFrame {

private MonsterBlocks(int row, int col)
{
JFrame buttonFrame = new JFrame("build Buttons");
buttonFrame.setLayout(new GridLayout(row,col, 1, 1));
buttonFrame.setLocation(500,500);
buttonFrame.setSize(row*34,col*25);
buttonFrame.setVisible(true);

JButton[][] blockButton = new JButton[row][col];

int r = 0;
int c = 0;
while (r < row)
{
while (c < col)
{
if(TwoWindowsMarc.aGrid.rectangle[r][c] == TwoWindowsMarc.aGrid.blockChar)
{
blockButton[r][c]=new JButton("#");
blockButton[r][c].setBackground(Color.red);
}
else if(TwoWindowsMarc.aGrid.rectangle[r][c] == TwoWindowsMarc.aGrid.thingHereChar)
{
blockButton[r][c]=new JButton("O");
blockButton[r][c].setBackground(Color.ORANGE);
}
else
blockButton[r][c]=new JButton(".");

blockButton[r][c].setSize(10,10);
blockButton[r][c].setFont(new Font("Sans Serif", 1, 8));
blockButton[r][c].setMargin(new Insets(1, 1, 1, 1));
buttonFrame.add(blockButton[r][c]);

putBlockListener putBlocksListen= new putBlockListener( );
blockButton[r][c].addActionListener(putBlocksListen);
c++;
}
c = 0;
r++;
}
}

class putBlockListener implements ActionListener
{
// when the Build Grid button is clicked to this...
public void actionPerformed(ActionEvent e )
{
TwoWindowsMarc.aGrid.block(MonsterBlocks.row, MonsterBlocks.col); //this is were i get the error
TwoWindowsMarc.aGrid.updateState( );
}
}

} //end of gridListener


[/code]

Any help would be greatly appreciated!

Alice

Comments

Sign In or Register to comment.