I want to use an anonymous inner class to handle mouse actions for an object in my program. However, depending on a parameter, I want the inner class to act differently. For example:
Rectangle r1 = rectangleBuilder (Color.Red);
Rectangle r2 = rectangleBuilder (Color.Blue);
public Rectangle rectangleBuilder (final Color c) {
r = new Rectangle (100, 100, c);
r.dragListener = new DragListener () {
@Override
public void drag (double x, double y) {
if (c == Color.Red) {
r.setPosition (x, y);
}
if (c == Color.Blue) {
r.setPosition (2 *x, 2 *y);
}
}
}
return r;
}
The idea is that when creating the object, the behavior when it's dragged is decided by its color. This was working fine but now for some reason I'm getting an exception saying that c (Color) is null. I'm wondering if this sort of approach is possible in Java.