You can't write your own Agent (as a plain Java class) which includes elements in the AnyLogic model-building UI, if that's what you mean (plus, if you write your own Java class which extends Agent, you have to know the correct signatures to use for the two required constructors, which you can see from looking at the code generated for other Agents).
But, in AnyLogic 7, you can design an Agent visually and then use a limited form of inheritance to create subclasses of this Agent. (See the AnyLogic help on Agent Inheritance under Agent Based Modeling.) The inheritance is limited in that:
- you can override/overload methods (functions)---i.e., define one with the same name in a subclass---but you can't do the same for fields (variables/parameters), even if they are private in the superclass (so even for cases when it wouldn't be hiding/shadowing the field in a Java sense);
- you can't use such a subclass Agent as the top-level Agent in your model.
[Thanks to nikolaj for pointing out that function overrides are possible. I could have sworn I tried that in the initial AnyLogic 7 release and it had the same restriction as for parameters/variables; maybe it got changed in one of the 7.0.x point releases?]
Both of these can be pretty restrictive dependent on the circumstances but, in your example, this would be fine: have an Agent which has/'is' a button (with AnyLogic parameters for color, name, etc.).
Note that your onPress pseudo-code is problematic for a reusable Agent unless you do something like having a List<? extends Person> as a parameter for your Agent (so that, when created, it has a generic reference to something that allows it to loop through Agents with a tryToEscapeBuilding function).