Skip to content
Snippets Groups Projects
Select Git revision
  • 6391b0a948e08b06b581762f9411ed8c557b0fcb
  • master default protected
2 results

GameWindow.java

Blame
  • user avatar
    steyer authored
    6391b0a9
    History
    GameWindow.java 2.83 KiB
    package game;
    
    import javax.swing.*;
    import javax.swing.filechooser.FileFilter;
    import javax.swing.filechooser.FileNameExtensionFilter;
    import java.awt.*;
    
    public class GameWindow extends JFrame {
    
        private GameModel model;
        private JTable table;
        private JFileChooser fileChooser;
    
        public GameWindow(GameModel model){
            super("Game of Life");
            this.model = model;
            table = new GameBoardTable(model);
            fileChooser = new JFileChooser();
            FileFilter filter = new FileNameExtensionFilter("JSON File (.json)", "json");
            fileChooser.setFileFilter(filter);
    
            setLayout(new BorderLayout());
            add(table, BorderLayout.CENTER);
    
            JPanel bottomPanel = new JPanel();
            JButton startButton = new JButton("Indítás");
            JButton stopButton = new JButton("Megállítás");
            JButton saveButton = new JButton("Mentés");
    
            GameWindowButtonsListener buttonListener = new GameWindowButtonsListener(model);
    
            startButton.setActionCommand("start");
            stopButton.setActionCommand("stop");
    
            startButton.addActionListener(buttonListener);
            stopButton.addActionListener(buttonListener);
            saveButton.addActionListener(new SaveButtonListener(this));
    
            bottomPanel.add(startButton);
            bottomPanel.add(stopButton);
            bottomPanel.add(saveButton);
    
            add(bottomPanel, BorderLayout.PAGE_END);
    
            JMenuItem startItem = new JMenuItem("Indítás");
            startItem.setActionCommand("start");
            JMenuItem stopItem = new JMenuItem("Megállítás");
            stopItem.setActionCommand("stop");
    
            startItem.addActionListener(buttonListener);
            stopItem.addActionListener(buttonListener);
    
            JMenuItem saveItem = new JMenuItem("Mentés");
            saveItem.addActionListener(new SaveButtonListener(this));
            JMenuItem exitItem = new JMenuItem("Bezárás");
            exitItem.addActionListener(new ExitListener(this));
            JMenu menu = new JMenu("Menü");
            menu.add(startItem);
            menu.add(stopItem);
            menu.addSeparator();
            menu.add(exitItem);
            JMenuBar menuBar = new JMenuBar();
            menuBar.add(menu);
            setJMenuBar(menuBar);
    
            addWindowListener(new GameWindowAdapter(model));
    
            pack();
            setResizable(false);
            setLocationRelativeTo(null);
        }
    
        public void showSaveDialog(){
            if (model.isRunning()){
                JOptionPane.showMessageDialog(this, "Csak megállított játékot lehet menteni",
                        "", JOptionPane.WARNING_MESSAGE);
                return;
            }
            int returnVal = fileChooser.showSaveDialog(this);
            if (returnVal == JFileChooser.APPROVE_OPTION){
                model.saveToFile(fileChooser.getSelectedFile());
            }
        }
    
        public void dispose(){
            model.stop();
            super.dispose();
        }
    }