Example #1
0
  public View(Model model) {

    this.model = model;
    model.makeMeObserver(this);

    frame = new JFrame();

    statusbar = new JLabel(" 0");
    board = new Board();
    frame.add(board);

    JMenuBar menubar = new JMenuBar();
    JMenu file = new JMenu("Settings");
    file.setMnemonic(KeyEvent.VK_F);

    JMenuItem eMenuItem = new JMenuItem("New game");
    eMenuItem.setToolTipText("Start a new game");
    eMenuItem.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent event) {
            menuEvent = -1;
            setChanged();
            notifyObservers();
            menuEvent = 0;
          }
        });
    file.add(eMenuItem);

    eMenuItem = new JMenuItem("Pause");
    eMenuItem.setMnemonic(KeyEvent.VK_P);
    eMenuItem.setToolTipText("Set pause");
    eMenuItem.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent event) {
            menuEvent = 1;
            setChanged();
            notifyObservers();
            menuEvent = 0;
          }
        });
    file.add(eMenuItem);

    JMenu imp = new JMenu("Set level");
    imp.setMnemonic(KeyEvent.VK_M);

    JMenuItem lvl1 = new JMenuItem("level 1");
    lvl1.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent event) {
            menuEvent = 2;
            setChanged();
            notifyObservers();
            menuEvent = 0;
          }
        });

    JMenuItem lvl2 = new JMenuItem("level 2");
    lvl2.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent event) {
            menuEvent = 3;
            setChanged();
            notifyObservers();
            menuEvent = 0;
          }
        });

    JMenuItem lvl3 = new JMenuItem("level 3");
    lvl3.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent event) {
            menuEvent = 4;
            setChanged();
            notifyObservers();
            menuEvent = 0;
          }
        });

    JMenuItem lvl4 = new JMenuItem("level 4");
    lvl4.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent event) {
            menuEvent = 5;
            setChanged();
            notifyObservers();
            menuEvent = 0;
          }
        });

    JMenuItem lvl5 = new JMenuItem("level 5");
    lvl5.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent event) {
            menuEvent = 6;
            setChanged();
            notifyObservers();
            menuEvent = 0;
          }
        });

    imp.add(lvl1);
    imp.add(lvl2);
    imp.add(lvl3);
    imp.add(lvl4);
    imp.add(lvl5);

    file.add(imp);

    eMenuItem = new JMenuItem("Table of recorgs");
    eMenuItem.setToolTipText("Show table of records");
    eMenuItem.addActionListener(
        new ActionListener() {

          public void actionPerformed(ActionEvent event) {
            recordDialog ad;
            try {
              ad = new recordDialog();
              ad.setVisible(true);
            } catch (IOException e) {
              e.printStackTrace();
            }
          }
        });
    file.add(eMenuItem);

    eMenuItem = new JMenuItem("Exit");
    eMenuItem.setMnemonic(KeyEvent.VK_C);
    eMenuItem.setToolTipText("Exit application");
    eMenuItem.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent event) {
            System.exit(0);
          }
        });
    file.add(eMenuItem);

    menubar.add(file);
    frame.setJMenuBar(menubar);

    statusbar.setPreferredSize(new Dimension(-1, 22));
    statusbar.setBorder(LineBorder.createGrayLineBorder());
    frame.add(statusbar, BorderLayout.SOUTH);

    frame.setSize(200, 400);
    frame.setTitle("Tetris");
    frame.setLocationRelativeTo(null);
  }
  private void createMenuBar() {

    JMenu file = new JMenu("File");
    file.setMnemonic(KeyEvent.VK_F);
    this.add(file);

    JMenuItem newMap = new JMenuItem("New");
    newMap.addActionListener(
        new ActionListener() {

          @Override
          public void actionPerformed(ActionEvent e) {
            NewMapDialog nmd = new NewMapDialog();
            MapSize newMapSize = nmd.showDialog();
            if (newMapSize != null) {
              map.initializeGrid(newMapSize);
            }
          }
        });
    newMap.setToolTipText("Make a new map");
    newMap.setMnemonic(KeyEvent.VK_N);
    file.add(newMap);

    JMenuItem loadMap = new JMenuItem("Open");
    loadMap.setToolTipText("Open saved new map");
    loadMap.setMnemonic(KeyEvent.VK_O);
    loadMap.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
            controller.loadMap();
          }
        });
    file.add(loadMap);

    JMenuItem saveMap = new JMenuItem("Save");
    saveMap.setToolTipText("Save map");
    saveMap.setMnemonic(KeyEvent.VK_S);
    saveMap.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
            JFileChooser saveMapDialog = new JFileChooser("D:\\");
            if (saveMapDialog.showSaveDialog((JMenuItem) e.getSource())
                == JFileChooser.APPROVE_OPTION) {
              try {
                File mapSaveFile = saveMapDialog.getSelectedFile();
                mapSaveFile = new File(mapSaveFile.toString() + ".jh3m");
                if (!mapSaveFile.exists()) {
                  mapSaveFile.createNewFile();
                }
                FileOutputStream mapSaveStream = new FileOutputStream(mapSaveFile);
                ObjectOutputStream mapSaveObjectStream = new ObjectOutputStream(mapSaveStream);
                SavedMap currentMapToSave = new SavedMap(map.getMapSize(), map.getMapCells());
                mapSaveObjectStream.writeObject(currentMapToSave);
                mapSaveObjectStream.close();
                mapSaveStream.close();
                JOptionPane.showMessageDialog(
                    null, "Zapis udany!", "Zapisano", JOptionPane.INFORMATION_MESSAGE);

              } catch (Exception ex) {
                JOptionPane.showMessageDialog(
                    null, ex.toString(), "ERROR", JOptionPane.ERROR_MESSAGE);
                ex.printStackTrace();
              }
            }
          }
        });
    file.add(saveMap);

    JMenu edit = new JMenu("Edit");
    edit.setMnemonic(KeyEvent.VK_E);
    this.add(edit);

    JMenuItem eShowGrid = new JMenuItem("Show/Hide grid");
    eShowGrid.setToolTipText("Shows/Hides grid on the map");
    eShowGrid.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent event) {
            map.showHideGrid();
          }
        });
    edit.add(eShowGrid);
  }