Java

Moderators: zibadian
Number of threads: 7836
Number of posts: 18235

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Can't access variable - Newbie Posted by alice35 on 22 Mar 2011 at 9:28 AM
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

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




Any help would be greatly appreciated!

Alice

Report
Re: Can't access variable - Newbie Posted by silveredge52 on 23 Mar 2011 at 9:33 AM
Hey,
Only partial code is here so definitive answer is questionable. However it appears that what is "missing" are MonsterBlocks class variables, ie. symbols, row and int.

Hope this helps,
se52



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.