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).
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).
Public class GameInput { private Scanner scanner; /**CLI constructor*/ public GameInput() { scanner = new Scanner(System. In); } /**GUI constructor*/ public GameInput(PipedOutputStream out) throws IOException { InputStream in = new PipedInputStream(out); scanner = new Scanner(in); } public String getInput() { return scanner.nextLine(); } public static void main(String args) throws IOException { GameInput gameInput; PipedOutputStream output = new PipedOutputStream(); final PrintWriter writer = new PrintWriter(output); gameInput = new GameInput(output); final JTextField textField = new JTextField(30); final JButton button = new JButton("OK"); button. AddActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String data = textField.getText(); writer.
Println(data); writer.flush(); } }); JFrame frame = new JFrame(); frame. SetDefaultCloseOperation(JFrame. EXIT_ON_CLOSE); frame.getContentPane().
SetLayout(new FlowLayout()); frame.getContentPane(). Add(textField); frame.getContentPane(). Add(button); frame.pack(); frame.
SetVisible(true); String data = gameInput.getInput(); System.out. Println("Input=" + data); System. Exit(0); } } However, it might be better to rethink your game logic to cut out the streams altogether in GUI mode.
Thanks! This is exactly what I was looking for. You're right that there should be a better way to handle user input, but I was trying get something that has little room for bugs implemented quickly.
Took more time/effort than I expected... ah, well. Lesson learned. – perimosocordiae Oct 5 '08 at 9:04.
To be honest, after re-reading your question I'm not exactly sure what you want. Anyway, perhaps you need to check out the method java.lang.System. SetIn(InputStream in).
This will allow you to change what reader is used to read input from the terminal (i.e. Changing it from the actual terminal to what ever you like).
Assuming you have many operations like the given example, you might consider the interface approach described by Richie_W but make one routine per operation rather than generic "in/out" methods. For example: public interface IGameInteraction { public String askForMove( String prompt ); public boolean askAreYouSure( String prompt ); } Your command-line implementation is clear; now your GUI implementation could use an appropriate dialog for each logical operation rather than just be a text area that's really just the command-line version. Furthermore this is easier to write unit tests against because in your tests you can stub out these routines in any manner.
I made an application once that could run via the command line or using a GUI. The way I did this was to define an Interface (named IODevice) which defined the following methods: public String getInput(); public void showOutput(String output); I then had two classes which implemented this interface - One used the host computer's terminal (as you are doing now) and one used a JTextArea (output) / JOptionPane (input). Perhaps you could do something similar - To change the input device used, simply change the instance of the IODevice.
Hope this is of some use.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.