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

📄 megamekgui.java

📁 MegaMek is a networked Java clone of BattleTech, a turn-based sci-fi boardgame for 2+ players. Fight
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            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 = null;            return;        }        client = new Client(hd.playerName, "localhost", hd.port); //$NON-NLS-1$        ClientGUI gui = new ClientGUI(client);        gui.initialize();        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.HostGameAllert.title"), JOptionPane.ERROR_MESSAGE); //$NON-NLS-1$            frame.setVisible(false);            client.die();        }        optdlg = null;        launch(gui.getFrame());    }    /**     * Host a game constructed from a scenario file     */    private void scenario() {        JFileChooser fc = new JFileChooser("data" + File.separatorChar + "scenarios");        fc.setLocation(frame.getLocation().x + 150, frame.getLocation().y + 100);        fc.setDialogTitle(Messages.getString("MegaMek.SelectScenarioDialog.title"));        fc.setFileFilter(new FileFilter() {            public boolean accept(File dir) {                return (dir.getName() != null && dir.getName().endsWith(".mms")); //$NON-NLS-1$            }            public String getDescription() {                return ".mms";            }        });        int returnVal = fc.showOpenDialog(frame);        if (returnVal != JFileChooser.APPROVE_OPTION || fc.getSelectedFile() == null) {            // I want a file, y'know!            return;        }        ScenarioLoader sl = new ScenarioLoader(fc.getSelectedFile());        IGame g;        try {            g = sl.createGame();        } catch (Exception e) {            JOptionPane.showMessageDialog(frame, Messages.getString("MegaMek.HostScenarioAllert.message") + e.getMessage(), Messages.getString("MegaMek.HostScenarioAllert.title"), JOptionPane.ERROR_MESSAGE); //$NON-NLS-1$ //$NON-NLS-2$            return;        }                // popup options dialog        GameOptionsDialog god = new GameOptionsDialog(frame, g.getOptions());        god.update(g.getOptions());        god.setEditable(true);        god.setVisible(true);        for(IBasicOption opt:god.getOptions()) {            IOption orig = g.getOptions().getOption(opt.getName());            orig.setValue(opt.getValue());        }        god=null;        // get player types and colors set        Player[] pa = new Player[g.getPlayersVector().size()];        g.getPlayersVector().copyInto(pa);        ScenarioDialog sd = new ScenarioDialog(frame, pa);        sd.setVisible(true);        if (!sd.bSet) {            return;        }        // host with the scenario.  essentially copied from host()        HostDialog hd = new HostDialog(frame);        boolean hasSlot = false;        if (!("".equals(sd.localName)))            hasSlot = true;        hd.yourNameF.setText(sd.localName);        hd.setVisible(true);        // verify dialog data        if (hd.playerName == null || hd.serverPass == null || hd.port == 0) {            return;        }        sd.localName = hd.playerName;        // 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.HostScenarioAllert1.message"), Messages.getString("MegaMek.HostScenarioAllert1.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);        } 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.HostGameAllert.title"), JOptionPane.ERROR_MESSAGE); //$NON-NLS-1$            return;        }        server.setGame(g);        // apply any scenario damage        sl.applyDamage(server);        ClientGUI gui = null;        if (!"".equals(sd.localName)) { //$NON-NLS-1$            // initialize game            client = new Client(hd.playerName, "localhost", hd.port); //$NON-NLS-1$            gui = new ClientGUI(client);            gui.initialize();            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.HostScenarioAllert.title"), JOptionPane.ERROR_MESSAGE); //$NON-NLS-1$                frame.setVisible(false);                client.die();            }        }        optdlg = null;                //calculate initial BV        server.calculatePlayerBVs();        // setup any bots        for (int x = 0; x < pa.length; x++) {            if (sd.playerTypes[x] == ScenarioDialog.T_BOT) {                BotClient c = new TestBot(pa[x].getName(), "localhost", hd.port); //$NON-NLS-1$                c.game.addGameListener(new BotGUI(c));                if (!c.connect()) {                }                c.retrieveServerInfo();            }        }        // If he didn't have a name when hasSlot was set, then the host should be        // an observer.        if (!hasSlot) {            Enumeration pE = server.getGame().getPlayers();            while (pE.hasMoreElements()) {                Player tmpP = (Player) pE.nextElement();                if (tmpP.getName().equals(sd.localName))                    tmpP.setObserver(true);            }        }        launch(gui.getFrame());    }    /**     * Connect to to a game and then launch the chat lounge.     */    private void connect() {        ConnectDialog cd;        cd = new ConnectDialog(frame);        cd.setVisible(true);        // verify dialog data        if (cd.playerName == null || cd.serverAddr == null || cd.port == 0) {            return;        }        // Players should have to enter a non-blank, non-whitespace name.        boolean foundValid = false;        char[] nameChars = cd.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.ConnectAllert.message"), Messages.getString("MegaMek.ConnectAllert.title"), JOptionPane.ERROR_MESSAGE); //$NON-NLS-1$ //$NON-NLS-2$            return;        }        // initialize game        client = new Client(cd.playerName, cd.serverAddr, cd.port);        ClientGUI gui = new ClientGUI(client);        gui.initialize();        if (!client.connect()) {            StringBuffer error = new StringBuffer();            error.append("Error: could not connect to server at ").append(cd.serverAddr).append(':').append(cd.port).append('.');            JOptionPane.showMessageDialog(frame, error.toString(), Messages.getString("MegaMek.ConnectAllert.title"), JOptionPane.ERROR_MESSAGE); //$NON-NLS-1$            frame.setVisible(false);            client.die();        }        launch(gui.getFrame());    }    private void connectBot() {        ConnectDialog cd;        cd = new ConnectDialog(frame);        cd.setVisible(true);        // verify dialog data        if (cd.playerName == null || cd.serverAddr == null || cd.port == 0) {            return;        }        // Players should have to enter a non-blank, non-whitespace name.        boolean foundValid = false;        char[] nameChars = cd.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.ConnectGameAllert.message"), Messages.getString("MegaMek.ConnectGameAllert.title"), JOptionPane.ERROR_MESSAGE); //$NON-NLS-1$ //$NON-NLS-2$            return;        }        // initialize game        client = new TestBot(cd.playerName, cd.serverAddr, cd.port);        client.game.addGameListener(new BotGUI((BotClient) client));        ClientGUI gui = new ClientGUI(client);        gui.initialize();        if (!client.connect()) {            StringBuffer error = new StringBuffer();            error.append("Error: could not connect to server at ").append(cd.serverAddr).append(':').append(cd.port).append('.');            JOptionPane.showMessageDialog(frame, error.toString(), Messages.getString("MegaMek.ConnectAllert.title"), JOptionPane.ERROR_MESSAGE); //$NON-NLS-1$            frame.setVisible(false);            client.die();        }        launch(gui.getFrame());        client.retrieveServerInfo();    }    private void addBag(JComponent comp, GridBagLayout gridbag, GridBagConstraints c) {        gridbag.setConstraints(comp, c);        frame.getContentPane().add(comp);    }    /**     * Called when the user selects the "Help->About" menu item.     */    private void showAbout() {        // Do we need to create the "about" dialog?        if (about == null) {            about = new CommonAboutDialog(frame);        }        // Show the about dialog.        about.setVisible(true);    }    /**     * Called when the user selects the "Help->Contents" menu item.     */    private void showHelp() {        if (help == null) {            help = showHelp(frame, "readme"); //$NON-NLS-1$        }        // Show the help dialog.        help.setVisible(true);    }    /**     * display the filename in a CommonHelpDialog     */    private static CommonHelpDialog showHelp(JFrame frame, String filename) {        Locale l = Locale.getDefault();        File helpfile = new File(filename + '-' + l.getDisplayLanguage(Locale.ENGLISH) + ".txt"); //$NON-NLS-1$ //$NON-NLS-2$        if (!helpfile.exists()) {            helpfile = new File(filename + ".txt"); //$NON-NLS-1$        }        return new CommonHelpDialog(frame, helpfile);    }    /**     * Called when the user selects the "View->Client Settings" menu item.     */    private void showSettings() {        // Do we need to create the "settings" dialog?        if (setdlg == null) {            setdlg = new CommonSettingsDialog(frame);        }        // Show the settings dialog.        setdlg.setVisible(true);    }    /**     * Called when the quit buttons is pressed or the main menu is closed.     */    private static void quit() {        PreferenceManager.getInstance().save();        System.exit(0);    }    /**     * Hides this window for later.  Listens to the frame until it closes,     * then calls unlaunch().     */    private void launch(JFrame launched) {        // listen to new frame        launched.addWindowListener(new WindowAdapter() {            public void windowClosing(WindowEvent e) {                unlaunch();            }            public void windowClosed(WindowEvent e) {                unlaunch();            }        });        // hide menu frame        frame.setVisible(false);    }    /**     * Un-hides the main menu and tries to clean up the client or server.     */    private void unlaunch() {        // clean up server, if we have one        if (server != null) {            server.die();            server = null;        }        // show menu frame        frame.setVisible(true);        //just to free some memory        client = null;        System.gc();        System.runFinalization();    }    private ActionListener actionListener = new ActionListener() {        //        // ActionListener        //        public void actionPerformed(ActionEvent ev) {            if ("fileBoardNew".equalsIgnoreCase(ev.getActionCommand())) { //$NON-NLS-1$                showEditor();            }            if ("fileBoardOpen".equalsIgnoreCase(ev.getActionCommand())) { //$NON-NLS-1$                showEditorOpen();            }            if ("fileGameNew".equalsIgnoreCase(ev.getActionCommand())) { //$NON-NLS-1$                host();            }            if ("fileGameScenario".equalsIgnoreCase(ev.getActionCommand())) { //$NON-NLS-1$                scenario();            }            if ("fileGameConnect".equalsIgnoreCase(ev.getActionCommand())) { //$NON-NLS-1$                connect();            }            if ("fileGameConnectBot".equalsIgnoreCase(ev.getActionCommand())) { //$NON-NLS-1$                connectBot();            }            if ("fileGameOpen".equalsIgnoreCase(ev.getActionCommand())) { //$NON-NLS-1$                loadGame();            }            if ("viewGameOptions".equalsIgnoreCase(ev.getActionCommand())) { //$NON-NLS-1$                showGameOptions();            }            if ("helpAbout".equalsIgnoreCase(ev.getActionCommand())) { //$NON-NLS-1$                showAbout();            }            if ("helpContents".equalsIgnoreCase(ev.getActionCommand())) { //$NON-NLS-1$                showHelp();            }            if ("viewClientSettings".equalsIgnoreCase(ev.getActionCommand())) { //$NON-NLS-1$                showSettings();            }            if ("quit".equalsIgnoreCase(ev.getActionCommand())) { //$NON-NLS-1$                quit();            }        }    };}

⌨️ 快捷键说明

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