📄 tictactoedialogview.java
字号:
localActionPlayAgain(); } }); gameStateLabel = new JLabel("gameStateLabel"); panel.add(gameStateLabel, "1,0,l,c"); playersTurnLabel = new JLabel("Players Turn"); panel.add(playersTurnLabel, "2,0,r,c"); playersTurnLabel.setFont(new Font("Arial", Font.BOLD, 12)); playersTurnLabel.setForeground(Color.BLACK); return panel; } private JPanel buildUpperInfoPanel() { double sizes[][] = {{TableLayout.FILL, TableLayout.FILL}, {TableLayout.PREFERRED}}; TableLayout tbl = new TableLayout(sizes); JPanel panel = new JPanel(tbl); panel.setBackground(Color.white); localPlayerNameLabel = new JLabel(this.localPlayerName); panel.add(localPlayerNameLabel, "0,0,l,c"); localPlayerNameLabel.setFont(new Font("Arial", Font.BOLD, 18)); localPlayerNameLabel.setForeground(Color.BLUE); remotePlayerNameLabel = new JLabel("remotePlayer"); panel.add(remotePlayerNameLabel, "1,0,r,c"); remotePlayerNameLabel.setFont(new Font("Arial", Font.BOLD, 18)); remotePlayerNameLabel.setForeground(Color.BLUE); return panel; } private JPanel buildLowerInfoPanel() { double sizes[][] = {{TableLayout.FILL, TableLayout.FILL, TableLayout.FILL}, {TableLayout.FILL}}; TableLayout tbl = new TableLayout(sizes); JPanel panel = new JPanel(tbl); panel.setBackground(Color.white); remotePlayerIconLabel = new JLabel(); panel.add(remotePlayerIconLabel, "0,0,l,c"); remotePlayerIconLabel.setIcon(this.iconBlank); JPanel statsPanel = buildGameStatsPanel(); panel.add(statsPanel, "1,0,c,c"); statsPanel.setBorder(null); localPlayerIconLabel = new JLabel(); panel.add(localPlayerIconLabel, "2,0,r,c"); localPlayerIconLabel.setIcon(this.iconBlank); return panel; } private JPanel buildGameInfoPanel() { double sizes[][] = {{TableLayout.FILL}, {TableLayout.PREFERRED, TableLayout.FILL}}; TableLayout tbl = new TableLayout(sizes); JPanel panel = new JPanel(tbl); panel.setBackground(Color.white); JSeparator separator = new JSeparator(SwingConstants.HORIZONTAL); panel.add(separator, "0,0,2,0"); panel.add(this.buildUpperInfoPanel(), "0,0"); panel.add(this.buildLowerInfoPanel(), "0,1"); return panel; } private JPanel buildGameStatsPanel() { double sizes[][] = {{TableLayout.FILL}, {TableLayout.PREFERRED, TableLayout.PREFERRED}}; TableLayout tbl = new TableLayout(sizes); JPanel panel = new JPanel(tbl); panel.setBackground(Color.white); panel.setBorder(null); double sizesLabel[][] = {{TableLayout.FILL}, {TableLayout.PREFERRED, TableLayout.FILL}}; TableLayout tblLabel = new TableLayout(sizesLabel); JPanel panelLabel = new JPanel(tblLabel); panel.add(panelLabel, "0,0,c,c"); panelLabel.setBackground(Color.white); panelLabel.setBorder(null); double sizesData[][] = {{TableLayout.FILL, TableLayout.PREFERRED, TableLayout.FILL}, {TableLayout.PREFERRED, TableLayout.PREFERRED}}; TableLayout tblData = new TableLayout(sizesData); JPanel panelData = new JPanel(tblData); panel.add(panelData, "0,1"); panelData.setBackground(Color.white); panelData.setBorder(null); //winsLabel JLabel winsLabel = new JLabel("WINS"); panelLabel.add(winsLabel, "0,0,c,c"); winsLabel.setFont(new Font("Arial", Font.PLAIN, 18)); winsLabel.setForeground(Color.BLACK); //separator JSeparator separatorH = new JSeparator(SwingConstants.HORIZONTAL); panelLabel.add(separatorH, "0,1"); //localPlayerWinsLabel localPlayerWinsLabel = new JLabel("0"); panelData.add(localPlayerWinsLabel, "0,0,l,c"); localPlayerWinsLabel.setFont(new Font("Arial", Font.BOLD, 26)); localPlayerWinsLabel.setForeground(Color.BLACK); //separator JSeparator separatorV = new JSeparator(SwingConstants.VERTICAL); panelData.add(separatorV, "1,0,1,0"); //remotePlayerWinsLabel remotePlayerWinsLabel = new JLabel("0"); panelData.add(remotePlayerWinsLabel, "2,0,r,c"); remotePlayerWinsLabel.setFont(new Font("Arial", Font.BOLD, 26)); remotePlayerWinsLabel.setForeground(Color.BLACK); return panel; } private JPanel buildGameGridPanel() { MouseAdapter gridMouseLisetner = new MouseAdapter() { public void mouseClicked(MouseEvent e) { if (e.getSource() instanceof TicTacToeGridBox) { if (getGameControl().getPlayersTurn() == TicTacToeGameControl.LOCAL_PLAYER) { if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) { LOG.info("BoxClicked : "); } TicTacToeGridBox box = (TicTacToeGridBox) e.getSource(); /** * if user hasn't already played this box */ if (box.isAvailable()) { box.makeMove(TicTacToeGameControl.LOCAL_PLAYER); box.setAvailable(false); notifyMoveListeners(box); } } else { setTempStatus("Not Your Move!"); } } } }; double b = 5; double sizes[][] = {{b, TableLayout.PREFERRED, b, TableLayout.PREFERRED, b, TableLayout.PREFERRED, b}, {b, TableLayout.PREFERRED, b, TableLayout.PREFERRED, b, TableLayout.PREFERRED, b}}; TableLayout tbl = new TableLayout(sizes); JPanel gridPanel = new JPanel(tbl); String rowId = null; String colId = null; gridPanel.setOpaque(false); gridPanel.setBorder(null); for (int r = 0; r < this.gridRows; r++) { if (r == 0) rowId = "A"; if (r == 1) rowId = "B"; if (r == 2) rowId = "C"; for (int c = 0; c < this.gridColumns; c++) { colId = String.valueOf(c); TicTacToeGridBox box = new TicTacToeGridBox(rowId + colId, this); box.addMouseListener(gridMouseLisetner); if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) { LOG.info("grid [" + r + "] [" + c + "]"); } grid[r][c] = box; // LOG.info("these are" + r + "," + c ); gridPanel.add(box, this.coordinatesToString(r, c)); } } return gridPanel; } private String coordinatesToString(int r, int c) { String suffix = null; if (r == 0) { r = 1; } else if (r == 1) { r = 3; } else if (r == 2) { r = 5; } if (c == 0) { c = 1; suffix = new String("l,c"); } else if (c == 1) { c = 3; suffix = new String("c,c"); } else if (c == 2) { c = 5; suffix = new String("r,c"); } String rString = String.valueOf(r); String cString = String.valueOf(c); LOG.info("String coords " + rString + "," + cString); return rString + "," + cString + "," + suffix; } private JPanel buildGamePanel() { double sizes[][] = {{TableLayout.FILL}, {TableLayout.PREFERRED, TableLayout.PREFERRED, TableLayout.PREFERRED, TableLayout.PREFERRED}}; TableLayout tbl = new TableLayout(sizes); JPanel panel = new JPanel(tbl); panel.setBorder(null); panel.setBackground(Color.WHITE); panel.add(buildGameStatusPanel(), "0,0,c,c"); panel.add(buildGameGridPanel(), "0,1,c,c"); panel.add(buildGameInfoPanel(), "0,2,c,c"); closeButton = new JButton("Close"); panel.add(closeButton, "0,3,r,c"); closeButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { destruct(); } }); return panel; } protected void showPanel(final String panelName) { EventQueue.invokeLater(new Runnable() { public void run() { cardLayout.show(deckPanel, panelName); } }); } protected void showConfigPanel(final String panelName) { EventQueue.invokeLater(new Runnable() { public void run() { configCardLayout.show(configDeckPanel, panelName); } }); } private JPanel buildConfigChooseIconPanel() { double sizes[][] = {{TableLayout.PREFERRED, TableLayout.PREFERRED, TableLayout.PREFERRED}, {TableLayout.PREFERRED, TableLayout.PREFERRED, TableLayout.PREFERRED}}; TableLayout tbl = new TableLayout(sizes); JPanel panel = new JPanel(tbl); panel.setBorder(new TitledBorder("Choose Icon")); remoteIconLabel = new JLabel(getIconBlank()); panel.add(remoteIconLabel, "0,0"); remoteIconLabel.setBorder(new TitledBorder("Remote Player")); currentIconLabel = new JLabel(getIconBlank()); panel.add(currentIconLabel, "1,0"); currentIconLabel.setBorder(new TitledBorder("Local Player")); JLabel labelX = new JLabel(getIconX()); panel.add(labelX, "0,1"); labelX.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { if (getRemotePlayerIconType() != ICON_TYPE_X) { setLocalPlayerIconType(ICON_TYPE_X); setLocalPlayerIcon(getIconX()); currentIconLabel.setIcon(getIconX()); } else { updateMessageLabel("Remote peer chose that icon."); } } }); JLabel labelO = new JLabel(getIconO()); panel.add(labelO, "1,1"); labelO.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { if (getRemotePlayerIconType() != ICON_TYPE_O) { setLocalPlayerIcon(getIconO()); setLocalPlayerIconType(ICON_TYPE_O); currentIconLabel.setIcon(getIconO()); } else { updateMessageLabel("Remote peer chose that icon."); } } }); customIconLabel = new JLabel(getIconBlank()); panel.add(customIconLabel, "2,1"); customIconLabel.setBorder(new TitledBorder("Custom Icon (Click)")); customIconLabel.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { localActionCustomIconChosen(); } }); startGameButton = new JButton("Start Game"); panel.add(startGameButton, "2,2"); startGameButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { getGameControl().viewActionConfigured(); } }); return panel; } /* class BasicImageFilter extends FileFilter { public boolean accept(File f) { String ext = null; String name = f.getName (); int extIndex = name.lastIndexOf ("."); if (extIndex > 0 && extIndex < name.length ()) { ext = name.substring ( extIndex + 1, name.length () -1); } } public String getDescription() { return "Basic Image Filter" } } **/ protected void localActionCustomIconChosen() { JFileChooser chooser = new JFileChooser(); // TODO: use a filefilter int returnVal = chooser.showOpenDialog(getRootPane().getParent()); if (returnVal == JFileChooser.APPROVE_OPTION) { File file = chooser.getSelectedFile(); long fileSize = file.length(); LOG.info("localActionCustomIconChoosen " + file.getAbsolutePath() + " " + fileSize); if (fileSize > 0L && fileSize <= TicTacToeGameControl.MAX_ICON_SIZE) { LOG.info("setting icon "); ImageIcon icon1 = new ImageIcon(new ImageIcon(this.gameControl.getFileImage(file.getAbsolutePath())).getImage().getScaledInstance(68, 60, Image.SCALE_SMOOTH)); ImageIcon icon2 = new ImageIcon(new ImageIcon(this.gameControl.getFileImage(file.getAbsolutePath())).getImage().getScaledInstance(68, 60, Image.SCALE_SMOOTH)); ImageIcon icon3 = new ImageIcon(new ImageIcon(this.gameControl.getFileImage(file.getAbsolutePath())).getImage().getScaledInstance(68, 60, Image.SCALE_SMOOTH)); // xxx: chcek for exceptions here customIconLabel.setIcon(icon1); setLocalPlayerIconType(ICON_TYPE_CUSTOM); setLocalPlayerIcon(icon2); setLocalPlayerIconFileName(file.getAbsolutePath()); currentIconLabel.setIcon(icon3); } } } protected void localActionPlayAgain() { EventQueue.invokeLater(new Runnable() { public void run() { playAgainButton.setEnabled(false); playAgainButton.setVisible(false); } }); updateGameStateView("New Game!"); this.resetGrid(); getGameControl().viewActionNewGame(); } protected TicTacToeGameControl getGameControl() { return gameControl; } public void setCurrentPlayersTurn(int player) { if (TicTacToeGameControl.REMOTE_PLAYER == player) { updatePlayersTurnView(this.remotePlayerName + DISPLAY_REMOTE_PLAYERS_TURN); } else if (TicTacToeGameControl.LOCAL_PLAYER == player) { updatePlayersTurnView(DISPLAY_LOCAL_PLAYERS_TURN); } else if (TicTacToeGameControl.SESSION_ENDED == player) { updatePlayersTurnView(""); } } public void setRemotePlayerName(String name) { this.remotePlayerName = name; updateRemotePlayerNameView(); } public void updateRemotePlayerNameView() { EventQueue.invokeLater(new Runnable() { public void run() { remotePlayerNameLabel.setText(remotePlayerName);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -