Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Calling a method of an object inside an array

$
0
0
OK, so basically I have a GHexagon (custom using the acm library, in case that is necessary). I have an ArrayList of GHexagons for each GHexagon that has its neighbors listed in order. One of the subclasses, LampHex, needs to read this list and light up all of it's neighbors when clicked. This is achieved through the following method:
public void toggleLight(Color c)
    {
        this.setFillColor(c);
    }


So that method is called when a LampHex is clicked and it applies for all the neighbors. My issue is that I need to figure out how to tell the method to run for each individual GHexagon surround the LampHex from the array. Here is the MouseListener I am using that's supposed to do that:[code]
@Override
public void mouseClicked(MouseEvent e)
{
if(lit)
{
setFillColor(Color.white);//For this LampHex to turn yellow
//This is supposed to go through the entirety of the ArrayList called
//'neighbors' and call the toggleLight() method, which is in every GHexagon
for(int i = 0; i < neighbors.size(); i++)
{
neighbors.get(i).toggleLight(Color.white);
}

lit = false;
}
else
{
setFillColor(Color.yellow);
for(int i = 0; i < neighbors.size(); i++)
{
neighbors.get(i).toggleLight(Color.yellow);
System.out.println(neighbors.get(i));
}
lit = true;//lit is an boolean which determines whether
//the lamp is being turned on or off.
}
}

If any more info is needed I will gladly provide.

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>