/** Called by server on game start */ void gameStarted(Board b, int gameID, String[] players) { clearData(); getContentPane().remove(boardPanel); boardPanel = b.createBoardPanel(); boardPanel.setPreferredSize(new Dimension(BOARD_SIZE, BOARD_SIZE)); getContentPane().add(boardPanel, BorderLayout.CENTER); pack(); repaint(); StringBuffer title = new StringBuffer("Game " + gameID + ": "); for (int i = 0; i < players.length; i++) title.append(players[i] + (i < players.length - 1 ? " vs. " : "")); this.setTitle(title.toString()); this.board = b; boardUpdated(b, null); setCurrentBoard(0); enableLaunchActions(false); enableServerActions(false); openAction.setEnabled(false); closeAction.setEnabled(false); killServerAction.setEnabled(true); statusLabel.setText("Game in progress, " + board.getNameForID(board.getTurn()) + " to play."); }
public GUI() { super(); try { sprites = ImageIO.read(new File("Sprites2.png")); } catch (IOException e) { e.printStackTrace(); } lowerPane.setPreferredSize(new Dimension(384, 130)); gameInfo.setEditable(false); boardPanel.setPreferredSize(new Dimension(384, 384)); boardPanel.addMouseListener(new gameMouseListener()); boardPanel.setBackground(Color.cyan); itemList.setPreferredSize(new Dimension(384, 80)); gamePanel.setPreferredSize(new Dimension(400, 700)); gamePanel.add(boardPanel); lowerPane.add("Info", gameInfo); chatPanel.setLayout(new BorderLayout()); chatPanel.add(chatScrollPane, BorderLayout.CENTER); chatScrollPane.setVerticalScrollBar(new JScrollBar()); chatArea.setEditable(false); chatPanel.add(chatField, BorderLayout.SOUTH); lowerPane.add("Chat", chatPanel); gamePanel.add(lowerPane); gamePanel.add(endTurnButton); gamePanel.add(itemList); gamePanel.add(useItemButton); gamePanel.add(surrenderButton); // login panel newAccountButton.addActionListener(new CreateAccountListener()); failedLoginPanel.add(tryNewUser); failedLoginPanel.add(newAccountButton); loginButton.addActionListener(new LoginListener()); loginPanel.setLayout(new GridLayout(2, 2)); usernameHere.setEditable(false); passwordHere.setEditable(false); loginPanel.add(usernameHere); loginPanel.add(username); loginPanel.add(passwordHere); loginPanel.add(password); logisticsPanel.setPreferredSize(new Dimension(350, 300)); loginButtonPanel.add(loginButton); logisticsPanel.add(loginPanel); logisticsPanel.add(loginButtonPanel); logisticsPanel.add(failedLoginPanel); mainPanel.add(logisticsPanel); possibleUnitList.setPreferredSize(new Dimension(200, 104)); userUnitList.setPreferredSize(new Dimension(200, 87)); setuploadoutPanel(); setupGameRoomLobby(); setupMainOptionsPanel(); setUpUserInfoPanel(); mainFrame.setResizable(false); mainFrame.add(mainPanel); mainFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); mainFrame.setSize(800, 800); mainFrame.setVisible(true); setListeners(); }
public ServerGUI() { // This constructor just builds the GUI, nothing // very interresting here... super("Board Game"); currentBoard = -1; this.setDefaultCloseOperation(DISPOSE_ON_CLOSE); // Define some menu/toolbar actions firstAction = new AbstractAction("First move", new ImageIcon("image/first.png")) { public void actionPerformed(ActionEvent arg0) { setCurrentBoard(0); } }; backAction = new AbstractAction("Prev. move", new ImageIcon("image/prev.png")) { public void actionPerformed(ActionEvent arg0) { setCurrentBoard(currentBoard - 1); } }; fwdAction = new AbstractAction("Next move", new ImageIcon("image/next.png")) { public void actionPerformed(ActionEvent arg0) { setCurrentBoard(currentBoard + 1); } }; lastAction = new AbstractAction("Last move", new ImageIcon("image/last.png")) { public void actionPerformed(ActionEvent arg0) { setCurrentBoard(boardHistory.size() - 1); } }; openAction = new AbstractAction("Open log...") { public void actionPerformed(ActionEvent ev) { JFileChooser chooser = new JFileChooser(); chooser.setCurrentDirectory(new File(Server.LOG_DIR)); chooser.setFileFilter( new FileFilter() { public boolean accept(File arg0) { return arg0.isDirectory() || arg0.getName().endsWith(".log"); } public String getDescription() { return "Board game log files"; } }); int returnVal = chooser.showOpenDialog(theFrame); if (returnVal == JFileChooser.APPROVE_OPTION) { try { clearData(); loadLogFile(chooser.getSelectedFile().getAbsolutePath()); } catch (Exception e) { JOptionPane.showMessageDialog(theFrame, e, "Load Error", JOptionPane.ERROR_MESSAGE); } } } }; closeAction = new AbstractAction("Close log") { public void actionPerformed(ActionEvent ev) { clearData(); } }; lastAction.setEnabled(false); firstAction.setEnabled(false); backAction.setEnabled(false); fwdAction.setEnabled(false); closeAction.setEnabled(false); // An action to launch a human player playAsAction = new AbstractAction("Launch human player") { public void actionPerformed(ActionEvent arg0) { // Diable this action enableLaunchActions(false); // Create and lauch a new client Client c = new Client(theHumanPlayer, server.getHostName(), server.getPort()); (new Thread(c)).start(); } }; // An action to terminate the server killServerAction = new AbstractAction("End game") { public void actionPerformed(ActionEvent e) { server.killServer(); } }; killServerAction.setEnabled(false); // An action to start a server JMenu launchMenu = new JMenu("Launch"); launchMenu.add(playAsAction); launchMenu.addSeparator(); clientActions = new AbstractAction[PLAYER_CLASSES.length]; for (int i = 0; i < PLAYER_CLASSES.length; i++) { clientActions[i] = new LaunchClientAction(PLAYER_CLASSES[i]); clientActions[i].setEnabled(false); launchMenu.add(clientActions[i]); } launchMenu.addSeparator(); serverActions = new AbstractAction[BOARD_CLASSES.length]; for (int i = 0; i < BOARD_CLASSES.length; i++) { serverActions[i] = new LaunchServerAction(BOARD_CLASSES[i]); launchMenu.add(serverActions[i]); } launchMenu.addSeparator(); fromHereAction = new AbstractAction("Launch server from current position") { public void actionPerformed(ActionEvent arg0) { try { Board bd = (Board) boardHistory.get(currentBoard); int currentMove = currentBoard; // The current move might be the special 'null' at the // end of the list used to display the outcome if (currentMove == moveHistory.size() - 1 && moveHistory.get(currentMove) == null) currentMove--; if (bd == null || currentMove < 1 || bd.getWinner() != Board.NOBODY) throw new IllegalStateException("Can't start game from move " + currentMove); Object[] h = moveHistory.subList(1, currentMove + 1).toArray(); Move[] hist = new Move[h.length]; for (int i = 0; i < h.length; i++) hist[i] = (Move) h[i]; clearData(); ; java.lang.reflect.Constructor co = bd.getClass().getConstructor(new Class[0]); Board b = (Board) co.newInstance(new Object[0]); Server svr = new Server(b, false, true, Server.DEFAULT_PORT, Server.DEFAULT_TIMEOUT); svr.setHistory(hist); setServer(svr); svr.setGUI(theFrame); (new Thread(svr)).start(); } catch (Exception ex) { System.err.println("Error launching server:"); ex.printStackTrace(); } } }; fromHereAction.setEnabled(false); launchMenu.add(fromHereAction); launchMenu.addSeparator(); launchMenu.add(killServerAction); enableLaunchActions(false); JMenuBar menuBar = new JMenuBar(); JMenu fileMenu = new JMenu("File"); fileMenu.add(openAction); fileMenu.add(closeAction); menuBar.add(fileMenu); JMenu histMenu = new JMenu("History"); histMenu.add(firstAction); histMenu.add(backAction); histMenu.add(fwdAction); histMenu.add(lastAction); menuBar.add(histMenu); menuBar.add(launchMenu); JToolBar toolBar = new JToolBar("History"); toolBar.add(firstAction); toolBar.add(backAction); toolBar.add(fwdAction); toolBar.add(lastAction); toolBar.setFloatable(false); this.setJMenuBar(menuBar); this.getContentPane().setLayout(new BorderLayout()); this.getContentPane().add(toolBar, BorderLayout.NORTH); boardPanel.setPreferredSize(new Dimension(BOARD_SIZE, BOARD_SIZE)); this.getContentPane().add(boardPanel, BorderLayout.CENTER); moveList = new JList(moveListModel); moveList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); moveList.addListSelectionListener( new ListSelectionListener() { public void valueChanged(ListSelectionEvent arg0) { int idx = moveList.getSelectedIndex(); if (idx >= 0 && idx < moveHistory.size() && idx != currentBoard) setCurrentBoard(idx); } }); JScrollPane movePane = new JScrollPane(moveList); movePane.setPreferredSize(new Dimension(LIST_WIDTH, BOARD_SIZE)); this.getContentPane().add(movePane, BorderLayout.EAST); statusLabel = new JLabel("GUI Loaded"); this.getContentPane().add(statusLabel, BorderLayout.SOUTH); }