예제 #1
0
  private void deathStuff() {
    float rec = readRecord();

    if (rec < points) {
      try {
        textOutput();
      } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }

    record.setText("The record is: " + (int) rec);
    add(record);
    add(score);
    add(tips);
    add(restart);
    add(reset);
    reset.setLabel("Reset Record");
    reset.addActionListener(this);
    reset.setSize(100, 20);
    reset.setLocation(300, 10);
    restart.addActionListener(this);
    restart.setSize(100, 20);
    restart.setLocation(150, 200);

    add(exit);
    exit.addActionListener(this);
    exit.setSize(160, 20);
    exit.setLocation(120, 250);
  }
예제 #2
0
  public UserInfoDialog(Frame parent) {
    super(parent, "Input", true);
    setLayout(null);

    l.setFont(default_font);
    l.setSize(50, 30);
    l.setLocation(30, 50);

    name.setFont(default_font);
    name.setSize(150, 30);
    name.setLocation(90, 50);
    // name.selectAll();

    ok.setFont(default_font);
    ok.setSize(50, 30);
    ok.setLocation(30, 90);

    add(l);
    add(name);
    add(ok);
    ok.addActionListener(this);
    setSize(300, 150);

    Point my_loc = parent.getLocation();
    my_loc.x += 50;
    my_loc.y += 150;
    setLocation(my_loc);
    show();
  }
예제 #3
0
  @Override
  public void actionPerformed(ActionEvent e) {
    if (e.getSource() == restart) {
      map.clean();
      start();
    }
    if (e.getSource() == exit) {
      if (random.nextInt(2) == 0) {
        exit.setLabel("No!, wurm's hungry");
      } else {
        exit.setLabel("More Food!!!");
      }
    }
    if (e.getSource() == reset) {
      if (reset.getLabel() == "Reset Record") {
        reset.setSize(180, 20);
        reset.setLocation(220, 10);
        reset.setLabel("Realy? All that hard work..");
      } else {

        try {
          this.resetRecord();
        } catch (IOException e1) {
          // TODO Auto-generated catch block
          e1.printStackTrace();
        }
        record.setText("The record is: " + (int) this.readRecord());
      }
    }
  }
예제 #4
0
  private void openDialog(String title, String string) {
    // TODO Auto-generated method stub
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension dialogSize = new Dimension(200, 120);
    final Dialog dialog = new Dialog((Frame) getParent(), title);
    dialog.setSize(dialogSize);
    dialog.setLayout(null);
    dialog.setResizable(true);

    Label label = new Label(string);
    label.setSize(400, 50);
    label.setLocation(20, 30);
    dialog.add(label, "Center");

    Button enter = new Button("Return");
    enter.setSize(50, 25);
    enter.setLocation(75, 80);
    enter.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent arg0) {
            // TODO Auto-generated method stub
            dialog.dispose();
          }
        });
    dialog.add(enter);

    dialog.setLocation(
        (screenSize.width - dialogSize.width + 50) / 2,
        (screenSize.height - dialogSize.height + 40) / 2);
    dialog.setModal(true);
    dialog.addWindowListener(
        new WindowAdapter() {
          public void windowClosing(WindowEvent arg0) {
            // TODO Auto-generated method stub
            dialog.dispose();
          }
        });
    dialog.setVisible(true);
  }
예제 #5
0
 /* WARNING: THIS METHOD WILL BE REGENERATED. */
 private java.awt.Button getDetailsButton() {
   if (ivjDetailsButton == null) {
     try {
       ivjDetailsButton = new java.awt.Button();
       ivjDetailsButton.setName("DetailsButton");
       ivjDetailsButton.setLocation(33, 170);
       ivjDetailsButton.setBackground(java.awt.SystemColor.control);
       ivjDetailsButton.setSize(23, 23);
       ivjDetailsButton.setFont(new java.awt.Font("dialog", 0, 8));
       ivjDetailsButton.setActionCommand("...");
       ivjDetailsButton.setLabel("...");
       // user code begin {1}
       // user code end
     } catch (java.lang.Throwable ivjExc) {
       // user code begin {2}
       // user code end
       handleException(ivjExc);
     }
   }
   ;
   return ivjDetailsButton;
 }
예제 #6
0
  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();
  }
