コード例 #1
0
  void loadGame() {
    JFileChooser fc = new JFileChooser("savegames");
    fc.setLocation(frame.getLocation().x + 150, frame.getLocation().y + 100);
    fc.setDialogTitle(Messages.getString("MegaMek.SaveGameDialog.title"));
    fc.setFileFilter(
        new FileFilter() {
          @Override
          public boolean accept(File dir) {
            return ((dir.getName() != null)
                && (dir.getName().endsWith(".sav")
                    || dir.getName().endsWith(".sav.gz")
                    || dir.isDirectory())); // $NON-NLS-1$
          }

          @Override
          public String getDescription() {
            return "Savegames";
          }
        });
    int returnVal = fc.showOpenDialog(frame);
    if ((returnVal != JFileChooser.APPROVE_OPTION) || (fc.getSelectedFile() == null)) {
      // I want a file, y'know!
      return;
    }
    HostDialog hd = new HostDialog(frame);
    hd.setVisible(true);
    if ((hd.playerName == null) || (hd.serverPass == null) || (hd.port == 0)) {
      return;
    }

    // Players should have to enter a non-blank, non-whitespace name.
    boolean foundValid = false;
    char[] nameChars = hd.playerName.toCharArray();
    for (int loop = 0; !foundValid && (loop < nameChars.length); loop++) {
      if (!Character.isWhitespace(nameChars[loop])) {
        foundValid = true;
      }
    }
    if (!foundValid) {
      JOptionPane.showMessageDialog(
          frame,
          Messages.getString("MegaMek.PlayerNameAlert1.message"),
          Messages.getString("MegaMek.PlayerNameAlert1.title"),
          JOptionPane.ERROR_MESSAGE); // $NON-NLS-1$ //$NON-NLS-2$
      return;
    }

    // kick off a RNG check
    d6();
    // start server
    try {
      server = new Server(hd.serverPass, hd.port, hd.register, hd.register ? hd.metaserver : "");
    } catch (IOException ex) {
      System.err.println("could not create server socket on port " + hd.port);
      StringBuffer error = new StringBuffer();
      error
          .append("Error: could not start server at localhost")
          .append(":")
          .append(hd.port)
          .append(" (")
          .append(ex.getMessage())
          .append(").");
      JOptionPane.showMessageDialog(
          frame,
          error.toString(),
          Messages.getString("MegaMek.HostGameAlert.title"),
          JOptionPane.ERROR_MESSAGE); // $NON-NLS-1$
      return;
    }
    if (!server.loadGame(fc.getSelectedFile())) {
      JOptionPane.showMessageDialog(
          frame,
          Messages.getString("MegaMek.LoadGameAlert.message"),
          Messages.getString("MegaMek.LoadGameAlert.title"),
          JOptionPane.ERROR_MESSAGE); // $NON-NLS-1$ //$NON-NLS-2$
      server.die();
      server = null;
      return;
    }
    client = new Client(hd.playerName, "localhost", hd.port); // $NON-NLS-1$
    ClientGUI gui = new ClientGUI(client, controller);
    controller.clientgui = gui;
    frame.setCursor(new Cursor(Cursor.WAIT_CURSOR));
    gui.initialize();
    frame.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
    if (!client.connect()) {
      StringBuffer error = new StringBuffer();
      error
          .append("Error: could not connect to server at localhost")
          .append(":")
          .append(hd.port)
          .append(".");
      JOptionPane.showMessageDialog(
          frame,
          error.toString(),
          Messages.getString("MegaMek.HostGameAlert.title"),
          JOptionPane.ERROR_MESSAGE); // $NON-NLS-1$
      frame.setVisible(false);
      client.die();
    }
    optdlg = null;

    // free some memory thats only needed in lounge
    // This normally happens in the deployment phase in Client, but
    // if we are loading a game, this phase may not be reached
    MechFileParser.dispose();
    RandomNameGenerator.getInstance().dispose();
    // We must do this last, as the name and unit generators can create
    // a new instance if they are running
    MechSummaryCache.dispose();

    launch(gui.getFrame());
  }
コード例 #2
0
  /** Contruct a MegaMek, and display the main menu in the specified frame. */
  private void createGUI() {
    createController();

    // Set a couple of things to make the Swing GUI look more "Mac-like" on
    // Macs
    // Taken from:
    // http://www.devdaily.com/apple/mac/java-mac-native-look/Introduction.shtml
    System.setProperty("apple.laf.useScreenMenuBar", "true");
    System.setProperty("com.apple.mrj.application.apple.menu.about.name", "MegaMek");

    // this should also help to make MegaMek look more system-specific
    try {
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (Exception e) {
      System.err.println("Error setting look and feel!");
      e.printStackTrace();
    }

    ToolTipManager.sharedInstance().setInitialDelay(GUIPreferences.getInstance().getTooltipDelay());
    if (GUIPreferences.getInstance().getTooltipDismissDelay() >= 0) {
      ToolTipManager.sharedInstance()
          .setDismissDelay(GUIPreferences.getInstance().getTooltipDismissDelay());
    }
    frame = new JFrame("MegaMek"); // $NON-NLS-1$
    frame.addWindowListener(
        new WindowAdapter() {
          @Override
          public void windowClosing(WindowEvent e) {
            quit();
          }
        });
    frame.setBackground(SystemColor.menu);
    frame.setForeground(SystemColor.menuText);
    List<Image> iconList = new ArrayList<Image>();
    iconList.add(
        frame
            .getToolkit()
            .getImage(new File(Configuration.miscImagesDir(), FILENAME_ICON_16X16).toString()));
    iconList.add(
        frame
            .getToolkit()
            .getImage(new File(Configuration.miscImagesDir(), FILENAME_ICON_32X32).toString()));
    iconList.add(
        frame
            .getToolkit()
            .getImage(new File(Configuration.miscImagesDir(), FILENAME_ICON_48X48).toString()));
    iconList.add(
        frame
            .getToolkit()
            .getImage(new File(Configuration.miscImagesDir(), FILENAME_ICON_256X256).toString()));
    frame.setIconImages(iconList);
    CommonMenuBar menuBar = new CommonMenuBar();
    menuBar.addActionListener(actionListener);
    frame.setJMenuBar(menuBar);
    showMainMenu();

    // set visible on middle of screen
    frame.pack();
    frame.setLocationRelativeTo(null);
    // init the cache
    MechSummaryCache.getInstance();

    // Show the window.
    frame.setVisible(true);

    // tell the user about the readme...
    if (GUIPreferences.getInstance().getNagForReadme()) {
      ConfirmDialog confirm =
          new ConfirmDialog(
              frame,
              Messages.getString("MegaMek.welcome.title") + MegaMek.VERSION, // $NON-NLS-1$
              Messages.getString("MegaMek.welcome.message"), // $NON-NLS-1$
              true);
      confirm.setVisible(true);
      if (!confirm.getShowAgain()) {
        GUIPreferences.getInstance().setNagForReadme(false);
      }
      if (confirm.getAnswer()) {
        showHelp();
      }
    }
  }