Ejemplo n.º 1
0
  private ChoiceButton createButton(Element xmlButton, Game window) {
    String label = xmlButton.getAttribute("label");
    String target = xmlButton.getAttribute("target");

    ChoiceButton button;
    if (xmlButton.hasAttribute("image")) {
      try {
        BufferedImage img = ImageIO.read(getClass().getResource(xmlButton.getAttribute("image")));
        ImageIcon image = new ImageIcon(img.getScaledInstance(150, 150, Image.SCALE_SMOOTH));

        button = new ChoiceButton(image, label, target, window);
      } catch (IOException ex) {
        button = new ChoiceButton(label, target, window);
        Logger.getLogger(StoryModel.class.getName()).log(Level.SEVERE, null, ex);
      }

    } else {
      button = new ChoiceButton(label, target, window);
    }

    NodeList actions = xmlButton.getElementsByTagName("action");
    for (int i = 0; i < actions.getLength(); i++) {
      Element xmlAction = (Element) actions.item(i);
      Action action = actionFactory.createAction(xmlAction.getAttribute("type"));
      action.createFromXML(xmlAction);

      button.addAction(action);
    }
    button.addActionListener(button);

    return button;
  }
  /**
   * Starts the program, creating the main objects.
   *
   * @return A reference to the application's MainWindow.
   */
  public MainWindow start() {
    ActionFactory actionFactory;

    // create the main objects and link them together
    process = new biogenesis.Process();
    currentWorld = new CurrentWorld(new World(new STOrganismFactory()));
    mainWindow = new MainWindow(currentWorld, process);
    visibleWorld = new VisibleWorld(mainWindow, currentWorld);
    mainWindow.setIconImage(imageIcon.getImage());
    currentWorld.getWorld().genesis();
    netServer = new NetServerThread(currentWorld);
    currentWorld.addListener(netServer);
    // sets net server
    if (Utils.ACCEPT_CONNECTIONS) {
      netServer.setAcceptConnections(true);
      netServer.startServer();
    }
    // sets listeners
    netServer.addStatusListener(mainWindow.getStatusBar());
    process.addTimeListener(mainWindow);
    process.addTimeListener(currentWorld);
    // starts timer
    process.startLifeProcess(Utils.DELAY);
    // initialize actions
    ActionFactory.init(mainWindow, process, currentWorld, visibleWorld, netServer);
    actionFactory = ActionFactory.getInstance();
    // set specific actions as listeners
    mainWindow.addWindowListener(actionFactory.getQuitAction());
    mainWindow.getOrganismTracker().addObserver(actionFactory.getAbortTrackingAction());
    process.addPauseListener(actionFactory.getStartStopAction());
    // create menu bars
    initToolBar();
    mainWindow.setJMenuBar(new MainMenu());

    return mainWindow;
  }
  /**
   * Initialize toolbars. Crates a tool bar to show when selecting an alive agent, another for dead
   * agents, and a third one when no agent is selected.
   *
   * <p>I'm not sure if this is the best way to create the tool bars.
   *
   * <p>To add a new toolbar for new types of agents, see MultipleToolBar and ActionFactory.
   */
  private void initToolBar() {
    MultipleToolBar toolBar = mainWindow.getToolBar();
    ActionFactory actionFactory = ActionFactory.getInstance();
    Action[] normalActions = {
      actionFactory.getNewGameAction(),
      actionFactory.getStartStopAction(),
      actionFactory.getSaveGameAction(),
      actionFactory.getIncreaseCO2Action(),
      actionFactory.getDecreaseCO2Action(),
      actionFactory.getManageConnectionsAction(),
      actionFactory.getAbortTrackingAction(),
      actionFactory.getZoomInAction(),
      actionFactory.getZoomOutAction(),
      actionFactory.getToggleEfficiencyModeAction()
    };
    Action[] aliveActions = {
      actionFactory.getFeedAction(),
      actionFactory.getWeakenAction(),
      actionFactory.getKillAction(),
      actionFactory.getCopyAction(),
      actionFactory.getSaveImageAction(),
      actionFactory.getTrackAction(),
      actionFactory.getAbortTrackingAction(),
      actionFactory.getZoomInAction(),
      actionFactory.getZoomOutAction()
    };
    Action[] deadActions = {actionFactory.getReviveAction(), actionFactory.getDisperseAction()};

    toolBar.addActionArray("normal", normalActions);
    toolBar.selectActionArray("normal");
    toolBar.addActionArray("alive", aliveActions);
    toolBar.addActionArray("dead", deadActions);
  }