⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 adventuregui.java

📁 The project Adventure is a simple text-based adventure game. The game, as given, involves the hero t
💻 JAVA
字号:
package ope.adventure.ui;

import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import ope.adventure.Adventure;
import ope.adventure.Player;
import ope.guikka.ApplicationFrame;

/**
 * This class contains a GUIfor the Adventure text game project. 
 * The GUI reads its input from a text field and displays
 * data about the game in uneditable text areas.
 * 
 * @see ope.adventure.ui.AdventureTextUI
 */
public class AdventureGUI extends ApplicationFrame {

  private Adventure adventure;				// fixed value: a session only has one adventure and ends when that one finishes
  private Player player;              // fixed value

  // Components:
  private JMenuItem quitItem;               
  private JTextField input;
  private JTextArea locationInfo; 
  private JTextArea turnOutput; 
  private JLabel turnCounter;
  
  
  /**
   * Creates a new adventure game frame and starts a new
   * adventure from the beginning.
   */
  public AdventureGUI() {
    this.setTitle("Forest Adventure");

    this.adventure = new Adventure();
    this.player = this.adventure.getPlayer();
    
    this.locationInfo = new JTextArea(7, 50);
    this.locationInfo.setEditable(false);
    this.place(new JLabel("Location:"), 0, 0);
    this.place(this.locationInfo, 0, 1);

    this.input = new JTextField(50);
    this.place(new JLabel("Command:"), 1, 0);
    this.place(this.input, 1, 1);

    this.turnOutput = new JTextArea(7, 50);
    this.turnOutput.setEditable(false);
    this.place(new JLabel("Events:"), 2, 0);
    this.place(this.turnOutput, 2, 1);

    this.turnCounter = new JLabel();
    this.place(this.turnCounter, 3, 0, 2, 1);

    this.quitItem = new JMenuItem("Quit");
    this.place(this.quitItem, "Program");
    
    this.updateInfo(this.adventure.getWelcome());
    
    this.pack();
    this.input.requestFocus();
  }

  
  /**
   * Event handler method: reacts (assuming the game is not over) to 
   * the user giving a new command in the command input field.
   * Clears the input field and proceeds to execute the given command.
   * 
   * @param field the field that that received the input
   */
  public void textEntered(JTextField field) {
    if (field == this.input && !this.adventure.isOver()) {
      String command = field.getText();
      if (command.length() > 0) {
      	field.setText("");
      	this.playTurn(command);
      }
    }
  }
  

  /**
   * Event handler method: reacts to the user selecting 
   * the quit menu item by exiting the application.
   * 
   * @param item the selected menu item
   */
  public void menuItemSelected(JMenuItem item) {
    if (item == this.quitItem) {
      this.exit();
    }
  }

  
  /**
   * Lets the player play a turn by executing the
   * given command. If the player wants to quit,
   * exits the application. Otherwise, updates
   * the GUI with the game's new status after the
   * player's turn.
   * 
   * @param command a command to execute, e.g. "go north", "quit"
   */
  private void playTurn(String command) {
    String turnReport = this.adventure.playTurn(command);
    if (this.player.hasQuit()) {
      this.exit();
    }
    this.updateInfo(turnReport);
  }

  
  /**
   * Updates the GUI with the current game status data.
   * 
   * @param turnReport a report of the player's latest turn
   */
  private void updateInfo(String turnReport) { 
    if (!this.adventure.isOver()) {
      this.turnOutput.setText(turnReport);
    } else {
      this.turnOutput.setText(turnReport + "\n\n" + this.adventure.getGoodbye());
    }
    this.locationInfo.setText(this.player.getLocation().getFullDescription()); 
    this.turnCounter.setText("Turns played: " + this.adventure.getTurnCount());
  }
  

  /**
   * Creates a user interface frame and displays it.
   */
  public static void main(String[] arguments) {
    AdventureGUI ui = new AdventureGUI();
    ui.setVisible(true);
  }
  
  
  
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -