I’m writing a game in Java, and I want the user to be able to provide input from both the command line and my GUI. Currently, I use this method to get input:
static String getInput(){ System.out.println('Your move:'); Scanner sc = new Scanner(System.in); return sc.nextLine(); }
I want to keep using this, but let a mousePressed event emulate the user actually typing in their input as well. It’s not that efficient of a solution, but it makes sense in my application. So the question is: how do I simulate a user typing to System.in from the code side?
This is possible – the easiest substitution for System.in would be a PipedInputStream. This must be hooked up to a PipedOutputStream that writes from another thread (in this case, the Swing thread).
However, it might be better to rethink your game logic to cut out the streams altogether in GUI mode.