A general approach. Create your console class that implements KeyListener. It should be responsible for rendering the visual representation of your console as well as echoing user input and command processor output to your game’s display. Next assign a key to toggling your console’s visibility. When the console is turned on, you need to redirect all keyboard input through it, accumulate the keys typed into a StringBuffer as well as render their textural representations to the console, then send the contents of the buffer to the command processor when you detect the appropriate key event (such as the enter key being pressed). You should also listen for a “close console” key combination and react accordingly. Once control has returned from the command processor, grab the results (if any) and render them to the console.
My experience with using a command processor comes from integrating Mozilla Rhino into a non game application, but the principles are pretty much the same. JSR-223 (http://docs.oracle.com/javase/6/docs/technotes/guides/scripting/) should get you started with the basics of scripting a Java application in general.
Off the top of my head, I seem to remember the steps being something like:
- create an instance of the scripting languages command processor
- create a context to run your script in – essentially the same as a namespace or package name
- tell the command processor what internal class instances that you would like to expose to the scripts and assign them an identifier for the scripts to refer to them by
- Execute the scripts – If successful, the internal state of your application will be affected by the contents of your script, if not, the command processor usually throws an exception containing useful information about scripts failure conditions
- Rinse and repeat.
Be warned though that scripting needs to be thought about carefully. Get familiar with the concepts of Shutters which limit what scripts have access to. Don’t directly expose game objects to the command processor; use proxy classes that only have the functionality you want exposed as opposed to exposing an entire “core” class. It may not be a huge deal in single player type games, but it’s ripe for exploitation in multi-player ones. Finally don’t directly expose the Java language to the scripts unless you’re sure you absolutely need to. While not as big of a deal for an internal game console where you control the input, if you’re loading up an external script file for something like modding, failure to lock down the language means a malicious scripter can easily bypass your games sandbox, and wreak havoc on a users system.
I will say that I have seen other methods for providing scriptable capabilities to Java applications, but none of them seem as robust or as well tested as the “official way”. Scripting is an awesome tool when it makes sense for a project. Just make sure that it adds some value, that you think about the implications and implementations thoroughly, and finally make sure you’ve implemented whatever security is appropriate for your sandboxed environment.