public MainFrame(
      final Socket socket, String user, ObjectInputStream reader, ObjectOutputStream writer) {
    super("Main");

    frame = this;
    this.socket = socket;
    owner = user;
    Reader = reader;
    Writer = writer;

    screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    frameSize = new Dimension(300, 510);
    setSize(frameSize);
    setLocation(screenSize.width / 2 - frameSize.width, (screenSize.height - frameSize.height) / 2);
    setBackground(Color.decode("#ccfffa"));
    setResizable(false);
    setLayout(null);

    // add Label "Online User List:"
    Label label1 = new Label("Online User List:");
    label1.setSize(200, 20);
    label1.setLocation(20, 40);
    add(label1);
    // add user list
    userList = new List(10, false);
    userList.setSize(260, 95);
    userList.setLocation(20, 60);
    add(userList);
    // add label "chat content:"
    Label label2 = new Label("Chat Content:");
    label2.setSize(200, 20);
    label2.setLocation(20, 160);
    add(label2);
    // add chat content
    textArea = new TextArea("", 18, 30, TextArea.SCROLLBARS_VERTICAL_ONLY);
    textArea.setBackground(Color.white);
    textArea.setEditable(false);
    textArea.setSize(260, 220);
    textArea.setLocation(20, 180);
    add(textArea);
    // add label "Send Message:"
    Label label3 = new Label("Send Message:");
    label3.setSize(200, 20);
    label3.setLocation(20, 405);
    add(label3);
    // add input textfield
    final TextField inputArea = new TextField();
    inputArea.setSize(205, 25);
    inputArea.setLocation(20, 425);
    add(inputArea);

    // add send button
    Button send = new Button("Send");
    send.addActionListener(
        new ActionListener() {

          public void actionPerformed(ActionEvent arg0) {
            String text = inputArea.getText();
            try {
              Writer.writeObject(new MSG("MSG", text));
            } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
            }
            textArea.setText(textArea.getText().concat("[" + owner + "]:" + text + "\n"));
            inputArea.setText("");
          }
        });
    send.setSize(50, 25);
    send.setLocation(230, 425);
    add(send);

    // add button "Start Game"
    final Button btnGame = new Button("Start Game");
    btnGame.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent arg0) {
            if (!isGame) {
              setSize(460, 510);
              controlPanel = new ControlPanel(socket, Reader, Writer, frame, owner);
              controlPanel.setLocation(300, 60);
              add(controlPanel);
              isGame = true;
              try {
                Writer.writeObject(new MSG("REQLIST", ""));
              } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
              }
              btnGame.setLabel("End Game");
            } else {
              try {
                Writer.writeObject(new MSG("QUIT", ""));
              } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
              }
              setSize(300, 510);
              remove(controlPanel);
              isGame = false;
              btnGame.setLabel("Start Game");
            }
          }
        });
    btnGame.setSize(90, 25);
    btnGame.setLocation(65, 465);
    add(btnGame);

    // add button quit
    Button btnQuit = new Button("Exit");
    btnQuit.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent arg0) {
            if (isGame) {
              if (!controlPanel.canQuit) return;
              try {
                Writer.writeObject(new MSG("QUIT", ""));
              } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
              }
              controlPanel.isHost = false;
              controlPanel.panelHost = null;
              controlPanel.panelHost = null;
              controlPanel.initPanelStart();
              controlPanel.panelStart.setSize(135, 420);
              controlPanel.panelStart.setLocation(0, 0);
              controlPanel.add(controlPanel.panelStart);
            }
            runFlag = false;
            if (socket != null) {
              try {
                Writer.writeObject(new MSG("QUT", ""));
                socket.close();
              } catch (IOException e) {
                // TODO Auto-generated catch block
                // e.printStackTrace();
              }
            }
            System.exit(0);
          }
        });
    btnQuit.setSize(55, 25);
    btnQuit.setLocation(175, 465);
    add(btnQuit);

    new ReceiveThread(Reader).start();
  }
