Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

Christian Lawrence
Christian Lawrence
3,941 Points

Java Game GUI pass a command

This was originally a text based game played in the terminal window. I'm trying to get the buttons to process directional commands, so when a users presses "East" the move into the east room. However I get an error when trying to pass an arg to the goRoom(); method.

(this was based on the world of zuul game)

private boolean processCommand(Command command) 
    {
        boolean wantToQuit = false;

        CommandWord commandWord = command.getCommandWord();

        if(commandWord == CommandWord.UNKNOWN) {
            System.out.println("I don't know what you mean...");
            return false;
        }

        if (commandWord == CommandWord.HELP) {
            printHelp();
        }
        else if (commandWord == CommandWord.GO) {
            goRoom(command);
        }
        else if (commandWord == CommandWord.MAP) {
            System.out.println("Your current location is at " + currentRoom.getCurrentRoomName()); 
           // The room you are in
        }
        else if (commandWord == CommandWord.CODE) {
            wantToQuit = code(command);
        }
        else if (commandWord == CommandWord.FAKE) {
            fake(command);
        }
        else if (commandWord == CommandWord.QUIT) {
            wantToQuit = quit(command);
        }
        // else command not recognised.
        return wantToQuit;
    }

 private void goRoom(Command command) 
    {
        if(!command.hasSecondWord()) {
            // if there is no second word, we don't know where to go...
            System.out.println("Go where?");
            return;
        }

        String direction = command.getSecondWord();

        // Try to leave current room.
        Room nextRoom = currentRoom.getExit(direction);

        if (nextRoom == null) {
            System.out.println("You can not go this way!");
        }
        else {
            currentRoom = nextRoom;
            System.out.println(currentRoom.getLongDescription());
        }
    }


    private void makeFrame()
    {
        frame = new JFrame("ImageViewer");
        JPanel toolbar = new JPanel();

        //makeMenuBar(frame);

        Container contentPane = frame.getContentPane();

        // Specify the layout manager with nice spacing
        contentPane.setLayout(new BorderLayout(6, 6));

        statusLabel = new JLabel(title, SwingConstants.CENTER);
        contentPane.add(statusLabel, BorderLayout.NORTH);

        // Bottom Nav Toolbar
        contentPane.add(toolbar, BorderLayout.SOUTH); 
        north = new JButton("North");
        north.addActionListener(new ActionListener() {
                               public void actionPerformed(ActionEvent e) { 
                                   System.out.println("go east");
                                }
                           });
        toolbar.add(north);

        east = new JButton("Help");
        east.addActionListener(new ActionListener() {

                               public void actionPerformed(ActionEvent e) { 


                                 goRoom("north"); 

                                }

                           });
        toolbar.add(east);

        south = new JButton("South");
        toolbar.add(south);

        west = new JButton("West");
        toolbar.add(west);

        //imagePanel = new ImagePanel();
        //contentPane.add(imagePanel, BorderLayout.CENTER);

        // building is done - arrange the components and show        
        frame.pack();
        frame.setVisible(true);
    } 

1 Answer