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

📄 clientgui.java

📁 MegaMek is a networked Java clone of BattleTech, a turn-based sci-fi boardgame for 2+ players. Fight
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
/* * MegaMek - Copyright (C) 2000,2001,2002,2003,2004 Ben Mazur (bmazur@sev.org) * *  This program is free software; you can redistribute it and/or modify it *  under the terms of the GNU General Public License as published by the Free *  Software Foundation; either version 2 of the License, or (at your option) *  any later version. * *  This program is distributed in the hope that it will be useful, but *  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY *  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License *  for more details. */package megamek.client.ui.AWT;import megamek.client.Client;import megamek.client.event.BoardViewListener;import megamek.client.ui.AWT.util.PlayerColors;import megamek.client.ui.AWT.widget.BufferedPanel;import megamek.common.BuildingTarget;import megamek.common.Coords;import megamek.common.Entity;import megamek.common.EntityListFile;import megamek.common.HexTarget;import megamek.common.IGame;import megamek.common.IHex;import megamek.common.MechSummaryCache;import megamek.common.MinefieldTarget;import megamek.common.Player;import megamek.common.Targetable;import megamek.common.Terrains;import megamek.common.event.GameEndEvent;import megamek.common.event.GameListener;import megamek.common.event.GameListenerAdapter;import megamek.common.event.GameMapQueryEvent;import megamek.common.event.GamePhaseChangeEvent;import megamek.common.event.GamePlayerChatEvent;import megamek.common.event.GamePlayerDisconnectedEvent;import megamek.common.event.GameReportEvent;import megamek.common.event.GameSettingsChangeEvent;import megamek.common.util.Distractable;import megamek.common.util.StringUtil;import java.applet.Applet;import java.applet.AudioClip;import java.awt.BorderLayout;import java.awt.Button;import java.awt.CardLayout;import java.awt.Component;import java.awt.Dialog;import java.awt.Dimension;import java.awt.FileDialog;import java.awt.Frame;import java.awt.GraphicsConfiguration;import java.awt.GraphicsDevice;import java.awt.GraphicsEnvironment;import java.awt.GridBagConstraints;import java.awt.GridBagLayout;import java.awt.Image;import java.awt.Label;import java.awt.MenuItem;import java.awt.Panel;import java.awt.Point;import java.awt.PopupMenu;import java.awt.Rectangle;import java.awt.Scrollbar;import java.awt.SystemColor;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.KeyEvent;import java.awt.event.KeyListener;import java.awt.event.MouseListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.awt.event.WindowListener;import java.io.File;import java.io.FilenameFilter;import java.io.IOException;import java.util.Enumeration;import java.util.Hashtable;import java.util.Iterator;import java.util.Map;import java.util.TreeMap;import java.util.Vector;public class ClientGUI        extends Panel        implements MouseListener, WindowListener, ActionListener, KeyListener {    // Action commands.    public static final String VIEW_MEK_DISPLAY = "viewMekDisplay"; //$NON-NLS-1$    public static final String VIEW_MINI_MAP = "viewMiniMap"; //$NON-NLS-1$    public static final String VIEW_LOS_SETTING = "viewLOSSetting"; //$NON-NLS-1$    public static final String VIEW_UNIT_OVERVIEW = "viewUnitOverview"; //$NON-NLS-1$    public static final String VIEW_ZOOM_IN = "viewZoomIn"; //$NON-NLS-1$    public static final String VIEW_ZOOM_OUT = "viewZoomOut"; //$NON-NLS-1$    // a frame, to show stuff in    public Frame frame;    // A menu bar to contain all actions.    protected CommonMenuBar menuBar = null;    private CommonAboutDialog about = null;    private CommonHelpDialog help = null;    private CommonSettingsDialog setdlg = null;    private String helpFileName = "readme.txt"; //$NON-NLS-1$    // keep me    private ChatterBox cb;    public BoardView1 bv;    private Panel scroller;    public Dialog mechW;    public MechDisplay mechD;    public Dialog minimapW;    public MiniMap minimap;    public PopupMenu popup = new PopupMenu(Messages.getString("ClientGUI.BoardPopup")); //$NON-NLS-1$    private UnitOverview uo;    public Ruler ruler; // added by kenn    protected Component curPanel;    public ChatLounge chatlounge = null;    // some dialogs...    private BoardSelectionDialog boardSelectionDialog;    private GameOptionsDialog gameOptionsDialog;    private MechSelectorDialog mechSelectorDialog;    private CustomBattleArmorDialog customBADialog;    private StartingPositionDialog startingPositionDialog;    private PlayerListDialog playerListDialog;    /**     * Save and Open dialogs for MegaMek Unit List (mul) files.     */    private FileDialog dlgLoadList = null;    private FileDialog dlgSaveList = null;    public Client client;    /**     * Cache for the "bing" soundclip.     */    AudioClip bingClip = null;    /**     * Map each phase to the name of the card for the main display area.     */    private Hashtable mainNames = new Hashtable();    /**     * The <code>Panel</code> containing the main display area.     */    private Panel panMain = new Panel();    /**     * The <code>CardLayout</code> of the main display area.     */    private CardLayout cardsMain = new CardLayout();    /**     * Map each phase to the name of the card for the secondary area.     */    private Hashtable secondaryNames = new Hashtable();    /**     * The <code>Panel</code> containing the secondary display area.     */    private Panel panSecondary = new Panel();    /**     * The <code>CardLayout</code> of the secondary display area.     */    private CardLayout cardsSecondary = new CardLayout();    /**     * Map phase component names to phase component objects.     */    private Hashtable phaseComponents = new Hashtable();    //TODO: there's a better place for this    private Map bots = new TreeMap(StringUtil.stringComparator());    /**     * Current Selected entity     */    int selectedEntityNum = Entity.NONE;    /**     * Construct a client which will display itself in a new frame.  It will     * not try to connect to a server yet.  When the frame closes, this client     * will clean up after itself as much as possible, but will not call     * System.exit().     */    public ClientGUI(Client client) {        super(new BorderLayout());        this.client = client;        loadSoundClip();        panMain.setLayout(cardsMain);        panSecondary.setLayout(cardsSecondary);        Panel panDisplay = new Panel(new BorderLayout());        panDisplay.add(panMain, BorderLayout.CENTER);        panDisplay.add(panSecondary, BorderLayout.SOUTH);        this.add(panDisplay, BorderLayout.CENTER);    }    public IBoardView getBoardView() {        return bv;    }    /*     * Try to load the "bing" sound clip.     */    public void loadSoundClip() {        if (GUIPreferences.getInstance().getSoundBingFilename() == null)            return;        try {            File file = new File(GUIPreferences.getInstance().getSoundBingFilename());            if (!file.exists()) {                System.err.println("Failed to load audio file: " + GUIPreferences.getInstance().getSoundBingFilename()); //$NON-NLS-1$                return;            }            bingClip = Applet.newAudioClip(file.toURL());        } catch (Exception ex) {            ex.printStackTrace();        }    }    public void keyPressed(KeyEvent ke) {        switch (ke.getKeyCode()) {            case KeyEvent.VK_PAGE_DOWN:                bv.zoomIn();                break;            case KeyEvent.VK_PAGE_UP:                bv.zoomOut();                break;        }    }    public void keyTyped(KeyEvent ke) {    }    public void keyReleased(KeyEvent ke) {    }    /**     * Display a system message in the chat box.     *     * @param message the <code>String</code> message to be shown.     */    public void systemMessage(String message) {        this.cb.systemMessage(message);    }    /**     * Initializes a number of things about this frame.     */    private void initializeFrame() {        this.frame = new Frame(Messages.getString("ClientGUI.title")); //$NON-NLS-1$        menuBar.setGame(client.game);        frame.setMenuBar(menuBar);        Rectangle virtualBounds = new Rectangle();        GraphicsEnvironment ge = GraphicsEnvironment.                getLocalGraphicsEnvironment();        GraphicsDevice[] gs =                ge.getScreenDevices();        for (int j = 0; j < gs.length; j++) {             GraphicsDevice gd = gs[j];            GraphicsConfiguration[] gc =                gd.getConfigurations();            for (int i=0; i < gc.length; i++) {                virtualBounds =                    virtualBounds.union(gc[i].getBounds());            }        }         if (GUIPreferences.getInstance().getWindowSizeHeight() != 0) {            int x = GUIPreferences.getInstance().getWindowPosX();            int y = GUIPreferences.getInstance().getWindowPosY();            int w = GUIPreferences.getInstance().getWindowSizeWidth();            int h = GUIPreferences.getInstance().getWindowSizeHeight();            if (x < virtualBounds.getMinX() || x + w >virtualBounds.getMaxX())                x = 0;            if (y < virtualBounds.getMinY() || y + h >virtualBounds.getMaxY())                y = 0;            if(w > virtualBounds.getWidth())                w = (int)virtualBounds.getWidth();            if(h > virtualBounds.getHeight())                h = (int)virtualBounds.getHeight();            frame.setLocation(x, y);            frame.setSize(w, h);        } else {            frame.setSize(800, 600);        }        frame.setBackground(SystemColor.menu);        frame.setForeground(SystemColor.menuText);        frame.setIconImage(frame.getToolkit().getImage("data/images/misc/megamek-icon.gif")); //$NON-NLS-1$    }    /**     * Lays out the frame by setting this Client object to take up the full     * frame display area.     */    private void layoutFrame() {        frame.setTitle(client.getName() + Messages.getString("ClientGUI.clientTitleSuffix")); //$NON-NLS-1$        frame.setLayout(new BorderLayout());        frame.add(this, BorderLayout.CENTER);        frame.validate();    }    /**     * Have the client register itself as a listener wherever it's needed.     * <p/>     * According to http://www-106.ibm.com/developerworks/java/library/j-jtp0618.html     * it is a major bad no-no to perform these registrations before the     * constructor finishes, so this function has to be called after the     * <code>Client</code> is created.     */    public void initialize() {        menuBar = new CommonMenuBar(this.getClient());        initializeFrame();        try {            client.game.addGameListener(gameListener);            // Create the board viewer.            bv = new BoardView1(client.game, frame, this);            // Place the board viewer in a set of scrollbars.            scroller = new Panel();            scroller.setLayout(new BorderLayout());            Scrollbar vertical = new Scrollbar(Scrollbar.VERTICAL);            Scrollbar horizontal = new Scrollbar(Scrollbar.HORIZONTAL);            scroller.add(bv, BorderLayout.CENTER);            // Scrollbars are broken for "Brandon Drew" <brandx0@hotmail.com>            if (System.getProperty                    ("megamek.client.clientgui.hidescrollbars", "false").equals //$NON-NLS-1$ //$NON-NLS-2$                    ("false")) { //$NON-NLS-1$                // Assign the scrollbars to the board viewer.                scroller.add(vertical, BorderLayout.EAST);                scroller.add(horizontal, BorderLayout.SOUTH);                bv.setScrollbars(vertical, horizontal);            }        } catch (IOException e) {            doAlertDialog(Messages.getString("ClientGUI.FatalError.title"), Messages.getString("ClientGUI.FatalError.message") + e); //$NON-NLS-1$ //$NON-NLS-2$            die();        }        layoutFrame();        frame.setVisible(true);        menuBar.addActionListener(this);        frame.addKeyListener(this);        frame.addWindowListener(new WindowAdapter() {            public void windowClosing(WindowEvent e) {                frame.setVisible(false);                saveSettings();                die();            }        });        UnitLoadingDialog unitLoadingDialog = new UnitLoadingDialog(frame);        if (!MechSummaryCache.getInstance().isInitialized()) {            unitLoadingDialog.setVisible(true);        }        uo = new UnitOverview(this);        bv.addDisplayable(uo);        bv.addMouseListener(this);        bv.addKeyListener(this);        bv.add(popup);        Dimension screenSize = frame.getToolkit().getScreenSize();        int x, y, h, w;        mechW = new Dialog(frame, Messages.getString("ClientGUI.MechDisplay"), false); //$NON-NLS-1$        x = GUIPreferences.getInstance().getDisplayPosX();        y = GUIPreferences.getInstance().getDisplayPosY();        h = GUIPreferences.getInstance().getDisplaySizeHeight();        w = GUIPreferences.getInstance().getDisplaySizeWidth();        if (x + w > screenSize.width) {            x = 0;            w = Math.min(w, screenSize.width);        }        if (y + h > screenSize.height) {            y = 0;            h = Math.min(h, screenSize.height);        }        mechW.setLocation(x, y);        mechW.setSize(w, h);        mechW.setResizable(true);

⌨️ 快捷键说明

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