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

📄 tictactoedialogpanel.java

📁 jxta官方例程
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/* * TicTacToePanel.java * * Created on February 6, 2005, 6:28 AM */package net.jxta.myjxta.plugins.tictactoe;import info.clearthought.layout.TableLayout;import java.awt.CardLayout;import java.awt.Color;import java.awt.Dimension;import java.awt.EventQueue;import java.awt.Font;import java.awt.Image;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.io.File;import java.util.ArrayList;import java.util.Iterator;import java.util.List;import javax.swing.ImageIcon;import javax.swing.JButton;import javax.swing.JFileChooser;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JSeparator;import javax.swing.SwingConstants;import javax.swing.border.TitledBorder;import net.jxta.myjxta.View;import net.jxta.myjxta.dialog.Dialog;import net.jxta.myjxta.plugin.PluginPanel;import net.jxta.myjxta.util.Resources;import org.apache.log4j.Level;import org.apache.log4j.Logger;/** * * @author Jeff Moore */public class TicTacToeDialogPanel extends JPanel implements PluginPanel{        private int gridRows = 3;        private int gridColumns = 3;        private static final String NO_RESPONSE_PANEL = "noResponsePanel";        private static final String CONFIG_CHOOSE_ICON_PANEL = "configChooseIconPanel";        public static final String ICON_TYPE_CUSTOM = "IconTypeCustom";        public static final String ICON_TYPE_X = "IconTypeX";        public static final String ICON_TYPE_O = "IconTypeO";        private TicTacToeGridBox[][] grid = new TicTacToeGridBox[gridRows][gridColumns];        private static final String CONFIG_PANEL = "ConfigPanel";        private static final String GAME_CONTROL_PANEL = "gameControlPanel";        private static final String ERROR_PANEL = "errorPanel";        private final static String DISPLAY_LOCAL_PLAYERS_TURN = "Your Turn!";        private final static String DISPLAY_REMOTE_PLAYERS_TURN = "'s Turn!";        private final static String DISPLAY_SESSION_TTT_DISCONNECTED = "Disconnected";        private final static String DISPLAY_SESSION_TTT_DISCONNECTING = "Disconnecting";        private final static String DISPLAY_SESSION_TTT_ENDED = "Game Over";        private final static String DISPLAY_SESSION_TTT_ENDING = "Game Ending...";        private final static String DISPLAY_SESSION_TTT_STARTING = "Starting" ;        private final static String DISPLAY_SESSION_TTT_STARTED = "Started";        private final static String DISPLAY_SESSION_TTT_CONNECTED = "Connected";        private final static String DISPLAY_SESSION_TTT_CONNECTING = "Connecting";            private final static String GAME_DRAW = "Draw!!";        private final static String GAME_WON = "Game Won!!";        private final static String GAME_LOST = "Game Lost!!";        private int blinkVector = 8; //# times to blink        private int blinkRate = 250; // in milliseconds        private JLabel localPlayerNameLabel = null;        private JLabel remotePlayerNameLabel = null;        private JLabel remotePlayerWinsLabel = null;        private JLabel localPlayerWinsLabel = null;        private ImageIcon localPlayerIcon = null;        private ImageIcon remotePlayerIcon = null;        private String localPlayerIconType = null;        private String remotePlayerIconType = null;        private JLabel productLabel = null;        private JLabel errorMessageLabel = null;        private JLabel protocolStateLabel = null;        private JLabel messageLabel = null;        private JLabel playersTurnLabel = null;        private JLabel localPlayerIconLabel = null;        private JLabel remotePlayerIconLabel = null;        private JLabel customIconLabel = null;        private JLabel currentIconLabel = null;        private JLabel remoteIconLabel = null;        private TicTacToeGameControl gameControl = null;        private List<MoveListener> moveListeners = null;        private ImageIcon iconX = null;        private ImageIcon iconBlank = null;        private ImageIcon iconO = null;        private JLabel gameStateLabel = null;        private CardLayout cardLayout = null;        private CardLayout configCardLayout = null;        private JPanel deckPanel = null;        private JPanel configDeckPanel = null;        private Dialog tttDialog = null;                private JButton startGameButton = null;        private JButton closeButton = null;        private JButton playAgainButton = null;        private String remotePlayerName = null;        private String localPlayerName = null;        private String localPlayerIconFileName = null;    private final boolean m_isLocallyInitiated;        static final Logger LOG = Logger.getLogger (TicTacToeDialogPanel.class);        /** Creates a new instance of TicTacToePanel */    public TicTacToeDialogPanel (View view, Dialog dialog,boolean locallyInitiated) {                LOG.setLevel (Level.INFO);        m_isLocallyInitiated=locallyInitiated;        if (LOG.isEnabledFor (Level.INFO)) {            LOG.info ("Contructor");                    }                this.tttDialog = dialog;                this.moveListeners = new ArrayList<MoveListener> ();                Resources res = Resources.getInstance ();                iconX = res.getIconResource ("TicTacToe.X");                iconO = res.getIconResource ("TicTacToe.O");                iconBlank = res.getIconResource ("TicTacToe.BLANK");                localPlayerName = this.tttDialog.getGroup ().getPeerGroup ().getPeerName ();                UI ();                                gameControl = new TicTacToeGameControl (this, view, dialog);                                if (LOG.isEnabledFor (Level.INFO)) {            LOG.info ("End constructor");                    }                gameControl.initSession ();            }            public Dialog getDialog () {        return this.tttDialog;    }        public void dismiss () {        this.destruct ();    }    private void destruct () {                if(getGameControl()!=null) {        getGameControl ().destruct ();        }                if(moveListeners!= null) {        moveListeners.clear ();        }                        gameControl = null;                        if(tttDialog != null) {                            this.tttDialog.close ();        }                        //myJxtaView.removeDialog (tttDialog);            }        private void updateMessageLabel (final String message) {                        if(messageLabel != null) {            EventQueue.invokeLater (new Runnable () {                                                public void run () {                                        messageLabel.setText (message);                }            });        }    }        private void updateProtocolStateLabel (final String protocolState) {        if(protocolStateLabel != null) {            EventQueue.invokeLater (new Runnable () {                                                public void run () {                                        protocolStateLabel.setText (protocolState);                }            });        }    }        public void protocolStateChanged (int protocolState) {                LOG.info ("gameControl " + this.gameControl);        LOG.info ("getSessionString " + this.gameControl.getSessionStateString (protocolState));                this.updateProtocolStateLabel (this.gameControl.getSessionStateString (protocolState));                if(protocolState == TicTacToeGameControl.SESSION_INVITE_REQUEST_RECEIVED) {                                                this.updateMessageLabel ("Remote player Choosing Icon");                                            }else if (protocolState == TicTacToeGameControl.SESSION_INVITE_ACCEPT_RECEIVED) {                                    showPanel (TicTacToeDialogPanel.CONFIG_PANEL);                        this.showConfigPanel (TicTacToeDialogPanel.CONFIG_CHOOSE_ICON_PANEL);                        this.updateMessageLabel ("Choose Your Icon");                    }else if(protocolState == TicTacToeGameControl.SESSION_CONFIG_ACCEPT_SENT ) {                        this.updateMessageLabel ("Starting...");                    }else if(protocolState == TicTacToeGameControl.SESSION_CONFIG_REQUEST_RECEIVED ) {                        this.updateMessageLabel ("Choose Your Icon");                        remoteIconLabel.setIcon (this.getRemotePlayerIcon ());                        remotePlayerIconChosen();                        showPanel (TicTacToeDialogPanel.CONFIG_PANEL);                        this.showConfigPanel (TicTacToeDialogPanel.CONFIG_CHOOSE_ICON_PANEL);                    }else if(protocolState == TicTacToeGameControl.SESSION_CONFIG_REQUEST_SENT ) {                        this.updateMessageLabel ("Remote Player Choosing Icon");                    }else if(protocolState == TicTacToeGameControl.SESSION_START_REQUEST_RECEIVED) {                        this.updateMessageLabel ("Starting Game");                        showPanel (TicTacToeDialogPanel.GAME_CONTROL_PANEL);                        resetGrid ();                                }else if(protocolState == TicTacToeGameControl.SESSION_START_ACCEPT_RECEIVED) {                        this.updateMessageLabel ("Starting Game");                        showPanel (TicTacToeDialogPanel.GAME_CONTROL_PANEL);                        resetGrid ();                    }else if(protocolState == TicTacToeGameControl.SESSION_PLAYING) {                        updateGameStateView ("Playing");                                }else if(protocolState == TicTacToeGameControl.SESSION_ENDED) {                        this.updateMessageLabel ("Game Over");                        updatePlayersTurnView ("");                        if(isLocallyInitiated ()) {                playAgainButton.setEnabled (true);                playAgainButton.setVisible (true);            }                    }else if(protocolState == TicTacToeGameControl.SESSION_END_REQUEST_RECEIVED) {            this.updateMessageLabel ("Game Ending");        }else if(protocolState == TicTacToeGameControl.SESSION_DISCONNECT_REQUEST_RECEIVED) {            this.updateMessageLabel ("Disconnecting");        }else if(protocolState == TicTacToeGameControl.SESSION_DISCONNECTED) {            this.updateMessageLabel ("Disconnected");                                }                    }            private void remotePlayerIconChosen() {                    }        private void UI () {                this.setPreferredSize (new Dimension (440, 300));                double sizes[][] = {{TableLayout.FILL},{TableLayout.PREFERRED, TableLayout.FILL}};        TableLayout tbl = new TableLayout (sizes);        JPanel uiPanel = new JPanel (tbl);                uiPanel.add (buildMessagePanel (), "0,0,l,c");                cardLayout  = new CardLayout ();        deckPanel = new JPanel (cardLayout);                deckPanel.add (this.buildNoResponsePanel (), NO_RESPONSE_PANEL);                deckPanel.add (this.buildConfigPanel (), CONFIG_PANEL);                deckPanel.add (this.buildGamePanel (), TicTacToeDialogPanel.GAME_CONTROL_PANEL);                deckPanel.add (this.buildErrorPanel (), TicTacToeDialogPanel.ERROR_PANEL);                uiPanel.add (deckPanel,"0,1,c,c");                showPanel (TicTacToeDialogPanel.NO_RESPONSE_PANEL);                this.add (uiPanel);    }        private JPanel buildErrorPanel () {                double sizes[][] = {{TableLayout.PREFERRED},        {TableLayout.PREFERRED,TableLayout.PREFERRED}};                TableLayout tbl = new TableLayout (sizes);                JPanel panel = new JPanel (tbl);                errorMessageLabel = new JLabel ("ERROR");                panel.add (errorMessageLabel,"0,0");                return panel;            }            private JPanel buildNoResponsePanel () {                double sizes[][] = {{ TableLayout.FILL},        {TableLayout.FILL}};                TableLayout tbl = new TableLayout (sizes);                JPanel panel = new JPanel (tbl);                        return panel;    }        private JPanel buildMessagePanel () {                double b = 5;        double sizes[][] = {{ b, TableLayout.FILL, b},        {b,TableLayout.PREFERRED,b,TableLayout.PREFERRED,b,TableLayout.PREFERRED, b}};                TableLayout tbl = new TableLayout (sizes);                JPanel panel = new JPanel (tbl);                productLabel = new JLabel ("TicTacToe .01");        //productLabel.setFont (new Font ("Arial", Font.BOLD, 14));        productLabel.setForeground (new Color (0.0f,0.0f,0.0f,0.5f));        panel.add (productLabel, "1,1,l,c");                protocolStateLabel = new JLabel ("protocolState");        panel.add (protocolStateLabel, "1,3,l,c");                messageLabel = new JLabel ("Message");        messageLabel.setFont (new Font ("Arial", Font.BOLD, 14));        messageLabel.setForeground (new Color (0.0f,0.0f,0.0f));        panel.add (messageLabel, "1,5,c,c");                return panel;    }        private JPanel buildConfigPanel () {                configCardLayout = new CardLayout ();                configDeckPanel = new JPanel (configCardLayout);                configDeckPanel.add (buildConfigChooseIconPanel (),TicTacToeDialogPanel.CONFIG_CHOOSE_ICON_PANEL);                this.showConfigPanel (TicTacToeDialogPanel.CONFIG_CHOOSE_ICON_PANEL);                return configDeckPanel;            }            private JPanel buildGameStatusPanel () {                double sizes[][] = {{TableLayout.FILL,TableLayout.FILL,TableLayout.FILL},        {TableLayout.PREFERRED}};        TableLayout tbl = new TableLayout (sizes);        JPanel panel = new JPanel (tbl);                panel.setBorder (null);        panel.setBackground (Color.WHITE);                //game status label        playAgainButton = new JButton ("Play Again");        panel.add (playAgainButton, "0,0, l , c");        playAgainButton.setEnabled (false);        playAgainButton.setVisible (false);        playAgainButton.addActionListener (new ActionListener () {            public void actionPerformed (ActionEvent ae) {                localActionPlayAgain ();            }        });

⌨️ 快捷键说明

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