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

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

  • silveredge52silveredge52 Posts: 198Member
    Hey,
    Only partial code is here so definitive answer is questionable. However it appears that what is "missing" are MonsterBlocks class variables, ie. symbols, [u]row[/u] and [u]int[/u].

    Hope this helps,
    se52
Sign In or Register to comment.