/**
   * I Layout sono 3 intanto Layout esterno: è di tipo Border e ordina banner a nord e tastiera a
   * sud Layout del banner: è di tipo Border e ordina messaggio e casella di testo Layout tastiera:
   * è ovviamente una grid.
   */
  public void init() { // DICHIARO GLI OGGETTI DELLA APPLET, DEI PANNELLI. LI AGGIUNGO A QUESTA

    setLayout(new BorderLayout());

    Panel banner = new Panel();
    banner.setLayout(new BorderLayout());
    banner.setBackground(Color.RED);

    Label titolo = new Label("benvenuto in pCalc");
    banner.add(titolo, BorderLayout.NORTH);

    TextField risultato = new TextField(30);
    banner.add(risultato, BorderLayout.SOUTH);

    Panel tastiera = new Panel();
    tastiera.setLayout(new GridLayout(3, 3));

    Button tasto0 = new Button("0");
    tastiera.add(tasto0);
    Button tasto1 = new Button("1");
    tastiera.add(tasto1);
    Button tasto2 = new Button("2");
    tastiera.add(tasto2);
    Button tasto3 = new Button("3");
    tastiera.add(tasto3);
    Button tasto4 = new Button("4");
    tastiera.add(tasto4);
    Button tasto5 = new Button("5");
    tastiera.add(tasto5);
    Button tasto6 = new Button("6");
    tastiera.add(tasto6);
    Button tasto7 = new Button("7");
    tastiera.add(tasto7);
    Button tasto8 = new Button("8");
    tastiera.add(tasto8);
    Button tasto9 = new Button("9");
    tastiera.add(tasto9);

    add(banner, BorderLayout.NORTH);
    add(tastiera, BorderLayout.SOUTH);
  }
Exemple #2
0
  public void init() {
    sounds = new TetrisSound();
    installNewPiece();

    pause_resume_butt.setEnabled(false);
    start_newgame_butt.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent ae) {
            if (start_newgame_butt.getLabel().equals("Start")) startGame();
            else newGame();
          }
        });
    pause_resume_butt.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent ae) {
            if (pause_resume_butt.getLabel().equals("Pause")) pauseGame();
            else resumeGame();
          }
        });

    KeyListener key_listener =
        new KeyAdapter() {
          public void keyPressed(KeyEvent e) {
            if (timer.isPaused()) return;
            if (e.getKeyCode() == 37 || e.getKeyCode() == 39) {
              int dir = e.getKeyCode() == 37 ? -1 : 1;
              synchronized (timer) {
                cur_piece.cut();
                cur_piece.setX(cur_piece.getX() + dir);
                if (!cur_piece.canPaste()) cur_piece.setX(cur_piece.getX() - dir);
                cur_piece.paste();
              }
              game_grid.repaint();
            } else if (e.getKeyCode() == 38) {
              synchronized (timer) {
                cur_piece.cut();
                cur_piece.rotate();
                if (!cur_piece.canPaste()) cur_piece.rotateBack();
                cur_piece.paste();
              }
              game_grid.repaint();
            }
            if (e.getKeyCode() == 40) {
              timer.setFast(true);
            }
          }
        };

    start_newgame_butt.addKeyListener(key_listener);
    pause_resume_butt.addKeyListener(key_listener);

    Panel right_panel = new Panel(new GridLayout(3, 1));
    right_panel.setBackground(BACKGROUND_COLOR);

    Panel control_panel = new Panel();
    control_panel.add(start_newgame_butt);
    control_panel.add(pause_resume_butt);
    control_panel.setBackground(BACKGROUND_COLOR);
    right_panel.add(control_panel);

    Panel tmp = new Panel(new BorderLayout());
    tmp.add("North", new TetrisLabel("    Next Piece:"));
    tmp.add("Center", next_piece_canvas);
    tmp.setBackground(BACKGROUND_COLOR);
    right_panel.add(tmp);

    Panel stats_panel = new Panel(new GridLayout(4, 2));
    stats_panel.add(new TetrisLabel("    Rows Deleted: "));
    stats_panel.add(rows_deleted_label);
    stats_panel.add(new TetrisLabel("    Level: "));
    stats_panel.add(level_label);
    stats_panel.add(new TetrisLabel("    Score: "));
    stats_panel.add(score_label);
    stats_panel.add(new TetrisLabel("    High Score: "));
    stats_panel.add(high_score_label);
    tmp = new Panel(new BorderLayout());
    tmp.setBackground(BACKGROUND_COLOR);
    tmp.add("Center", stats_panel);
    right_panel.add(tmp);

    this.setLayout(new GridLayout(1, 2));
    this.add(game_grid);
    this.add(right_panel);
    this.setBackground(BACKGROUND_COLOR);
    this.validate();
  }