예제 #7
0
  /**
   * Class that contains the main function which creates the window for the game and implements the
   * server/client socket
   */
  @SuppressWarnings("deprecation")
  public static void main(String args[]) {
    int i = -1; // Variable to keep track of the result from the game

    do // Do-while loop
    {

      try {
        JFrame frame = getFrame(); // Create the frame

        // Dialog to get the name from the player as string, put the string in a label
        String sname = returnNameString();
        JLabel label = new JLabel(sname);

        // Create the status bar panel and shove it down the bottom of the frame
        JPanel statusPanel = new JPanel();
        statusPanel.setBorder(new BevelBorder(BevelBorder.LOWERED));
        statusPanel.setBackground(Color.red);
        frame.add(statusPanel, BorderLayout.SOUTH);

        // Create a label, placed at the bottom of the frame
        JLabel statusLabel = new JLabel();
        statusLabel.setHorizontalAlignment(SwingConstants.LEFT);
        statusPanel.add(statusLabel);
        Button clearButton = new Button("Clear"); // Creating a "clear" button
        clearButton.setSize(new Dimension(10, 20));
        statusPanel.add(label);
        statusPanel.add(clearButton); // Adding the button to the panel

        statusPanel.add(getTime()); // Adding the clock to the status panel

        frame.setVisible(true);

        char ch;
        if (args.length == 0) ch = 'O';
        else ch = 'X';

        TicTacPanel ticTacPanel = new TicTacPanel(ch); // Create a panel for the game
        TicTacAction ticTacAction = new TicTacAction(ticTacPanel); // Handle actions in the game

        clearButton.addActionListener(ticTacAction);
        ticTacPanel.addMouseListener(ticTacAction);
        frame.add(ticTacPanel);
        frame.show();

        // Creating socket and I/O stream objects
        Socket s;
        ObjectOutputStream oops;
        ObjectInputStream oips;

        switch (ch) {
          case 'O':
            s = (new ServerSocket(7777)).accept();
            oops = new ObjectOutputStream(s.getOutputStream());
            oops.writeObject(ticTacPanel.ttt);
            ticTacAction.ready = false;
            break;
          case 'X':
          default:
            s = new Socket(args[0], 7777);
            ticTacAction.ready = true;
        }
        while (true) // Infinite loop
        {
          oips = new ObjectInputStream(s.getInputStream());
          ticTacPanel.ttt = (TicTacGame) (oips.readObject());
          ticTacPanel.paint(ticTacPanel.getGraphics());
          ticTacAction.ready = true;
          while (ticTacAction.ready) {
            Thread.sleep(100);
          }
          oops = new ObjectOutputStream(s.getOutputStream());
          oops.writeObject(ticTacPanel.ttt);

          i = ticTacPanel.ttt.checkWin(); // Check if there's a winner
          if (i == 1) // A winner is declared
          {
            TicTacPanel.infoBox("The winner is " + sname + "!", "Game Over");
            ticTacPanel.ttt.clearAll();
            ticTacPanel.paint(ticTacPanel.getGraphics());
          } else if (i == 0) // No winner, but every square is covered so the game is done
          {
            TicTacPanel.infoBox("We have a tie!", "Game Over");
            ticTacPanel.ttt.clearAll();
            ticTacPanel.paint(ticTacPanel.getGraphics());
          }
        }
      } catch (Exception e) {
        System.out.println(e);
        e.printStackTrace();
        System.exit(1);
      }
    } while (i == -1); // End of do-while loop
  }
예제 #8
0
  public MyFrame() {
    super("My Dialer");

    GridLayout gl = new GridLayout(7, 1, 2, 2);
    setLayout(gl);

    Panel displayPanel = new Panel();
    Panel padPanel = new Panel();
    Panel padPanel1 = new Panel();
    Panel padPanel2 = new Panel();
    Panel padPanel3 = new Panel();
    Panel padPanel4 = new Panel();
    Panel butPanel = new Panel();
    Panel dialingPanel = new Panel();

    tfPhoneNumber = new TextField(20);
    displayPanel.add(tfPhoneNumber);
    //        padPanel.setLayout(new GridLayout(4,3,2,2));

    int i;
    for (i = 0; i < 9; i++) {
      dialPad[i] = new Button(Integer.toString(i + 1));
    }
    dialPad[9] = new Button("*");
    dialPad[10] = new Button("0");
    dialPad[11] = new Button("#");

    MyMouseAdapter ma = new MyMouseAdapter();
    for (i = 0; i < 12; i++) {
      dialPad[i].setForeground(Color.blue);
      dialPad[i].addMouseListener(ma);
      dialPad[i].setPreferredSize(new Dimension(25, 25));
      //	    dialPad[i].setSize(35,35);
      //	    padPanel.add(dialPad[i]);
    }

    // Try flow layout as grid layout does not give set button size.
    for (i = 0; i < 3; i++) {
      padPanel1.add(dialPad[i]);
    }

    for (i = 3; i < 6; i++) {
      padPanel2.add(dialPad[i]);
    }

    for (i = 6; i < 9; i++) {
      padPanel3.add(dialPad[i]);
    }
    for (i = 9; i < 12; i++) {
      padPanel4.add(dialPad[i]);
    }

    tfDialing = new TextField(20);
    dialingPanel.add(tfDialing);
    dialButton = new Button("Dial");
    cancelButton = new Button("Cancel/Erase");
    tfDialing.setEditable(false);
    dialButton.setSize(40, 20);
    cancelButton.setSize(40, 20);
    dialButton.addMouseListener(ma);
    dialButton.setForeground(Color.blue);
    cancelButton.setForeground(Color.blue);
    cancelButton.addMouseListener(ma);
    butPanel.add(dialButton);
    butPanel.add(cancelButton);

    displayPanel.setSize(250, 40);
    padPanel1.setSize(250, 40);
    padPanel2.setSize(180, 40);
    padPanel3.setSize(180, 40);
    padPanel4.setSize(180, 40);
    butPanel.setSize(250, 25);
    dialingPanel.setSize(250, 20);

    // Add Panels to Frame
    add(displayPanel);
    // add(padPanel);
    add(padPanel1);
    add(padPanel2);
    add(padPanel3);
    add(padPanel4);
    add(butPanel);
    add(dialingPanel);

    //        MyMouseAdapter ma = new MyMouseAdapter();
    //        button.addMouseListener(ma);

    setSize(250, 250);
    setVisible(true);
  }