2
statsButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e){

              //Return the string "stats" to gameLoop() as cmd
            }
        }); 

public void gameLoop(){
        Scanner lineScanner = new Scanner(System.in);
        cmd = "";

        System.out.print(getBoard().printBoard(false));

        while (!cmd.equals("quit")) {
            System.out.print(">");
            Scanner wordScanner = new Scanner(lineScanner.nextLine());

        if (wordScanner.hasNext()) {
            cmd = wordScanner.next();
            if (cmd.equals("board")) {
                System.out.print(getBoard().printBoard(false));
                } else if (cmd.equals("ships")) {
            System.out.print(getBoard().printBoard(true));
                } else if (cmd.equals("help")) {
                    printHelp();    
                } else if (cmd.equals("stats")) {
                    printStats();
                } else if (cmd.equals("fire")) {
            if(fire(wordScanner)) { 
            printStats();
            cmd = "quit";
                    }
                } else if (cmd.equals("quit")) {    
                } else if (!cmd.equals("")) {
                    System.out.println(ILLEGAL_COMMAND);
                }
            }

        }
    }

What I'm trying to do is that when the user clicks the statsButton, the String cmd in the gameLoop would be changed to "stats". The statsButton and the gameLoop() are located in two different classes. Anyone can give me an idea how to do it? (I've attempted pipedreader/pipedwriter) and I just can't seem to get it right.

*I'm basically trying to make my console application into a GUI application without changing the original console application.

Edit: What I've tried

Class textBased 
PipedInputStream in = new PipedInputStream()

public void gameLoop(){
    try{
        in.connect(GUIclass.out);

    Scanner lineScanner = new Scanner(in);`

Class GUIclass  
    PipedOutputStream out = new PipedOutputStream();
    PrintWriter writer;

  public GUIclass(){
    final PrintWriter writer = new PrintWriter(out);
    statsButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e){

              writer.println("stats");
             }
          }); 

that's what I tried writing but it doesn't seem to work.

1

4 Answers 4

4

Regarding

I'm basically trying to make my console application into a GUI application without changing the original console application..."

My advice is simple: "don't do it".

The two applications have completely different structure, one being linear, the other being event-driven, and are not directly translatable to each other. Better to make a new GUI program from the ground up. Now if your non-GUI application contains some well-structured and behaved object-oriented classes, then by all means use those classes in your GUI's "model" or logic section, but don't try to directly translate the program flow of one type of application to the other.

Edit
Based on your posted requirements:

"You should be able to play your Battleship game through your GUI interface. In addition, the text-based front-end you wrote for project 1 should still "work" and be playable."

Correct me if I'm wrong, but I'm betting that you have several classes involved here, and only one of them is the "text-based front-end". If so, then use the non front-end classes as the model of your GUI as I suggested above, but do not use the text-based front-end for anything GUI related, and do not try to emulate it in your GUI.

6
  • I understand about not doing it, I'm also more used to not doing it. However in this case, it is stated in the specification that I must do so.
    – rlhh
    Commented Nov 12, 2011 at 22:11
  • "You should be able to play your Battleship game through your GUI interface. In addition, the text-based front-end you wrote for project 1 should still "work" and be playable."
    – rlhh
    Commented Nov 12, 2011 at 22:20
  • @user1043625: please see update to my answer above. Also, consider editing your profile and giving yourself a less generic forum name, hopefully one better than mine. :) Commented Nov 12, 2011 at 22:22
  • I do have several classes and all the output are handled by one single class. However, we're told to pass the input from the GUI to the console instead of creating anything separate.
    – rlhh
    Commented Nov 12, 2011 at 22:26
  • @user1043625: no you weren't told to pass input from the GUI to the console, if those are the instructions you've received. All it states is to play the game through the GUI, meaning use the classes that you have available, and the console game should still be playable. Meaning you shouldn't alter the non-console classes in a way that would render the console interface unplayable. That's a big difference. I still stand by what I state in my answer. Commented Nov 12, 2011 at 23:03
3

Have the console application instantiate the button ActionListener and pass it to the UI. When the action event is fired, the listener will tell the console app that it happened. The method in the ActionListener will tell it what to do.

4
  • I'm not really getting it, can you explain with some simple examples? Also, I think that would be modifying the original class which I'm not suppose to do.
    – rlhh
    Commented Nov 12, 2011 at 22:11
  • "Not supposed to do" - why not?
    – duffymo
    Commented Nov 12, 2011 at 22:18
  • "You should be able to play your Battleship game through your GUI interface. In addition, the text-based front-end you wrote for project 1 should still "work" and be playable."
    – rlhh
    Commented Nov 12, 2011 at 22:27
  • Your text-based and GUI UIs need to be separate. I assume that you should be able to choose between the two. That will mean a modification to the original. What your professor is trying to do is get you to completely separate the game logic from the presentation. Your game logic should be passed what it needs. It shouldn't have to know whether it's the text or GUI UI that's getting it.
    – duffymo
    Commented Nov 12, 2011 at 22:50
3

I agree with Hovercrafts comment (changed to a reply).

But in general for problems like this I would change the method signature of your gameLoop(). I would use:

public void gameLoop(Reader reader)

Then you can pass different types of readers to the loop depending on the requirement.

For a console you might do something like:

gameloop( new InputStreamReader( System.in ) );

For a GUI you could do something like:

gameLoop ( new StringReader("some text") );

Edit:

Without changing the method signature you can redirect System.in to come from the String retrieved by the ActionListener:

public class Test
{
    public static void main(String args[]) throws Exception
    {
        String text = "some text";
        System.setIn( new ByteArrayInputStream( text.getBytes() ) );
        // gameloop(); 
        Scanner lineScanner = new Scanner(System.in);
        System.out.println( lineScanner.nextLine() );
    }
}
2
  • I've added something above to show you what I've tried, doesn't seem to work for me. Not sure what I'm doing wrong.
    – rlhh
    Commented Nov 12, 2011 at 22:47
  • @user1043625, I don't understand your commment. Why don't you try what I suggested since its working code. I did not suggest using a PipedStream, so I don't know whats wrong with your code.
    – camickr
    Commented Nov 13, 2011 at 3:50
1

If you have something like this :

class B {
 public void gameLoop(){
  ..
 }
}

and

class A{

  statsButton.addActionListener(new ActionListener() {
            ...
        }); 
}

You can declare reference to B in A with final . In that case it's will be visible in inner class ActionListener.

class A{
  final B b = ...; //final variable is visible in inner class
  statsButton.addActionListener(new ActionListener() {
            b.gameLoop();
             ...
        }); 
}
1
  • I do have something similar to what you've written. But the problem is how do I change the String cmd when I click the button? b.gameLoop().cmd wouldn't work and I don't think I should be doing that anyway.
    – rlhh
    Commented Nov 12, 2011 at 22:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.