Reputation: 9
I hope this makes sense, and that there is a tidier way of programming this.
I have an ArrayList of Buttons and a Collection of Territories, I am trying to figure out how to iterate through the ArrayList and set each label on the buttons to an int value that each Territory contains, then change the colour of the buttons background to correspond to its owner.
The long way is set the label for each button, and then use an if-else to check the owner and set the correct background colour, however, this would cause hundreds of lines of repeated code.
btnEgy.setLabel(Territory.EGYPT.units());
if(Territory.EGYPT.getOwner().toString().equals("Player 1"))
{
btnEgy.setBackground(Color.BLUE);
}
else if(Territory.EGYPT.getOwner().toString().equals("Player 2"))
{
btnEgy.setBackground(Color.RED);
}
else if (Territory.EGYPT.getOwner().toString().equals("Player 3"))
{
btnEgy.setBackground(Color.GREEN);
}
else if (Territory.EGYPT.getOwner().toString().equals("Player 4"))
{
btnEgy.setBackground(Color.YELLOW);
}
btnEus.setLabel(Territory.E_UNITEDSTATES.units());
if(Territory.E_UNITEDSTATES.getOwner().toString().equals("Player 1"))
{
btnEus.setBackground(Color.BLUE);
}
else if(Territory.E_UNITEDSTATES.getOwner().toString().equals("Player 2"))
{
btnEus.setBackground(Color.RED);
}
else if (Territory.E_UNITEDSTATES.getOwner().toString().equals("Player 3"))
{
btnEus.setBackground(Color.GREEN);
}
else if (Territory.E_UNITEDSTATES.getOwner().toString().equals("Player 4"))
{
btnEus.setBackground(Color.YELLOW);
}
Upvotes: 0
Views: 129
Reputation: 10772
If you have a List of Buttons and a List of Territories of equal length where buttons[0] is for territories[0] and so on...
final Iterator<Button> buttonI = buttons.iterator();
final Iterator<Territory> territoryI = territories.iterator();
while (territoryI.hasNext() && buttonI.hasNext()) {
final Button button = buttonI.next();
final Territory territory = territoryI.next();
button.setBackground(territory.getOwner().getColor());
button.setLabel(territory.units());
}
Here I have assumed you can add a getColor()
method to the class returned by territory.getOwner()
.
Upvotes: 0
Reputation: 5625
How about using function in java?
main code
setValues(btnEgy,Territory.EGYPT);
setValues(btnEus,Territory.E_UNITEDSTATES);
function code
public void setValues(Button btn,Territory t ){
btn.setLabel(t.units());
if(t.getOwner().toString().equals("Player 1"))
{
btn.setBackground(Color.BLUE);
}
else if(t.getOwner().toString().equals("Player 2"))
{
btn.setBackground(Color.RED);
}
else if (t.getOwner().toString().equals("Player 3"))
{
btn.setBackground(Color.GREEN);
}
else if (t.getOwner().toString().equals("Player 4"))
{
btn.setBackground(Color.YELLOW);
}
}
Upvotes: 0
Reputation: 4298
HashMap<String, Color> playerMap = new HashMap<String, Color>();
playerMap.add("Player 1", Color.BLUE);
playerMap.add("Player 2", Color.RED);
then
btnEgy.setBackground(playerMap.get(Territory.EGYPT.getOwner().toString()));
Upvotes: 3
Reputation: 18148
Assuming that you have the same number of buttons and territories,
Iterator<Button> itr1 = buttons.iterator();
Iterator<Territory> itr2 = territories.iterator();
while(itr1.hasNext() && itr2.hasNext()) {
Button button = itr1.next();
Territory territory = itr2.next();
// set button data to territory data
}
If the collection sizes don't match then you'll need to figure out if you want to terminate when you reach the end of the shorter collection, or if you want to keep looping through the shorter collection until you reach the end of the longer collection.
Upvotes: 0