Q:

Index of geometric functions

I am making a sort of command based application to draw geometric figures. So if a user enters something like RECT 100, 50, 200, 120 I draw a rectangle at specified location on the drawing panel.
So for this i need to map RECT to g.drawRect(100, 50, 200, 120); and all such similar functions to draw geometric figures.
I will use a hash map for mapping, but i don't know how to build a array of functions in java. In C++ i have done this though.
The key can be 'RECT' and the value the offset of the index.
Please show me how can i index these functions. Or is there a still better way to address the primary concern?

A:

There are no function pointers in Java, you need to do it through inheritance and/or interfaces. Here is an example:
interface Shape {
    void draw(int[] data);
}

class Polygon implements Shape {
    public void draw(int[] data) {
        // Draw polygon using points data[i], data[i+1] for points
    }
}

class Circle implements Shape {
     public void draw(int[] data) {
         // Draw circle using data[0], data[1] for the center, and data[2] for radius
     }
}

In your main program's constructor or static initializer:
Map<String,Shape> shapes = new HashMap<String,Shape>();
shapes.put("POLY", new Polygon());
shapes.put("CIRC", new Circle());

In your drawing code:
shapes.get("CIRC").draw(new int[] {100, 100, 50});

A:

I have to admit, I didn't really got your design, I'm not sure why do you need an array of functions as you said, but this is how this thing can be done in java.
Since Function (method in java terms) is not a "first-class-sitizen" in Java, you can't build an array of methods (at least in java 7) . What you can do instead is to use a more object oriented approach - define an interface, each method will be an implementation of the interface ( a class) so that you'll be able to store an array of interface implementation. 
I would use a design pattern Command instead of array of methods or something... Hope this helps

