?? tictactoedialogview.java
字號:
/* * TicTacToePanel.java * * Created on February 6, 2005, 6:28 AM */package net.jxta.myjxta.plugins.tictactoe;import info.clearthought.layout.TableLayout;import net.jxta.logging.Logging;import net.jxta.myjxta.View;import net.jxta.myjxta.dialog.Dialog;import net.jxta.myjxta.plugin.PluginView;import net.jxta.myjxta.util.Resources;import javax.swing.*;import javax.swing.border.TitledBorder;import java.awt.*;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 java.util.logging.Level;import java.util.logging.Logger;/** * @author Jeff Moore */public final class TicTacToeDialogView extends JPanel implements PluginView { private final int gridRows = 3; private final 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 final 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 final int blinkVector = 8; //# times to blink private final 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(TicTacToeDialogView.class.getName()); /** * Creates a new instance of TicTacToePanel */ public TicTacToeDialogView(View view, Dialog dialog, boolean locallyInitiated) { LOG.setLevel(Level.INFO); m_isLocallyInitiated = locallyInitiated; if (Logging.SHOW_INFO && LOG.isLoggable(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, dialog); if (Logging.SHOW_INFO && LOG.isLoggable(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(CONFIG_PANEL); this.showConfigPanel(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(CONFIG_PANEL); this.showConfigPanel(CONFIG_CHOOSE_ICON_PANEL); } else if (protocolState == TicTacToeGameControl.SESSION_CONFIG_REQUEST_SENT) { this.updateMessageLabel("Remote Player Choosing Icon"); } else if (protocolState == TicTacToeGameControl.SESSION_CONFIG_ACCEPT_RECEIVED) { this.updateMessageLabel("SESSION_CONFIG_ACCEPT_RECEIVED (Remote Player has choosen Icon)"); } else if (protocolState == TicTacToeGameControl.SESSION_START_REQUEST_SENT) { this.updateMessageLabel("SESSION_START_REQUEST_SENT "); } else if (protocolState == TicTacToeGameControl.SESSION_START_REQUEST_RECEIVED) { this.updateMessageLabel("Starting Game (session start request received)"); showPanel(GAME_CONTROL_PANEL); resetGrid(); } else if (protocolState == TicTacToeGameControl.SESSION_START_ACCEPT_RECEIVED) { this.updateMessageLabel("Starting Game (session start accepted)"); showPanel(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"); } else { System.out.println("strange state"+protocolState+ " is local:"+this.getGameControl().isLocallyInitiated()); } } 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(), GAME_CONTROL_PANEL); deckPanel.add(this.buildErrorPanel(), ERROR_PANEL); uiPanel.add(deckPanel, "0,1,c,c"); showPanel(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(), CONFIG_CHOOSE_ICON_PANEL); this.showConfigPanel(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) {
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -