예제 #1
0
 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);
 }
예제 #2
0
    public void paintComponent(Graphics g) {
      super.paintComponent(g);
      if (state == null) {
        g.drawString("Starting up.", 20, 35);
        return;
      }
      if (!paused) {
        //		   g.setColor(Color.RED);
        for (int i = 0; i < staticX.size(); ++i) {
          if (staticX.get(i) == 0 || staticX.get(i) == 1) g.setColor(Color.RED);
          else if (staticX.get(i) == 2 || staticX.get(i) == 3) g.setColor(Color.ORANGE);
          else if (staticX.get(i) == 4 || staticX.get(i) == 5) g.setColor(Color.YELLOW);
          else if (staticX.get(i) == 6 || staticX.get(i) == 7) g.setColor(Color.GREEN);
          else g.setColor(Color.BLUE);
          if (staticX.get(i) >= 0) {
            g.fillRect(
                staticX.get(i) * squareWidth(),
                staticY.get(i) * squareHeight(),
                squareWidth(),
                squareHeight());
          }
        }
        g.setColor(Color.GRAY);
        for (int u = 0; u < 4; ++u) {
          g.fillRect(
              shadowX[u] * squareWidth(),
              shadowY[u] * squareHeight(),
              squareWidth(),
              squareHeight());
        }
        if (randNum == 0) g.setColor(Color.RED);
        else if (randNum == 1) g.setColor(Color.ORANGE);
        else if (randNum == 2) g.setColor(Color.YELLOW);
        else if (randNum == 3) g.setColor(Color.GREEN);
        else if (randNum == 4) g.setColor(Color.BLUE);
        else if (randNum == 5) g.setColor(Color.CYAN);
        else g.setColor(Color.PINK);
        for (int j = 0; j < 4; ++j) {
          g.fillRect(
              currentX[j] * squareWidth(),
              currentY[j] * squareHeight(),
              squareWidth(),
              squareHeight());
        }

        g.setColor(Color.BLACK);
        int tempTime = 0;
        if (state.time % 60 == 0)
          g.drawString("Time Left " + state.time / 60 + ":" + state.time % 60 + tempTime, 180, 35);
        else if (state.time % 60 < 10)
          g.drawString("Time Left " + state.time / 60 + ":" + tempTime + state.time % 60, 180, 35);
        else g.drawString("Time Left " + state.time / 60 + ":" + state.time % 60, 180, 35);
      }
      if (state.time <= 0) {
        if (myID == state.winner) {
          g.setColor(Color.BLUE);
          g.drawString("YOU WIN!", getWidth() / 2, getHeight() / 2);
        } else if (state.tie) {
          g.setColor(Color.BLACK);
          g.drawString("IT'S A TIE!", getWidth() / 2, getHeight() / 2);
        } else {
          g.setColor(Color.RED);
          g.drawString("YOU LOSE!", getWidth() / 2, getHeight() / 2);
        }
      }
    }