There are three approaches you can use to solve the problem (Reflection is the last option I would recommend) :
Option 1 : Employ the Strategy pattern
If method1 and method2 are not objects of classes that are related. Employ the Strategy pattern as follows :
public interface Filler {
public void fill(Color color);
}
Create two classes that fill colors differently :
public class FloodFill implements Filler {
public void fill(Color color) {
//fill using flood fill algorithm
}
}
public class QuickFill implements Filler {
public void fill(Color color) {
//fill using quick fill algorithm
}
}
You can then use the Map approach explained below :
Option 2 : Use a HashMap
Assuming that method1 and method2 are related either through a common parent or through a parent child relationship, create a HashMap and prepopulate it with method1 and method2 objects :
Map<String,Filler> methods = new HashMap<>();
FloodFill method1 = new FloodFill();
QuickFill method2 = new QuickFill();
map.put("FloodFill",method1);
map.put("QuickFill",method2);
You can then change the methodCaller method to :
public void methodCaller(String method,Color color) {
methods.get(method).fill(color);
}
The client code can then look like this :
methodCaller("FloodFill",Color.RED);
methodCaller("QuickFill",Color.GREEN);
Using a HashMap instead of a basic array or a List allows you to associate meaningful names to your method calls. The HashMap is kind of a Factory for objects. The String constants used as the key for the Map can be defined as enum instead.
Option 3 : Use reflection :
This is not something I would recommend for this particular case but if you have to, take a look at this answer on how to achieve this trough reflection.