Exemple #2
0
  private void initComponents() {

    // Creating the top level panel
    joinPanel = new JPanel();
    joinPanel.setOpaque(false);
    joinPanel.setPreferredSize(IntroOutro.contentPanelDim);
    joinPanel.setLayout(new BorderLayout());

    // Creating the lower level join panel
    subJoinPanel = new JPanel();
    subJoinPanel.setOpaque(false);
    subJoinPanel.setPreferredSize(new Dimension(300, 500));
    subJoinPanel.setLayout(new BorderLayout(0, 10));

    // Creating the panel used for the "click to join" label
    clickLabelPanel = new JPanel();
    clickLabelPanel.setPreferredSize(new Dimension(300, 50));
    clickLabelPanel.setOpaque(false);
    GridBagLayout g1 = new GridBagLayout();
    clickLabelPanel.setLayout(g1);
    GridBagConstraints c1 = new GridBagConstraints();

    // Creating the panel used for the cancel button
    cancelPanel = new JPanel();
    cancelPanel.setPreferredSize(new Dimension(300, 60));
    cancelPanel.setOpaque(false);
    GridBagLayout g2 = new GridBagLayout();
    cancelPanel.setLayout(g2);
    GridBagConstraints c2 = new GridBagConstraints();

    // Creating the join button
    cancelButton = new JButton();
    cancelButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
    cancelButton.setSize(IntroOutro.navigationButtonDim);
    cancelButton.setPreferredSize(IntroOutro.navigationButtonDim);
    cancelButton.setMaximumSize(IntroOutro.navigationButtonDim);
    cancelButton.setMinimumSize(IntroOutro.navigationButtonDim);
    cancelButton.setBackground(new Color(53, 212, 160));
    cancelButton.setFont(new Font("Tahoma", 1, 14));
    cancelButton.setText("Cancel");
    cancelButton.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));

    // Creating the list of ongoing games
    gameList = new List();
    gameList.setBackground(new Color(230, 230, 230));
    gameList.setSize(new Dimension(350, 400));
    gameList.setPreferredSize(new Dimension(350, 400));
    gameList.setMaximumSize(new Dimension(350, 400));
    gameList.setMinimumSize(new Dimension(350, 400));

    // Dummy games
    gameList.add("Jade's game");
    gameList.add("Somebody else's game");

    // Creating the "Click to Join" label

    clickLabel = new JLabel("Double-click to Join");
    clickLabel.setFont(new Font("Tahoma", 1, 21));

    // Adding the components in the lower level panels
    c1.gridx = 0;
    c1.gridy = 0;
    clickLabelPanel.add(clickLabel, c1);

    c2.gridx = 0;
    c2.gridy = 0;
    cancelPanel.add(Box.createRigidArea(new Dimension(200, 30)), c2);
    c2.gridx = 1;
    c2.gridy = 0;
    cancelPanel.add(cancelButton, c2);

    subJoinPanel.add(clickLabelPanel, BorderLayout.PAGE_START);
    subJoinPanel.add(gameList, BorderLayout.CENTER);
    subJoinPanel.add(cancelPanel, BorderLayout.PAGE_END);

    // Adding the lower level panel to the top level panel with some formatting
    joinPanel.add(Box.createRigidArea(new Dimension(50, 30)), BorderLayout.PAGE_START);
    joinPanel.add(subJoinPanel, BorderLayout.CENTER);
    joinPanel.add(Box.createRigidArea(new Dimension(50, 30)), BorderLayout.LINE_START);
    joinPanel.add(Box.createRigidArea(new Dimension(250, 30)), BorderLayout.LINE_END);

    // Event action for cancel button
    cancelButton.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            superior.closeSubPanel();
          }
        });
    // When a user selects a game, the existing game appears
    gameList.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            superior.goToExistingGame();
          }
        });
  }