public TetrisWindow(String hostName, int serverPortNumber) throws IOException { super("Rainbow Tetris"); connection = new TetrisClient(hostName, serverPortNumber); myID = connection.getID(); board = new Board(); message = new JLabel("Waiting for two players to connect.", JLabel.CENTER); board.setBackground(Color.WHITE); board.setPreferredSize(new Dimension(300, 660)); board.addMouseListener( new MouseAdapter() { public void mousePressed(MouseEvent evt) { doMouseClick(); } }); message.setBackground(Color.LIGHT_GRAY); message.setOpaque(true); JPanel content = new JPanel(); content.setLayout(new BorderLayout(2, 2)); content.setBorder(BorderFactory.createLineBorder(Color.GRAY, 2)); content.setBackground(Color.GRAY); content.add(board, BorderLayout.CENTER); content.add(message, BorderLayout.SOUTH); setContentPane(content); pack(); setResizable(false); setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent evt) { dispose(); connection.disconnect(); try { Thread.sleep(333); } catch (InterruptedException e) { } System.exit(0); } }); setLocation(200, 100); setVisible(true); }
private void newState(TetrisGameState state) { if (state.playerDisconnected) { JOptionPane.showMessageDialog(this, "Your opponent has disconnected.\nThe game is ended."); System.exit(0); } this.state = state; board.repaint(); if (!state.gameInProgress || state == null) { return; } else if (state.winner != -1 || state.tie) { setTitle("Game Over"); } else { setTitle("Game In Progress"); if (myID == state.player1) message.setText( "You: " + state.score1 + " pts " + state.KO1 + " KOs Opponent: " + state.score2 + " pts " + state.KO2 + " KOs"); else message.setText( "You: " + state.score2 + " pts " + state.KO2 + " KOs Opponent: " + state.score1 + " pts " + state.KO1 + " KOs"); } }